From 9aa7973d2f7962e5303a5729ba56ccb87ab729f1 Mon Sep 17 00:00:00 2001 From: stitchy Date: Wed, 29 Nov 2023 20:33:45 +0000 Subject: [PATCH] Done in one go xP --- threads/main.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++ threads/main.h | 13 ++++++ threads/makefile | 8 ++++ 3 files changed, 135 insertions(+) create mode 100644 threads/main.c create mode 100644 threads/main.h create mode 100644 threads/makefile diff --git a/threads/main.c b/threads/main.c new file mode 100644 index 0000000..92df140 --- /dev/null +++ b/threads/main.c @@ -0,0 +1,114 @@ +#include "main.h" + + +#define SIZE 1 +pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t myCond1, myCond2; +int myCount; + +int isProduced = 0; + +int main() { + + printf("PROGRAM START\n"); + fflush(stdout); + + // Create the consumer thread + pthread_t consumerID; + int resultInt = pthread_create(&consumerID, NULL, consumer, NULL); + + producer(); + +} + + + +void producer() { + + while (1) { + + pthread_mutex_lock(&myMutex); + print_mutex("PRODUCER", "locked"); + + + while (isProduced == SIZE) { + print_wait("PRODUCER", "myCond2"); + pthread_cond_wait(&myCond2, &myMutex); + } + + if(myCount == 10) + end_program(); + + myCount++; + isProduced++; + + pthread_mutex_unlock(&myMutex); + print_mutex("PRODUCER", "unlocked"); + pthread_cond_signal(&myCond1); + print_signal("PRODUCER", "myCond1"); + } + +} + +void* consumer() { + + printf("CONSUMER THREAD CREATED\n"); + fflush(stdout); + + while (1) { + + pthread_mutex_lock(&myMutex); + print_mutex("CONSUMER", "locked"); + + while (isProduced == 0) { + print_wait("CONSUMER", "myCond1"); + pthread_cond_wait(&myCond1, &myMutex); + } + + //get_item(); + print_count(); + isProduced--; + + pthread_mutex_unlock(&myMutex); + print_mutex("CONSUMER", "unlocked"); + pthread_cond_signal(&myCond2); + print_signal("CONSUMER", "myCond1"); + } + +} + + +void end_program() { + + printf("PROGRAM END\n"); + fflush(stdout); + exit(0); + +} +void print_mutex(char* type, char* lockstate) { + + printf("%s: myMutex %s\n", type, lockstate); + fflush(stdout); + +} + +void print_count() { + + printf("myCount: %d -> %d\n", myCount-1, myCount); + fflush(stdout); + +} + +void print_wait(char* type, char* var) { + + printf("%s: waiting on %s\n", type, var); + fflush(stdout); + +} + +void print_signal(char* type, char* var) { + + printf("%s: signaling %s\n", type, var); + fflush(stdout); + +} diff --git a/threads/main.h b/threads/main.h new file mode 100644 index 0000000..beb3fb2 --- /dev/null +++ b/threads/main.h @@ -0,0 +1,13 @@ +#include "pthread.h" +#include "stdio.h" +#include "stdlib.h" + + +void* consumer(); +void producer(); +void end_program(); + +void print_mutex(char*, char*); +void print_count(); +void print_signal(char*, char*); +void print_wait(char*, char*); diff --git a/threads/makefile b/threads/makefile new file mode 100644 index 0000000..6a2a24a --- /dev/null +++ b/threads/makefile @@ -0,0 +1,8 @@ +CC=gcc --std=gnu99 -g +output=myCounter + +all: main.c main.h + $(CC) main.c -o $(output) + +clean: + rm -fr *.o vgcore.* $(output)