From 6611a7bd287cf62c511a9d25c74cdd9207d22753 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Wed, 26 Oct 2022 18:24:04 -0500 Subject: [PATCH] Removed stalling in producers and added assertion that should halt failing producers as a test --- src/consumer.c | 5 +++-- src/producer.c | 8 ++------ src/shared.c | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/consumer.c b/src/consumer.c index 4a8fb45..915d100 100644 --- a/src/consumer.c +++ b/src/consumer.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -67,7 +68,7 @@ int GetNextUsedSlot(SharedStruct* sharedMem) // Weird semaphore math that cleans up the stuck forks void SemaphoreCleanup(SharedStruct* sharedMem) { - int semMath = 2*(sharedMem->consumerCount) + (sharedMem->producerCount) - 1; + int semMath = (sharedMem->consumerCount) + 9; int semCheckEmpty; sem_getvalue(&sharedMem->empty, &semCheckEmpty); if (semCheckEmpty < semMath) @@ -94,4 +95,4 @@ int remove_item(SharedStruct* sharedMem) int item = sharedMem->buffer[slot]; sharedMem->buffer[slot] = -1; return item; -} \ No newline at end of file +} diff --git a/src/producer.c b/src/producer.c index ee24dba..7b0b3e1 100644 --- a/src/producer.c +++ b/src/producer.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -74,12 +75,7 @@ int produce_item(void) void insert_item(int item, SharedStruct* sharedMem) { int slot = GetNextFreeSlot(sharedMem); - while(slot < 0) - { - sem_post(&sharedMem->mutex); - sem_wait(&sharedMem->mutex); - slot = GetNextFreeSlot(sharedMem); - } + assert(slot > -1); sharedMem->buffer[slot] = item; return; } \ No newline at end of file diff --git a/src/shared.c b/src/shared.c index 4f85e03..245a1c9 100644 --- a/src/shared.c +++ b/src/shared.c @@ -41,7 +41,7 @@ void SharedStructInit(SharedStruct* sharedMem, int producers, int consumers) sharedMem->producerCount = producers; sharedMem->consumerCount = consumers; sem_init(&sharedMem->mutex, 1, 1); - sem_init(&sharedMem->empty, 1, producers+consumers); + sem_init(&sharedMem->empty, 1, 10); sem_init(&sharedMem->full, 1, 0); }