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 0000000..16632e8 Binary files /dev/null and b/while_loops/while_loops differ 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