Removed stalling in producers and added assertion that should halt failing producers as a test
This commit is contained in:
parent
581134a3a0
commit
6611a7bd28
@ -1,3 +1,4 @@
|
|||||||
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
@ -67,7 +68,7 @@ int GetNextUsedSlot(SharedStruct* sharedMem)
|
|||||||
// Weird semaphore math that cleans up the stuck forks
|
// Weird semaphore math that cleans up the stuck forks
|
||||||
void SemaphoreCleanup(SharedStruct* sharedMem)
|
void SemaphoreCleanup(SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
int semMath = 2*(sharedMem->consumerCount) + (sharedMem->producerCount) - 1;
|
int semMath = (sharedMem->consumerCount) + 9;
|
||||||
int semCheckEmpty;
|
int semCheckEmpty;
|
||||||
sem_getvalue(&sharedMem->empty, &semCheckEmpty);
|
sem_getvalue(&sharedMem->empty, &semCheckEmpty);
|
||||||
if (semCheckEmpty < semMath)
|
if (semCheckEmpty < semMath)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
@ -74,12 +75,7 @@ int produce_item(void)
|
|||||||
void insert_item(int item, SharedStruct* sharedMem)
|
void insert_item(int item, SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
int slot = GetNextFreeSlot(sharedMem);
|
int slot = GetNextFreeSlot(sharedMem);
|
||||||
while(slot < 0)
|
assert(slot > -1);
|
||||||
{
|
|
||||||
sem_post(&sharedMem->mutex);
|
|
||||||
sem_wait(&sharedMem->mutex);
|
|
||||||
slot = GetNextFreeSlot(sharedMem);
|
|
||||||
}
|
|
||||||
sharedMem->buffer[slot] = item;
|
sharedMem->buffer[slot] = item;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
@ -41,7 +41,7 @@ void SharedStructInit(SharedStruct* sharedMem, int producers, int consumers)
|
|||||||
sharedMem->producerCount = producers;
|
sharedMem->producerCount = producers;
|
||||||
sharedMem->consumerCount = consumers;
|
sharedMem->consumerCount = consumers;
|
||||||
sem_init(&sharedMem->mutex, 1, 1);
|
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);
|
sem_init(&sharedMem->full, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user