diff --git a/Makefile b/Makefile index 9b218b5..6fddbbf 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +# ---------- Variables listed below --------- # + # Executable TARGET := main.out @@ -17,7 +19,12 @@ CFLAGS = -I$(IPATH) -pthread -lrt SOURCES = $(wildcard $(SPATH)/*.cpp) OBJECTS = $(addprefix $(OPATH)/,$(notdir $(SOURCES:.cpp=.o))) -.PHONY: all clean dump + +# ----------- Targets listed below ---------- # +# Some targets aren't real +.PHONY: all clean run dump +# Don't autodelete object files: +.PRECIOUS: $(OPATH)/%.o all: $(DPATH) $(OPATH) $(TARGET) @@ -31,6 +38,9 @@ clean: -rm $(TARGET) -rm -r dep obj +run: + -$(addprefix ./,$(addsuffix ;,$(TARGET))) + $(DPATH) $(OPATH): mkdir -p $@ @@ -41,8 +51,6 @@ $(DPATH) $(OPATH): $(OPATH)/%.o: $(SPATH)/%.cpp $(CC) $(CFLAGS) -MMD -MF $(DPATH)/$(@F:.o=.d) -o $@ -c $< -# Don't autodelete object files: -.SECONDARY: $(OPATH)/%.o - +# --------- Inclusions listed below --------- # # use dependencies when rebuilding -include $(wildcard $(DPATH)/*.d) diff --git a/inc/globals.hpp b/inc/globals.hpp index 117cb4f..4da9b08 100644 --- a/inc/globals.hpp +++ b/inc/globals.hpp @@ -13,7 +13,6 @@ // Create all of memory extern block memory[N]; // create the three lists -extern list lists[3]; extern list *freelist, *list1, *list2; // count semaphores diff --git a/src/consumer.cpp b/src/consumer.cpp index 5ced6ae..df81270 100644 --- a/src/consumer.cpp +++ b/src/consumer.cpp @@ -1,5 +1,4 @@ -#include -#include +#include // 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; } diff --git a/src/main.cpp b/src/main.cpp index 6a4424f..63a91a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,12 @@ -#include -#include -#include -#include -#include -#include "list.hpp" -#include "globals.hpp" -#include "producer.hpp" -#include "consumer.hpp" -#include "transformer.hpp" +#include // POSIX semaphores! +#include // POSIX threads! +#include // usleep +#include // 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); } diff --git a/src/producer.cpp b/src/producer.cpp index 53c14a8..fe9662f 100644 --- a/src/producer.cpp +++ b/src/producer.cpp @@ -1,5 +1,4 @@ -#include -#include +#include // POSIX semaphores! #include "list.hpp" // list implementation #include "globals.hpp" // lists, sems, muts #include "producer.hpp" diff --git a/src/transformer.cpp b/src/transformer.cpp index dd610b9..18f2338 100644 --- a/src/transformer.cpp +++ b/src/transformer.cpp @@ -1,10 +1,9 @@ -#include -#include +#include // 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; }