Implement prerequisites

This commit is contained in:
John 2022-04-04 13:39:30 -05:00
parent ea6dfb0dee
commit 31a7583074
14 changed files with 163 additions and 63 deletions

View File

@ -1,9 +1,10 @@
CC = g++ CC = g++
CFLAGS = CFLAGS = -Iinc -pthread -lrt
VPATH = src
OBJECTS := %.o list.o OBJECTS := %.o list.o producer.o transformer.o consumer.o
EXECUTE := main.out EXECUTE := main.out test.out
.PHONY: all clean .PHONY: all clean

2
inc/consumer.hpp Normal file
View File

@ -0,0 +1,2 @@
void consumer ();

7
inc/globals.hpp Normal file
View 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;

View File

@ -21,3 +21,4 @@ void list_link (list *l, block *b);
void list_init (list *l, block *m, int size); void list_init (list *l, block *m, int size);
// list_print: prints a list // 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
View File

@ -0,0 +1,2 @@
void producer ();

2
inc/transformer.hpp Normal file
View File

@ -0,0 +1,2 @@
void transformer ();

View File

@ -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
View 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);
}

View File

@ -73,3 +73,17 @@ void list_print(list *l) {
std::printf(" []\n"); 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
View 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
View 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
View 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
View 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);
}

View File

@ -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;
}