From 12e2fb1ef8fb6d0011e21ec89c840fce518faf51 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Sun, 3 Dec 2023 19:40:14 -0700 Subject: [PATCH] while loops assignment --- while_loops/Makefile | 10 ++ while_loops/README.md | 3 + while_loops/pseudocode.c | 39 +++++ while_loops/run.sh | 11 ++ while_loops/while_loops | Bin 0 -> 23184 bytes while_loops/while_loops.c | 332 ++++++++++++++++++++++++++++++++++++++ while_loops/while_loops.h | 45 ++++++ 7 files changed, 440 insertions(+) create mode 100644 while_loops/Makefile create mode 100644 while_loops/README.md create mode 100644 while_loops/pseudocode.c create mode 100644 while_loops/run.sh create mode 100755 while_loops/while_loops create mode 100644 while_loops/while_loops.c create mode 100644 while_loops/while_loops.h diff --git a/while_loops/Makefile b/while_loops/Makefile new file mode 100644 index 0000000..9bdf227 --- /dev/null +++ b/while_loops/Makefile @@ -0,0 +1,10 @@ +all: while_loops + +while_loops: while_loops.c while_loops.h + gcc while_loops.c -o while_loops -lpthread -Wall -Wextra -Werror -pedantic -g -fsanitize=thread + +run: + sh run.sh + +clean: + rm -f while_loops \ No newline at end of file diff --git a/while_loops/README.md b/while_loops/README.md new file mode 100644 index 0000000..7c480c4 --- /dev/null +++ b/while_loops/README.md @@ -0,0 +1,3 @@ +# while loops + +Do the while loops really loop? diff --git a/while_loops/pseudocode.c b/while_loops/pseudocode.c new file mode 100644 index 0000000..d7e7796 --- /dev/null +++ b/while_loops/pseudocode.c @@ -0,0 +1,39 @@ +pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t nonempty = PTHREAD_COND_INITIALIZER; +pthread_cond_t nonfull = PTHREAD_COND_INITIALIZER; + +int buffer[SIZE]; + +int put = 0; +int get = 0; + +void insert (int x) +{ + pthread_mutex_lock (&lock); + while ( + (put > get && (put - get) == SIZE - 1) || + (put < get && (get + put) == SIZE - 1) + ) { + pthread_cond_wait (&nonfull, &lock); + } + buffer[put] = x; + put = (put + 1) % SIZE; + pthread_cond_signal (&nonempty); + pthread_mutex_unlock (&lock); + +} + +int remove () +{ + int x; + pthread_mutex_lock (&lock); + while (put == get) { + pthread_cond_wait (&nonempty, &lock); + } + x = buffer[get]; + get = (get + 1) % SIZE; + pthread_cond_signal (&nonfull); + pthread_mutex_unlock (&lock); + return x; + +} \ No newline at end of file diff --git a/while_loops/run.sh b/while_loops/run.sh new file mode 100644 index 0000000..21bbb2d --- /dev/null +++ b/while_loops/run.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +for i in 1 2 4 8 16 +do + for j in 1 2 3 4 5 + do + echo "running with $i threads and 1000 operations for the $j time" + ./while_loops $i 1000 + done + +done \ No newline at end of file diff --git a/while_loops/while_loops b/while_loops/while_loops new file mode 100755 index 0000000000000000000000000000000000000000..16632e87ece8b6291f9c6716ce91b355d341dd59 GIT binary patch literal 23184 zcmeHv4R}=5x$fRG`I#g`5)ys_1Q`$zh5Ya%1`(J*1}7RI0#Vuu!;nmpsgs#FKm2GZ zXw>U8(t_HSw%h~tv}#Xn57&E6F9$p&h!tvi&Y{-Ra#~wstDdA_OSMJYYIELq{mkr4 zGCl2cpXWZ$eePY!tZ#ka`o3>{>svp2ueI0Sdpu36ZKi4HU^A8*#MTzM1xdz+QF@y| z6|K%FFx`P+LfttMVjp zD@;O%641~nmD!Y|t$kA^-dE)o2@OkW5Q2+FF(^5jN4X11?t+rj&`|bksOv{H{B2bE z8*Of2#N7l&gNV~m7u)=1yp8G06_Vj{g6_8`KgdGAn1vqBLic8&-7Mv*-cYaN(C3E`uG+~)%tkNLyC_C&bV7YN6ryEG*l z@V70pB6UW0G#HMz8|~3R!07UaLXlRTzatuq2U6Tp+0J0x=#F<%kFP5c5A5_o;I0&@ zH4<*~?eJTe1SJ;i2>U}RE-9D@3sGgOC=Avp-K{7g9!Q}dj|9U;cOo7$;=!(fHZT}Q zL9wVm+{V1tPJh&B6*~R#NDxezom!Poy8KW8oPOc_R7*q^mcm<^T5R0h)VOk$Z$b5f z>g(ZebJ(9vU^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zU0wW|j0LmayGz#Li$&kfP3umget1QcArq8!}ef=7b&$G5c ze8C30eLjy@5xouT4PV1Y*4D3ST!qpzZWo6Ua6^jqqAqmZ>eU-O8+{w=S2lTkskNS5 zfT@qt$nW2%&(O#Zq_yS^yY5hLUBDrc$?>g#*jQU&+AG(XL)QqyMRqM01Sj5+f zjU+5S^wK!J*b~cmxApro!!|5*X`_8V$MUV8j3su~9W3$XoMEMjkLwICMJ$H3rFs$X z^KD$SO0|^wW{}>>(bB@j;!8jl&(>KN@Zu9i>a#z?{l!?&7d+qzn8npXv}a1x~Ym5tb> z?l44NCpMss>b6~B=#vl|WFlufHb}U&XGMI-!`82#0xH!ViW}9!rfPie329NW>S#m^ zkLp0DnhZMIAgeJcAy@8FLZ_kF-xb8haz_NZ5LJr;)L2XHZi|msaj!yrz$%EztQw7L zRAch$!YqTD1hyqQpwu7kV5UTa;r575`?qc5ww}u2uD(vgk2;C&t&>WVo`e5)AlDzb zB;uY~hB}*DBs}@;hyAk;VHJ z2A7D_QKmfCk~sg7D*vF(4Gbq%l>AYF;XsCh4h6NI2HeMbvGM4*f{=yun%BobEn z^J-tb##eEUhi|9=LQOb=95?&*Ypd>VBb;0J+(ie zzn`idtGZ)KpSJ6D$g=(^`5HGcyk;;>0IsNq1zoO&Zz0zzulE%Ol>FIY(j)Evb%wlN z*B^CB!EEJgwZBvHJ&K^$fqgDX(DG~}25q;*ux5hsr0M$it99ggm*QaMEWu?-Gs%4x zveahKwY;7ubhcKfp-ai@d<`$VWU5v(stjEp%~z8}-ZevVyfH>|N|>R}SeivXM^)(a z+A&H^rhH=-`7bS$0xxC^xJ>yLC7B>?8hA5-jyC^ue#~&O5PbSpDSt5|Gt>UDS>zvE NC*^l#2xOqfe*zg5z8C-i literal 0 HcmV?d00001 diff --git a/while_loops/while_loops.c b/while_loops/while_loops.c new file mode 100644 index 0000000..7a6ec3d --- /dev/null +++ b/while_loops/while_loops.c @@ -0,0 +1,332 @@ +// Bounded buffer example using condition variables `nonempty` and `nonfull` +// uses while loops to check if the buffer is full or empty +// we will add additional code to the two while loops to see if we ever +// go through the while loop more than once during a single call to insert() +// or remove() + +// We will need to be able to create N threads using a command line argument +// each thread will randomly insert and/or remove an item from the shared buffer +#include "while_loops.h" + +#define DEBUG 0 + +#define SIZE 8 + +pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t nonempty = PTHREAD_COND_INITIALIZER; +pthread_cond_t nonfull = PTHREAD_COND_INITIALIZER; +// initializs all values to 999 so it's easy to see if a value was overwritten +int buffer[SIZE]; +int put = 0; // buffer index for next insertion +int get = 0; // buffer index for next removal + +int buffer_is_empty(int put, int get) +{ + return put == get; +} + +int buffer_is_full(int put, int get) +{ + int is_full = + (put > get && (put - get) == SIZE - 1) || + (put < get && (get - put) == 1); + + return is_full; +} + +// each thread will run N insertions and N removals in a random order +// takes two functions as arguments +void *run_inserts_and_removals(void *void_args) +{ + // printf("running inserts and removals for thread %lu\n", pthread_self()); + struct thread_args *proper_args = (struct thread_args *)void_args; + + int total_while_loops = 0; + + int action_count = proper_args->operations_count * 2; + + for (int action_index = 0; action_index < action_count; action_index++){ + // printf("thread %lu action %d\n", pthread_self(), action_index); + int action = proper_args->actions[action_index]; + if (action == 0) { + if (buffer_is_full(put, get)) { + total_while_loops += proper_args->remove(proper_args); + } else { + total_while_loops += proper_args->insert(proper_args); + } + // total_while_loops += proper_args->insert(proper_args); + } else if (action == 1) { + if (buffer_is_empty(put, get)) { + total_while_loops += proper_args->insert(proper_args); + } else { + total_while_loops += proper_args->remove(proper_args); + } + // total_while_loops += proper_args->remove(proper_args); + } else { + printf("invalid action: %d\n", action); + } + } + + // printf("thread %lu finished\n", pthread_self()); + free(void_args); + + int *return_value = malloc(sizeof(int)); + *return_value = total_while_loops; + + return return_value; + +} + +int buffer_remove_bad() +{ + int while_loops = 0; + + pthread_mutex_lock(&lock); + while (buffer_is_empty(put, get)) { + while_loops++; + + pthread_cond_wait(&nonempty, &lock); + } + + get = (get + 1) % SIZE; + + pthread_cond_signal(&nonfull); + pthread_mutex_unlock(&lock); + + return while_loops; +} + +int buffer_insert_bad(struct thread_args *args) +{ + int while_loops = 0; + int value = args->value; + + pthread_mutex_lock(&lock); + while (buffer_is_full(put, get)) { + + while_loops++; + pthread_cond_wait(&nonfull, &lock); + } + buffer[put] = value; // insert value + put = (put + 1) % SIZE; // circular buffer + + pthread_cond_signal(&nonempty); + pthread_mutex_unlock(&lock); + + return while_loops; +} + +int buffer_insert_good(struct thread_args *args) +{ + int while_loops = 0; + + int value = args->value; + + pthread_mutex_lock(&lock); + while (buffer_is_full(put, get)) { + + while_loops++; + pthread_cond_wait(&nonfull, &lock); + } + buffer[put] = value; // insert value + put = (put + 1) % SIZE; // circular buffer + pthread_mutex_unlock(&lock); + pthread_cond_signal(&nonempty); + + return while_loops; +} + +int buffer_remove_good() +{ + int while_loops = 0; + + + pthread_mutex_lock(&lock); + while (buffer_is_empty(put, get)) { + while_loops++; + + pthread_cond_wait(&nonempty, &lock); + } + + get = (get + 1) % SIZE; + pthread_mutex_unlock(&lock); + pthread_cond_signal(&nonfull); + + + + return while_loops; +} + +int* generate_thread_actions(int num_values) +{ + int *actions = malloc(sizeof(int) * num_values * 2); + + int insertions = 0; + int removals = 0; + int action_index = 0; + int action = 0; + while (insertions < num_values && removals < num_values) { + // action = rand() % 2; + if (action == 0) { + if (insertions < num_values) { + actions[action_index] = 0; + insertions++; + action_index++; + } + } else if (action == 1) { + if (removals < num_values) { + actions[action_index] = 1; + removals++; + action_index++; + } + } + action = !action; + + } + + return actions; +} + +// generates num_threads threads with +// num_values insertions and num_values removals randomly sorted +pthread_t *create_threads( + int num_threads, + int num_values, + int (*insert)(struct thread_args *), + int (*remove)(struct thread_args *) +) +{ + pthread_t *threads = malloc(sizeof(pthread_t) * num_threads); + + for (int thread_index = 0; thread_index < num_threads; thread_index++) { + struct thread_args *args = malloc(sizeof(struct thread_args)); + args->value = thread_index; + args->actions = generate_thread_actions(num_values); + args->operations_count = num_values; + args->insert = insert; + args->remove = remove; + + pthread_create( + &threads[thread_index], + NULL, + run_inserts_and_removals, + args + ); + } + + return threads; +} + +int join_threads(pthread_t *threads, int num_threads) +{ + int total_while_loops = 0; + + for (int thread_index = 0; thread_index < num_threads; thread_index++) { + void * thread_return_value; + pthread_join(threads[thread_index], &thread_return_value); + + total_while_loops += *(int *)thread_return_value; + free(thread_return_value); + + } + + return total_while_loops; +} + +int reset_buffer() +{ + for (int i = 0; i < SIZE; i++) { + buffer[i] = 999; + } + put = 0; + get = 0; + + return 0; +} + +int main(int argc, char const *argv[]) +{ + // check for correct number of arguments + if (argc != 3) { + printf("usage: ./while_loops \n"); + return 1; + } + // command line arguments for number of threads and values + int num_threads = atoi(argv[1]); + int num_values = atoi(argv[2]); + int total_while_loops; + + // create arrays of threads + pthread_t *threads; + + + // Seed the random number generator + srand(time(NULL)); + + // test buffer_insert_bad and buffer_remove_bad + printf("\n"); + printf("testing buffer_insert_bad and buffer_remove_bad\n"); + // record cpu time + clock_t start, end; + double cpu_time_used; + start = clock(); + // printf("creating bad threads\n"); + threads = create_threads( + num_threads, + num_values, + buffer_insert_bad, + buffer_remove_bad + ); + // printf("created bad threads\n"); + + // wait for all threads to finish + // printf("joining bad threads\n"); + total_while_loops = join_threads(threads, num_threads); + // printf("joined bad threads\n"); + end = clock(); + cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; + + printf("total while loops: %d\n", total_while_loops); + printf("cpu time used: %f\n", cpu_time_used); + + reset_buffer(); + + // test buffer_insert_good and buffer_remove_good + printf("\n"); + printf("testing buffer_insert_good and buffer_remove_good\n"); + // record cpu time + start = clock(); + threads = create_threads( + num_threads, + num_values, + buffer_insert_good, + buffer_remove_good + ); + + // wait for all threads to finish and record total while loop iterations + total_while_loops = join_threads(threads, num_threads); + + end = clock(); + cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; + + printf("total while loops: %d\n", total_while_loops); + printf("cpu time used: %f\n", cpu_time_used); + + + if (DEBUG) { + printf("final buffer state: "); + for (int i = 0; i < SIZE; i++) { + printf("%d ", buffer[i]); + } + printf("\n"); + + printf("get: %d\n", get); + printf("put: %d\n", put); + + printf("\n"); + } + + + + return 0; +} diff --git a/while_loops/while_loops.h b/while_loops/while_loops.h new file mode 100644 index 0000000..436e18d --- /dev/null +++ b/while_loops/while_loops.h @@ -0,0 +1,45 @@ +#ifndef WHILE_LOOPS_H +#define WHILE_LOOPS_H + +#include +#include +#include +#include +#include + +// struct to pass value and number of insertions/removals to thread +struct thread_args { + // value for insertions + int value; + + // number of actions + int operations_count; + + // array of actions for each thread + int *actions; + + // insert function to be used + int (*insert)(struct thread_args *); + + // remove function to be used + int (*remove)(struct thread_args *); +}; + +// declare prototypes for all functions +void *run_inserts_and_removals(void *void_args); +int buffer_insert_good(struct thread_args *args); +int buffer_remove_good(); +int buffer_insert_bad(struct thread_args *args); +int buffer_remove_bad(); +int buffer_is_empty(); +int buffer_is_full(); +int* generate_thread_actions(int num_values); +pthread_t *create_threads( + int num_threads, + int num_values, + int (*insert)(struct thread_args *), + int (*remove)(struct thread_args *) +); +int join_threads(pthread_t *threads, int num_threads); + +#endif // WHILE_LOOPS_H \ No newline at end of file