Program is now fully functional
This commit is contained in:
parent
cbd8b5ceb0
commit
8540de1cd9
@ -8,11 +8,16 @@ typedef struct SharedStruct
|
|||||||
{
|
{
|
||||||
int buffer[10];
|
int buffer[10];
|
||||||
int count[10];
|
int count[10];
|
||||||
|
int producerCount;
|
||||||
|
int consumerCount;
|
||||||
sem_t mutex;
|
sem_t mutex;
|
||||||
sem_t empty;
|
sem_t empty;
|
||||||
sem_t full;
|
sem_t full;
|
||||||
} SharedStruct;
|
} SharedStruct;
|
||||||
|
|
||||||
void SharedStructInit(SharedStruct* sharedMem);
|
int IsProductionFinished(SharedStruct* sharedMem);
|
||||||
|
void SharedStructInit(SharedStruct* sharedMem, int producers, int consumers);
|
||||||
|
void PrintBuffer(SharedStruct* sharedMem);
|
||||||
|
void PrintNumberCount(SharedStruct* sharedMem);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
void StartConsumer(SharedStruct* sharedMem);
|
void StartConsumer(SharedStruct* sharedMem);
|
||||||
void Consume(SharedStruct* sharedMem);
|
void Consume(SharedStruct* sharedMem);
|
||||||
|
int GetNextUsedSlot(SharedStruct* sharedMem);
|
||||||
|
void SemaphoreCleanup(SharedStruct* sharedMem);
|
||||||
void consume_item(int item, SharedStruct* sharedMem);
|
void consume_item(int item, SharedStruct* sharedMem);
|
||||||
int remove_item(SharedStruct* sharedMem);
|
int remove_item(SharedStruct* sharedMem);
|
||||||
|
|
||||||
@ -20,18 +22,20 @@ int main(int argc, char* argv[])
|
|||||||
int sharedID = StringToNumber(argv[1]);
|
int sharedID = StringToNumber(argv[1]);
|
||||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||||
StartConsumer(sharedMem);
|
StartConsumer(sharedMem);
|
||||||
|
PrintNumberCount(sharedMem);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts the consumer process.
|
// Starts the consumer process.
|
||||||
void StartConsumer(SharedStruct* sharedMem)
|
void StartConsumer(SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 5; i++)
|
while (!IsProductionFinished(sharedMem))
|
||||||
{
|
{
|
||||||
sem_wait(&sharedMem->full);
|
sem_wait(&sharedMem->full);
|
||||||
Consume(sharedMem);
|
Consume(sharedMem);
|
||||||
sem_post(&sharedMem->empty);
|
sem_post(&sharedMem->empty);
|
||||||
}
|
}
|
||||||
|
SemaphoreCleanup(sharedMem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a number from the shared memory and consumes it.
|
// Takes a number from the shared memory and consumes it.
|
||||||
@ -40,12 +44,52 @@ void Consume(SharedStruct* sharedMem)
|
|||||||
int consumeNum;
|
int consumeNum;
|
||||||
sem_wait(&sharedMem->mutex);
|
sem_wait(&sharedMem->mutex);
|
||||||
consumeNum = remove_item(sharedMem);
|
consumeNum = remove_item(sharedMem);
|
||||||
consume_item(consumeNum, sharedMem);
|
if (consumeNum >= 0)
|
||||||
printf("%d was consumed.\n", consumeNum);
|
consume_item(consumeNum, sharedMem);
|
||||||
sem_post(&sharedMem->mutex);
|
sem_post(&sharedMem->mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetNextUsedSlot(SharedStruct* sharedMem)
|
||||||
|
{
|
||||||
|
int slot = -1;
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if (sharedMem->buffer[i] > -1)
|
||||||
|
{
|
||||||
|
slot = i;
|
||||||
|
return slot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slot;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weird semaphore math that cleans up the stuck forks
|
||||||
|
void SemaphoreCleanup(SharedStruct* sharedMem)
|
||||||
|
{
|
||||||
|
int semMath = 2*(sharedMem->consumerCount) + (sharedMem->producerCount) - 1;
|
||||||
|
// int semCheckMutex;
|
||||||
|
int semCheckEmpty;
|
||||||
|
// int semCheckFull;
|
||||||
|
// sem_getvalue(&sharedMem->mutex, &semCheckMutex);
|
||||||
|
sem_getvalue(&sharedMem->empty, &semCheckEmpty);
|
||||||
|
// sem_getvalue(&sharedMem->full, &semCheckFull);
|
||||||
|
// if (semCheckMutex > 0)
|
||||||
|
// sem_post(&sharedMem->mutex);
|
||||||
|
if (semCheckEmpty < semMath)
|
||||||
|
{
|
||||||
|
sem_post(&sharedMem->full);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
// sem_post(&sharedMem->empty);
|
||||||
|
// if (semCheckFull > 0)
|
||||||
|
// sem_post(&sharedMem->full);
|
||||||
|
// printf("Value of semCheckMutex: %d\n", semCheckMutex);
|
||||||
|
// printf("Value of semCheckEmpty: %d\n", semCheckEmpty);
|
||||||
|
// printf("Value of semCheckFull: %d\n", semCheckFull);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Increments the count of numbers generated so far in shared memory.
|
// Increments the count of numbers generated so far in shared memory.
|
||||||
void consume_item(int item, SharedStruct* sharedMem)
|
void consume_item(int item, SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
@ -56,7 +100,10 @@ void consume_item(int item, SharedStruct* sharedMem)
|
|||||||
// Take item out of shared memory.
|
// Take item out of shared memory.
|
||||||
int remove_item(SharedStruct* sharedMem)
|
int remove_item(SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
int item = sharedMem->buffer[0];
|
int slot = GetNextUsedSlot(sharedMem);
|
||||||
sharedMem->buffer[0] = -1;
|
if (slot < 0)
|
||||||
|
return -1;
|
||||||
|
int item = sharedMem->buffer[slot];
|
||||||
|
sharedMem->buffer[slot] = -1;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
@ -21,7 +21,7 @@ int main(int argc, char* argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||||
SharedStructInit(sharedMem);
|
SharedStructInit(sharedMem, producerAmount, consumerAmount);
|
||||||
CreateChildForks(producerAmount, 0, sharedID);
|
CreateChildForks(producerAmount, 0, sharedID);
|
||||||
CreateChildForks(consumerAmount, 1, sharedID);
|
CreateChildForks(consumerAmount, 1, sharedID);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -9,7 +9,7 @@ int GetNextFreeSlot(SharedStruct* sharedMem);
|
|||||||
void StartProducer(SharedStruct* sharedMem);
|
void StartProducer(SharedStruct* sharedMem);
|
||||||
void Produce(SharedStruct* sharedMem);
|
void Produce(SharedStruct* sharedMem);
|
||||||
int produce_item(void);
|
int produce_item(void);
|
||||||
int insert_item(int item, SharedStruct* sharedMem);
|
void insert_item(int item, SharedStruct* sharedMem);
|
||||||
|
|
||||||
// Checks for SharedID, then begins producing numbers into shared memory.
|
// Checks for SharedID, then begins producing numbers into shared memory.
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
@ -31,12 +31,15 @@ int main(int argc, char* argv[])
|
|||||||
int GetNextFreeSlot(SharedStruct* sharedMem)
|
int GetNextFreeSlot(SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
int slot = -1;
|
int slot = -1;
|
||||||
for (int i = 0; i < 10; i++)
|
while(1)
|
||||||
{
|
{
|
||||||
if ((sharedMem->buffer[i] != -1) && (sharedMem->buffer[(i+1)%10] == -1))
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
slot = (i+1)%10;
|
if (sharedMem->buffer[i] == -1)
|
||||||
return slot;
|
{
|
||||||
|
slot = i;
|
||||||
|
return slot;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return slot;
|
return slot;
|
||||||
@ -45,8 +48,7 @@ int GetNextFreeSlot(SharedStruct* sharedMem)
|
|||||||
// Starts the producer process.
|
// Starts the producer process.
|
||||||
void StartProducer(SharedStruct* sharedMem)
|
void StartProducer(SharedStruct* sharedMem)
|
||||||
{
|
{
|
||||||
// TODO: For testing purposes, revert max to 100
|
for (int i = 0; i < 100; i++)
|
||||||
for (int i = 0; i < 5; i++)
|
|
||||||
{
|
{
|
||||||
sem_wait(&sharedMem->empty);
|
sem_wait(&sharedMem->empty);
|
||||||
Produce(sharedMem);
|
Produce(sharedMem);
|
||||||
@ -61,7 +63,6 @@ void Produce(SharedStruct* sharedMem)
|
|||||||
sem_wait(&sharedMem->mutex);
|
sem_wait(&sharedMem->mutex);
|
||||||
insertNum = produce_item();
|
insertNum = produce_item();
|
||||||
insert_item(insertNum, sharedMem);
|
insert_item(insertNum, sharedMem);
|
||||||
printf("%d is inserted.\n", insertNum);
|
|
||||||
sem_post(&sharedMem->mutex);
|
sem_post(&sharedMem->mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -73,9 +74,7 @@ int produce_item(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert a number into the shared memory.
|
// Insert a number into the shared memory.
|
||||||
// TODO: Reimplement insertion using the next slot from GetNextFreeSlot().
|
void insert_item(int item, SharedStruct* sharedMem)
|
||||||
int insert_item(int item, SharedStruct* sharedMem)
|
|
||||||
{
|
{
|
||||||
sharedMem->buffer[0] = item;
|
sharedMem->buffer[GetNextFreeSlot(sharedMem)] = item;
|
||||||
return 1;
|
|
||||||
}
|
}
|
37
src/shared.c
37
src/shared.c
@ -1,17 +1,48 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
|
int IsProductionFinished(SharedStruct* sharedMem)
|
||||||
|
{
|
||||||
|
int producedTotal = 0;
|
||||||
|
int potentitalTotal = (sharedMem->producerCount)*100;
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
producedTotal += sharedMem->count[i];
|
||||||
|
return (producedTotal == potentitalTotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintBuffer(SharedStruct* sharedMem)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
printf("Buffer slot %d: %d\n", i, sharedMem->buffer[i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintNumberCount(SharedStruct* sharedMem)
|
||||||
|
{
|
||||||
|
int consumedTotal = 0;
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
consumedTotal += sharedMem->count[i];
|
||||||
|
printf("Number %d: %d time(s)\n", i, sharedMem->count[i]);
|
||||||
|
}
|
||||||
|
printf("Total produced: %d\n", (sharedMem->producerCount)*100);
|
||||||
|
printf("Total consumed: %d\n", consumedTotal);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Initializes the struct in shared memory
|
// Initializes the struct in shared memory
|
||||||
void SharedStructInit(SharedStruct* sharedMem)
|
void SharedStructInit(SharedStruct* sharedMem, int producers, int consumers)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
sharedMem->buffer[i] = -1;
|
sharedMem->buffer[i] = -1;
|
||||||
sharedMem->count[i] = 0;
|
sharedMem->count[i] = 0;
|
||||||
}
|
}
|
||||||
|
sharedMem->producerCount = producers;
|
||||||
|
sharedMem->consumerCount = consumers;
|
||||||
sem_init(&sharedMem->mutex, 1, 1);
|
sem_init(&sharedMem->mutex, 1, 1);
|
||||||
sem_init(&sharedMem->empty, 1, 1);
|
sem_init(&sharedMem->empty, 1, producers+consumers);
|
||||||
sem_init(&sharedMem->full, 1, 1);
|
sem_init(&sharedMem->full, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes array of letters and turns it into a number
|
// Takes array of letters and turns it into a number
|
||||||
|
Loading…
Reference in New Issue
Block a user