Implement prerequisites
This commit is contained in:
		
							
								
								
									
										7
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								inc/consumer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								inc/consumer.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
 | 
			
		||||
void consumer ();
 | 
			
		||||
							
								
								
									
										7
									
								
								inc/globals.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								inc/globals.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
			
		||||
@@ -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);
 | 
			
		||||
void   list_print  (list *l);
 | 
			
		||||
void   list_concise(list *l);
 | 
			
		||||
							
								
								
									
										2
									
								
								inc/producer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								inc/producer.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
 | 
			
		||||
void producer ();
 | 
			
		||||
							
								
								
									
										2
									
								
								inc/transformer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								inc/transformer.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
 | 
			
		||||
void transformer ();
 | 
			
		||||
							
								
								
									
										21
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								main.cpp
									
									
									
									
									
								
							@@ -1,21 +0,0 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/consumer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/consumer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
}
 | 
			
		||||
@@ -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");
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								src/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/producer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/producer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										83
									
								
								src/test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#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;
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										11
									
								
								src/transformer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/transformer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								test.cpp
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								test.cpp
									
									
									
									
									
								
							@@ -1,38 +0,0 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user