From 31a75830744bd4d2af4500ce5e4f868acc54be02 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Mon, 4 Apr 2022 13:39:30 -0500 Subject: [PATCH] Implement prerequisites --- Makefile | 7 ++-- inc/consumer.hpp | 2 + inc/globals.hpp | 7 ++++ list.hpp => inc/list.hpp | 3 +- inc/producer.hpp | 2 + inc/transformer.hpp | 2 + main.cpp | 21 ---------- src/consumer.cpp | 9 +++++ list.cpp => src/list.cpp | 14 +++++++ src/main.cpp | 18 +++++++++ src/producer.cpp | 9 +++++ src/test.cpp | 83 ++++++++++++++++++++++++++++++++++++++++ src/transformer.cpp | 11 ++++++ test.cpp | 38 ------------------ 14 files changed, 163 insertions(+), 63 deletions(-) create mode 100644 inc/consumer.hpp create mode 100644 inc/globals.hpp rename list.hpp => inc/list.hpp (90%) create mode 100644 inc/producer.hpp create mode 100644 inc/transformer.hpp delete mode 100644 main.cpp create mode 100644 src/consumer.cpp rename list.cpp => src/list.cpp (86%) create mode 100644 src/main.cpp create mode 100644 src/producer.cpp create mode 100644 src/test.cpp create mode 100644 src/transformer.cpp delete mode 100644 test.cpp diff --git a/Makefile b/Makefile index 88ef99b..9697cba 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ CC = g++ -CFLAGS = +CFLAGS = -Iinc -pthread -lrt +VPATH = src -OBJECTS := %.o list.o -EXECUTE := main.out +OBJECTS := %.o list.o producer.o transformer.o consumer.o +EXECUTE := main.out test.out .PHONY: all clean diff --git a/inc/consumer.hpp b/inc/consumer.hpp new file mode 100644 index 0000000..99e2815 --- /dev/null +++ b/inc/consumer.hpp @@ -0,0 +1,2 @@ + +void consumer (); \ No newline at end of file diff --git a/inc/globals.hpp b/inc/globals.hpp new file mode 100644 index 0000000..3c04cd4 --- /dev/null +++ b/inc/globals.hpp @@ -0,0 +1,7 @@ +#define N 8 +// Shared memory through global variables +// Create all of memory +extern block memory[N]; +// create the three lists +extern list lists[3]; +extern list *freelist, *list1, *list2; \ No newline at end of file diff --git a/list.hpp b/inc/list.hpp similarity index 90% rename from list.hpp rename to inc/list.hpp index 4222b8a..9cbcd78 100644 --- a/list.hpp +++ b/inc/list.hpp @@ -20,4 +20,5 @@ void list_link (list *l, block *b); // list_init: links an array of blocks onto the end of a list void list_init (list *l, block *m, int size); // list_print: prints a list -void list_print (list *l); \ No newline at end of file +void list_print (list *l); +void list_concise(list *l); \ No newline at end of file diff --git a/inc/producer.hpp b/inc/producer.hpp new file mode 100644 index 0000000..6198cf5 --- /dev/null +++ b/inc/producer.hpp @@ -0,0 +1,2 @@ + +void producer (); \ No newline at end of file diff --git a/inc/transformer.hpp b/inc/transformer.hpp new file mode 100644 index 0000000..15a543e --- /dev/null +++ b/inc/transformer.hpp @@ -0,0 +1,2 @@ + +void transformer (); \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 983f734..0000000 --- a/main.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include "list.hpp" - -#define N 0x8 - -// Create all of memory -block memory[N] = {0}; -// create the three lists -list freelist = {0}, list1 = {0}, list2 = {0}; - -int main (int argc, char* argv[]) { - // initialize the freelist - list_init(&freelist, memory, N); - // print the freelist - list_print(&freelist); - //TODO: Create producer, transformer, and consumer - //TODO: Create POSIX shared memory, and put memory, freelist, list1, list2 in it - //TODO: Use pthreads to split execution - //TODO: Implement a semaphore solution to the problem -} diff --git a/src/consumer.cpp b/src/consumer.cpp new file mode 100644 index 0000000..30254d4 --- /dev/null +++ b/src/consumer.cpp @@ -0,0 +1,9 @@ +#include "list.hpp" +#include "globals.hpp" +#include "consumer.hpp" + +void consumer () { + block* c = list_unlink(list2); + //TODO: consume information in block c + list_link(freelist, c); +} \ No newline at end of file diff --git a/list.cpp b/src/list.cpp similarity index 86% rename from list.cpp rename to src/list.cpp index 668a364..a1a2b49 100644 --- a/list.cpp +++ b/src/list.cpp @@ -72,4 +72,18 @@ void list_print(list *l) { } else { std::printf(" []\n"); } +} + +void list_concise (list *l) { + if (!l) return; + std::printf("%lx: {", (__uint64_t)l&0xffff); + block *b = l->start; + if (b) { + do { + std::printf("%lx:%d, ", (__uint64_t)b&0xfff, b->data); + } while (b = b->next); + std::printf("\b\b} \n"); + } else { + std::printf("}\n"); + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8fa432b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,18 @@ +#include +#include +#include "list.hpp" +#include "globals.hpp" +#include "producer.hpp" + +// Create all of memory +block memory[N] = {0}; +// create the three lists +list lists[3] = {0}; +list *freelist = &lists[0], *list1 = &lists[1], *list2 = &lists[2]; + +int main (int argc, char* argv[]) { + // initialize the freelist + list_init(freelist, memory, N); + //TODO: Use pthreads to split execution + //TODO: Implement a semaphore solution to the problem +} diff --git a/src/producer.cpp b/src/producer.cpp new file mode 100644 index 0000000..5e83cfe --- /dev/null +++ b/src/producer.cpp @@ -0,0 +1,9 @@ +#include "list.hpp" +#include "globals.hpp" +#include "producer.hpp" + +void producer () { + block* b = list_unlink(freelist); + //TODO: Produce information in block b + list_link(list1, b); +} \ No newline at end of file diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..93696cc --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,83 @@ +#include +#include +#include "list.hpp" +#include "globals.hpp" +#include "producer.hpp" +#include "consumer.hpp" +#include "transformer.hpp" + +// Create all of memory +block memory[N] = {0}; +// create the three lists +list lists[3] = {0}; +list *freelist = &lists[0], + *list1 = &lists[1], + *list2 = &lists[2]; + +void producer_test(); +void transfer_test(); +void consumer_test(); + +int main(int argc, char *argv[]) { + list_init(freelist, memory, N); + list_concise(freelist); + producer_test(); + transfer_test(); + consumer_test(); +} + +void producer_test() { + printf("Starting producer_test:\n");\ + producer(); + list_concise(freelist); + list_concise(list1); + list_concise(list2); + printf("Ending producer_test\n"); +} + +void transfer_test() { + printf("Starting transfer_test:\n"); + transformer(); + list_concise(freelist); + list_concise(list1); + list_concise(list2); + printf("Ending transfer_test\n"); +} + +void consumer_test() { + printf("Starting consumer_test:\n"); + consumer(); + list_concise(freelist); + list_concise(list1); + list_concise(list2); + printf("Ending consumer_test\n"); +} + +/* +int movement_test () { + + // Create all of memory + block memory[N] = {0}; + // create the three lists + list freelist = {0}, list1 = {0}; + // initialize the freelist + list_init(&freelist, memory, N); + + // print the lists + std::printf("Lists:\n"); + list_print(&freelist); + list_print(&list1); + + // move a block + block *b; + while (b = list_unlink(&freelist)) + { + list_link(&list1, b); + } + // print the lists again (should be reversed order) + std::printf("Lists, again:\n"); + list_print(&freelist); + list_print(&list1); + return 1; +} +*/ \ No newline at end of file diff --git a/src/transformer.cpp b/src/transformer.cpp new file mode 100644 index 0000000..012239b --- /dev/null +++ b/src/transformer.cpp @@ -0,0 +1,11 @@ +#include "list.hpp" +#include "globals.hpp" +#include "transformer.hpp" + +void transformer () { + block* x = list_unlink(list1); + block* y = list_unlink(freelist); + //TODO: use block x to produce info in y + list_link(freelist, x); + list_link(list2, y); +} \ No newline at end of file diff --git a/test.cpp b/test.cpp deleted file mode 100644 index 917695e..0000000 --- a/test.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include "list.hpp" - -#define N 0x8 - -int movement_test (); - -int main(int argc, char *argv[]) { - movement_test(); -} - -int movement_test () { - - // Create all of memory - block memory[N] = {0}; - // create the three lists - list freelist = {0}, list1 = {0}; - // initialize the freelist - list_init(&freelist, memory, N); - - // print the lists - std::printf("Lists:\n"); - list_print(&freelist); - list_print(&list1); - - // move a block - block *b; - while (b = list_unlink(&freelist)) - { - list_link(&list1, b); - } - // print the lists again (should be reversed order) - std::printf("Lists, again:\n"); - list_print(&freelist); - list_print(&list1); - return 1; -}