Code cleanup and commentary
This commit is contained in:
parent
ff108dcd0b
commit
4cdc3d2d89
16
Makefile
16
Makefile
@ -1,3 +1,5 @@
|
|||||||
|
# ---------- Variables listed below --------- #
|
||||||
|
|
||||||
# Executable
|
# Executable
|
||||||
TARGET := main.out
|
TARGET := main.out
|
||||||
|
|
||||||
@ -17,7 +19,12 @@ CFLAGS = -I$(IPATH) -pthread -lrt
|
|||||||
SOURCES = $(wildcard $(SPATH)/*.cpp)
|
SOURCES = $(wildcard $(SPATH)/*.cpp)
|
||||||
OBJECTS = $(addprefix $(OPATH)/,$(notdir $(SOURCES:.cpp=.o)))
|
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)
|
all: $(DPATH) $(OPATH) $(TARGET)
|
||||||
|
|
||||||
@ -31,6 +38,9 @@ clean:
|
|||||||
-rm $(TARGET)
|
-rm $(TARGET)
|
||||||
-rm -r dep obj
|
-rm -r dep obj
|
||||||
|
|
||||||
|
run:
|
||||||
|
-$(addprefix ./,$(addsuffix ;,$(TARGET)))
|
||||||
|
|
||||||
$(DPATH) $(OPATH):
|
$(DPATH) $(OPATH):
|
||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
|
|
||||||
@ -41,8 +51,6 @@ $(DPATH) $(OPATH):
|
|||||||
$(OPATH)/%.o: $(SPATH)/%.cpp
|
$(OPATH)/%.o: $(SPATH)/%.cpp
|
||||||
$(CC) $(CFLAGS) -MMD -MF $(DPATH)/$(@F:.o=.d) -o $@ -c $<
|
$(CC) $(CFLAGS) -MMD -MF $(DPATH)/$(@F:.o=.d) -o $@ -c $<
|
||||||
|
|
||||||
# Don't autodelete object files:
|
# --------- Inclusions listed below --------- #
|
||||||
.SECONDARY: $(OPATH)/%.o
|
|
||||||
|
|
||||||
# use dependencies when rebuilding
|
# use dependencies when rebuilding
|
||||||
-include $(wildcard $(DPATH)/*.d)
|
-include $(wildcard $(DPATH)/*.d)
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
// Create all of memory
|
// Create all of memory
|
||||||
extern block memory[N];
|
extern block memory[N];
|
||||||
// create the three lists
|
// create the three lists
|
||||||
extern list lists[3];
|
|
||||||
extern list *freelist, *list1, *list2;
|
extern list *freelist, *list1, *list2;
|
||||||
|
|
||||||
// count semaphores
|
// count semaphores
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <semaphore.h>
|
#include <semaphore.h> // POSIX semaphores!
|
||||||
#include <unistd.h>
|
|
||||||
#include "list.hpp" // list implementation
|
#include "list.hpp" // list implementation
|
||||||
#include "globals.hpp" // lists, sems, muts
|
#include "globals.hpp" // lists, sems, muts
|
||||||
#include "consumer.hpp"
|
#include "consumer.hpp"
|
||||||
@ -7,7 +6,7 @@
|
|||||||
int consume(block* c);
|
int consume(block* c);
|
||||||
|
|
||||||
void *consumer (void *) {
|
void *consumer (void *) {
|
||||||
block* c;
|
block *c;
|
||||||
while (true) {
|
while (true) {
|
||||||
// wait for element on list2
|
// wait for element on list2
|
||||||
wait(sem_list2);
|
wait(sem_list2);
|
||||||
@ -31,7 +30,6 @@ void *consumer (void *) {
|
|||||||
signal(sem_freelist_minus_1);
|
signal(sem_freelist_minus_1);
|
||||||
// signal new element on freelist
|
// signal new element on freelist
|
||||||
signal(sem_freelist);
|
signal(sem_freelist);
|
||||||
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
28
src/main.cpp
28
src/main.cpp
@ -1,13 +1,12 @@
|
|||||||
#include <semaphore.h>
|
#include <semaphore.h> // POSIX semaphores!
|
||||||
#include <pthread.h>
|
#include <pthread.h> // POSIX threads!
|
||||||
#include <unistd.h>
|
#include <unistd.h> // usleep
|
||||||
#include <stdint.h>
|
#include <stdio.h> // printf
|
||||||
#include <stdio.h>
|
#include "list.hpp" // list implementation
|
||||||
#include "list.hpp"
|
#include "globals.hpp" // globals/CPP defines
|
||||||
#include "globals.hpp"
|
#include "producer.hpp" // Thread-1
|
||||||
#include "producer.hpp"
|
#include "consumer.hpp" // Thread-3
|
||||||
#include "consumer.hpp"
|
#include "transformer.hpp" // Thread-2
|
||||||
#include "transformer.hpp"
|
|
||||||
|
|
||||||
// Create all of memory
|
// Create all of memory
|
||||||
block memory[N] = {0};
|
block memory[N] = {0};
|
||||||
@ -24,7 +23,7 @@ int main (int argc, char* argv[]) {
|
|||||||
// initialize the freelist
|
// initialize the freelist
|
||||||
list_init(freelist, memory, N);
|
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
|
// counting semaphores measure the length
|
||||||
sem_init(&sem_freelist, 0, freelist->length);
|
sem_init(&sem_freelist, 0, freelist->length);
|
||||||
sem_init(&sem_freelist_minus_1, 0, freelist->length-1);
|
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
|
//* Use pthreads to split execution
|
||||||
// Make some pthreads
|
// Make some pthreads
|
||||||
pthread_t thread1, thread2, thread3;
|
pthread_t thread1, thread2, thread3;
|
||||||
|
|
||||||
// Create thread1 as producer
|
// Create thread1 as producer
|
||||||
pthread_create(&thread1, NULL, producer, NULL);
|
pthread_create(&thread1, NULL, producer, NULL);
|
||||||
// Create thread2 as transformer
|
// Create thread2 as transformer
|
||||||
pthread_create(&thread2, NULL, transformer, NULL);
|
pthread_create(&thread2, NULL, transformer, NULL);
|
||||||
// Create thread3 as consumer
|
// Create thread3 as consumer
|
||||||
pthread_create(&thread3, NULL, consumer, NULL);
|
pthread_create(&thread3, NULL, consumer, NULL);
|
||||||
|
// print some information about the 3 lists
|
||||||
while (true) {
|
while (true) {
|
||||||
printf("\033c");
|
printf("\033cfreelist: ");
|
||||||
list_print(freelist);
|
list_print(freelist);
|
||||||
|
printf(" list1: ");
|
||||||
list_print(list1);
|
list_print(list1);
|
||||||
|
printf(" list2: ");
|
||||||
list_print(list2);
|
list_print(list2);
|
||||||
usleep(timescale);
|
usleep(timescale);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <semaphore.h>
|
#include <semaphore.h> // POSIX semaphores!
|
||||||
#include <unistd.h>
|
|
||||||
#include "list.hpp" // list implementation
|
#include "list.hpp" // list implementation
|
||||||
#include "globals.hpp" // lists, sems, muts
|
#include "globals.hpp" // lists, sems, muts
|
||||||
#include "producer.hpp"
|
#include "producer.hpp"
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
#include <semaphore.h>
|
#include <semaphore.h> // POSIX semaphores!
|
||||||
#include <unistd.h>
|
|
||||||
#include "list.hpp" // list implementation
|
#include "list.hpp" // list implementation
|
||||||
#include "globals.hpp" // lists, sems, muts
|
#include "globals.hpp" // lists, sems, muts
|
||||||
#include "transformer.hpp"
|
#include "transformer.hpp"
|
||||||
|
|
||||||
void transform(block* x, block* y);
|
void transform(block *x, block *y);
|
||||||
|
|
||||||
void *transformer (void *) {
|
void *transformer (void *) {
|
||||||
block *x, *y;
|
block *x, *y;
|
||||||
@ -47,12 +46,10 @@ void *transformer (void *) {
|
|||||||
signal(mut_list2);
|
signal(mut_list2);
|
||||||
// signal new element on list2
|
// signal new element on list2
|
||||||
signal(sem_list2);
|
signal(sem_list2);
|
||||||
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void transform(block *x, block *y) {
|
void transform(block *x, block *y) {
|
||||||
usleep(timescale);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user