Code cleanup and commentary

This commit is contained in:
2022-04-05 23:53:57 -05:00
parent ff108dcd0b
commit 4cdc3d2d89
6 changed files with 31 additions and 30 deletions

View File

@@ -1,5 +1,4 @@
#include <semaphore.h>
#include <unistd.h>
#include <semaphore.h> // POSIX semaphores!
#include "list.hpp" // list implementation
#include "globals.hpp" // lists, sems, muts
#include "consumer.hpp"
@@ -7,7 +6,7 @@
int consume(block* c);
void *consumer (void *) {
block* c;
block *c;
while (true) {
// wait for element on list2
wait(sem_list2);
@@ -31,7 +30,6 @@ void *consumer (void *) {
signal(sem_freelist_minus_1);
// signal new element on freelist
signal(sem_freelist);
}
return nullptr;
}

View File

@@ -1,13 +1,12 @@
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include "list.hpp"
#include "globals.hpp"
#include "producer.hpp"
#include "consumer.hpp"
#include "transformer.hpp"
#include <semaphore.h> // POSIX semaphores!
#include <pthread.h> // POSIX threads!
#include <unistd.h> // usleep
#include <stdio.h> // printf
#include "list.hpp" // list implementation
#include "globals.hpp" // globals/CPP defines
#include "producer.hpp" // Thread-1
#include "consumer.hpp" // Thread-3
#include "transformer.hpp" // Thread-2
// Create all of memory
block memory[N] = {0};
@@ -24,7 +23,7 @@ int main (int argc, char* argv[]) {
// initialize the freelist
list_init(freelist, memory, N);
//TODO: Implement a semaphore solution to the problem
//* Implement a semaphore solution to the problem
// counting semaphores measure the length
sem_init(&sem_freelist, 0, freelist->length);
sem_init(&sem_freelist_minus_1, 0, freelist->length-1);
@@ -38,18 +37,19 @@ int main (int argc, char* argv[]) {
//* 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);
// print some information about the 3 lists
while (true) {
printf("\033c");
printf("\033cfreelist: ");
list_print(freelist);
printf(" list1: ");
list_print(list1);
printf(" list2: ");
list_print(list2);
usleep(timescale);
}

View File

@@ -1,5 +1,4 @@
#include <semaphore.h>
#include <unistd.h>
#include <semaphore.h> // POSIX semaphores!
#include "list.hpp" // list implementation
#include "globals.hpp" // lists, sems, muts
#include "producer.hpp"

View File

@@ -1,10 +1,9 @@
#include <semaphore.h>
#include <unistd.h>
#include <semaphore.h> // POSIX semaphores!
#include "list.hpp" // list implementation
#include "globals.hpp" // lists, sems, muts
#include "transformer.hpp"
void transform(block* x, block* y);
void transform(block *x, block *y);
void *transformer (void *) {
block *x, *y;
@@ -47,12 +46,10 @@ void *transformer (void *) {
signal(mut_list2);
// signal new element on list2
signal(sem_list2);
}
return nullptr;
}
void transform(block *x, block *y) {
usleep(timescale);
return;
}