Make everything look like the problem statement

This commit is contained in:
John 2022-04-05 22:30:46 -05:00
parent e16a4396d7
commit 05cd1033cd
5 changed files with 37 additions and 36 deletions

View File

@ -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];
@ -16,5 +20,3 @@ extern list *freelist, *list1, *list2;
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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}