From 0395a88a63f62a00f5f755c29624f2fb82402a22 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 15 Oct 2022 16:06:55 -0500 Subject: [PATCH] Added initialization of SharedStruct --- include/shared.h | 1 + src/consumer.c | 29 +++++++++++++++++------------ src/driver.c | 6 +++--- src/producer.c | 11 +++++++---- src/shared.c | 11 +++++++++++ 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/include/shared.h b/include/shared.h index 1584077..5a038b7 100644 --- a/include/shared.h +++ b/include/shared.h @@ -12,5 +12,6 @@ typedef struct SharedStruct sem_t semProducer; } SharedStruct; +void SharedStructInit(SharedStruct* sharedMem); #endif \ No newline at end of file diff --git a/src/consumer.c b/src/consumer.c index 195c851..ef112e4 100644 --- a/src/consumer.c +++ b/src/consumer.c @@ -4,9 +4,9 @@ #include #include "shared.h" -void* Consumer(void* arg); -void consume_item(int item); -int remove_item(void); +void Consumer(SharedStruct* sharedMem); +void consume_item(int item, SharedStruct* sharedMem); +int remove_item(SharedStruct* sharedMem); int main(int argc, char* argv[]) { @@ -17,38 +17,43 @@ int main(int argc, char* argv[]) } int sharedID = StringToNumber(argv[1]); SharedStruct* sharedMem = shmat(sharedID, NULL, 0); - sem_wait(&sharedMem->semConsumer); + Consumer(sharedMem); // TODO: Fix buffer not printing correctly - printf("%d\n", sharedMem->buffer[0]); return 0; } // Consumer main function // TODO: Add waiting for one consumer at a time // Add ability to use shared memory -void* Consumer(void* arg) +void Consumer(SharedStruct* sharedMem) { int consumeNum; while (1) { + sem_wait(&sharedMem->semConsumer); // down(&full); // down(&mutex); - consumeNum = remove_item(); + consumeNum = remove_item(sharedMem); + printf("%d was consumed.\n", consumeNum); // up(&mutex); // up(&empty); - consume_item(consumeNum); + consume_item(consumeNum, sharedMem); + sem_post(&sharedMem->semProducer); break; } } // Manage item taken from shared memory -void consume_item(int item) +void consume_item(int item, SharedStruct* sharedMem) { - ; + sharedMem->count[item]++; + return; } // Take item out of shared memory -int remove_item(void) +int remove_item(SharedStruct* sharedMem) { - ; + int item = sharedMem->buffer[0]; + sharedMem->buffer[0] = -1; + return item; } \ No newline at end of file diff --git a/src/driver.c b/src/driver.c index fd00612..8331a70 100644 --- a/src/driver.c +++ b/src/driver.c @@ -14,14 +14,14 @@ int main(int argc, char* argv[]) { int producerAmount = GetSplitAmount(argc, argv, 0); int consumerAmount = GetSplitAmount(argc, argv, 1); - int sharedID; - SharedStruct* sharedMem; - sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666); + int sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666); if (sharedID < 0) { fprintf(stderr, "Shmget failed\n"); exit(1); } + SharedStruct* sharedMem = shmat(sharedID, NULL, 0); + SharedStructInit(sharedMem); // Create child forks // TODO: Add creating consumers and producers instead of general forks CreateChildForks(producerAmount, 0, sharedID); diff --git a/src/producer.c b/src/producer.c index 4561d12..333b7af 100644 --- a/src/producer.c +++ b/src/producer.c @@ -22,7 +22,7 @@ int main(int argc, char* argv[]) int sharedID = StringToNumber(argv[1]); SharedStruct* sharedMem = shmat(sharedID, NULL, 0); Producer(sharedMem); - sem_post(&sharedMem->semConsumer); + printf("Producer finished.\n"); return 0; } @@ -49,6 +49,7 @@ void Producer(SharedStruct* sharedMem) // TODO: For testing purposes, revert max to 100 for (int i = 0; i < 5; i++) { + sem_wait(&sharedMem->semProducer); insertNum = produce_item(); printf("%d is inserted.\n", insertNum); // sem_wait(&semProducer); @@ -56,6 +57,7 @@ void Producer(SharedStruct* sharedMem) // down(&mutex); // if (insert_item(insertNum, sharedMem) == 0) insert_item(insertNum, sharedMem); + sem_post(&sharedMem->semConsumer); // { // --i; // continue; @@ -75,9 +77,10 @@ int produce_item(void) // Insert a number into the shared memory int insert_item(int item, SharedStruct* sharedMem) { - int slot = GetNextFreeSlot(sharedMem); - if (slot == -1) - return 0; + // int slot = GetNextFreeSlot(sharedMem); + // while (slot < 0) + // slot = GetNextFreeSlot(sharedMem); + // sharedMem->buffer[slot] = item; sharedMem->buffer[0] = item; return 1; } \ No newline at end of file diff --git a/src/shared.c b/src/shared.c index 5463588..fa7a7f0 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1,6 +1,17 @@ #include #include "shared.h" +void SharedStructInit(SharedStruct* sharedMem) +{ + for (int i = 0; i < 10; i++) + { + sharedMem->buffer[i] = -1; + sharedMem->count[i] = 0; + } + sem_init(&sharedMem->semConsumer, 1, 1); + sem_init(&sharedMem->semProducer, 1, 1); +} + int StringToNumber(char* argv) { int splitAmount;