Removed stalling in producers and added assertion that should halt failing producers as a test

This commit is contained in:
TriantaTV 2022-10-26 18:24:04 -05:00
parent 581134a3a0
commit 6611a7bd28
3 changed files with 6 additions and 9 deletions

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
@ -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;
}
}

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
@ -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;
}

View File

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