From 05cd1033cded73a66932ab42eb063ad9b8b03b59 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Tue, 5 Apr 2022 22:30:46 -0500 Subject: [PATCH] Make everything look like the problem statement --- inc/globals.hpp | 10 ++++++---- src/consumer.cpp | 14 ++++++-------- src/main.cpp | 8 ++------ src/producer.cpp | 23 +++++++++++++---------- src/transformer.cpp | 18 ++++++++++-------- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/inc/globals.hpp b/inc/globals.hpp index bf6d4bb..3b28313 100644 --- a/inc/globals.hpp +++ b/inc/globals.hpp @@ -1,10 +1,14 @@ #define N 8 -#define timescale 5 +#define timescale 10000 //function defines, renaming wait and signal #define wait(x) sem_wait(&x) #define signal(x) sem_post(&x) +// renaming link and unlink to match pseudocode semantics +#define link(x,y) list_link(y,x) +#define unlink(x) list_unlink(x) + // Shared memory through global variables // Create all of memory extern block memory[N]; @@ -15,6 +19,4 @@ extern list *freelist, *list1, *list2; // count semaphores extern sem_t sem_freelist, sem_list1, sem_list2; // binary semaphores -extern sem_t mut_freelist, mut_list1, mut_list2; -// binary semaphores for reading/writing -extern sem_t mut_take, mut_give; \ No newline at end of file +extern sem_t mut_freelist, mut_list1, mut_list2; \ No newline at end of file diff --git a/src/consumer.cpp b/src/consumer.cpp index be4a2d2..cc64a95 100644 --- a/src/consumer.cpp +++ b/src/consumer.cpp @@ -4,24 +4,23 @@ #include "globals.hpp" // lists, sems, muts #include "consumer.hpp" -#define unlink(x) list_unlink(x) -#define link(x) list_link(x) - int consume(block* c); void *consumer (void *) { + block* c; while (true) { - wait(sem_list2); wait(mut_list2); - block* c = list_unlink(list2); + //* c:=unlink(list-2); + c = unlink(list2); signal(mut_list2); - //* consume information in block c + //* consume_information_in_block(c) consume(c); wait(mut_freelist); - list_link(freelist, c); + //* link(c, freelist) + link(c, freelist); signal(mut_freelist); signal(sem_freelist); @@ -30,6 +29,5 @@ void *consumer (void *) { } int consume(block *c) { - usleep(75*timescale); return c->data; } diff --git a/src/main.cpp b/src/main.cpp index 3f72976..a39bd8f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,19 +35,15 @@ int main (int argc, char* argv[]) { sem_init(&mut_freelist, 0, 1); sem_init(&mut_list1, 0, 1); sem_init(&mut_list2, 0, 1); - sem_init(&mut_take, 0, 1); - sem_init(&mut_give, 0, 1); - //TODO: Use pthreads to split execution + //* Use pthreads to split execution // Make some pthreads pthread_t thread1, thread2, thread3; // Create thread1 as producer pthread_create(&thread1, NULL, producer, NULL); - // Create thread2 as transformer pthread_create(&thread2, NULL, transformer, NULL); - // Create thread3 as consumer pthread_create(&thread3, NULL, consumer, NULL); @@ -56,7 +52,7 @@ int main (int argc, char* argv[]) { list_print(freelist); list_print(list1); list_print(list2); - usleep(100*timescale); + usleep(timescale); } // Wait for them to finish pthread_join(thread1, NULL); diff --git a/src/producer.cpp b/src/producer.cpp index b5d2849..b76ee00 100644 --- a/src/producer.cpp +++ b/src/producer.cpp @@ -7,26 +7,29 @@ void produce(block* b); void *producer (void *) { - while (true) { - + block *b; + while (true) + { + int v; + while(sem_getvalue(&sem_freelist, &v), v <= 1); wait(sem_freelist); // wait for freelist not empty wait(mut_freelist); // lock freelist - block *b = list_unlink(freelist); + //* b:= unlink(freelist); + b = unlink(freelist); signal(mut_freelist); // unlock freelist - //* Produce information in block b + //* produce_information_in_block(b) produce(b); - wait(mut_list1); // lock l1 - list_link(list1, b); - signal(mut_list1); // unlock l1 - signal(sem_list1); - + wait(mut_list1); // lock mutex + //* link(b, list1); + link(b, list1); + signal(mut_list1); // unlock mutex + signal(sem_list1); // signal new item on list } return nullptr; } void produce(block *b) { - usleep(65*timescale); return; } diff --git a/src/transformer.cpp b/src/transformer.cpp index 9b11cbe..215d23f 100644 --- a/src/transformer.cpp +++ b/src/transformer.cpp @@ -9,27 +9,30 @@ void transform(block* x, block* y); void *transformer (void *) { block *x, *y; while (true) { - wait(sem_list1); + wait(sem_freelist); wait(mut_list1); - x = list_unlink(list1); + //* x:=unlink(list1); + x = unlink(list1); signal(mut_list1); - wait(sem_freelist); wait(mut_freelist); - y = list_unlink(freelist); + //* y:=unlink(freelist); + y = unlink(freelist); signal(mut_freelist); - //* use block x to produce info in y + //* use_block_x_to_produce_info_in_y(x,y) transform(x, y); wait(mut_freelist); - list_link(freelist, x); + //* link(x, freelist); + link(x, freelist); signal(mut_freelist); signal(sem_freelist); wait(mut_list2); - list_link(list2, y); + //* link(y, list2); + link(y, list2); signal(mut_list2); signal(sem_list2); @@ -38,6 +41,5 @@ void *transformer (void *) { } void transform(block *x, block *y) { - usleep(55*timescale); return; }