Adjusted SharedStruct and reverted semaphores.

This commit is contained in:
TriantaTV 2022-10-15 20:18:06 -05:00
parent 0395a88a63
commit 898851a185
4 changed files with 47 additions and 43 deletions

View File

@ -8,8 +8,9 @@ typedef struct SharedStruct
{
int buffer[10];
int count[10];
sem_t semConsumer;
sem_t semProducer;
sem_t mutex;
sem_t empty;
sem_t full;
} SharedStruct;
void SharedStructInit(SharedStruct* sharedMem);

View File

@ -4,7 +4,8 @@
#include <unistd.h>
#include "shared.h"
void Consumer(SharedStruct* sharedMem);
void StartConsumer(SharedStruct* sharedMem);
void Consume(SharedStruct* sharedMem);
void consume_item(int item, SharedStruct* sharedMem);
int remove_item(SharedStruct* sharedMem);
@ -17,30 +18,32 @@ int main(int argc, char* argv[])
}
int sharedID = StringToNumber(argv[1]);
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
Consumer(sharedMem);
// TODO: Fix buffer not printing correctly
StartConsumer(sharedMem);
return 0;
}
// Consumer main function
// TODO: Add waiting for one consumer at a time
// Add ability to use shared memory
void Consumer(SharedStruct* sharedMem)
void StartConsumer(SharedStruct* sharedMem)
{
for (int i = 0; i < 5; i++)
{
sem_wait(&sharedMem->full);
Consume(sharedMem);
sem_post(&sharedMem->empty);
}
}
void Consume(SharedStruct* sharedMem)
{
int consumeNum;
while (1)
{
sem_wait(&sharedMem->semConsumer);
// down(&full);
// down(&mutex);
consumeNum = remove_item(sharedMem);
printf("%d was consumed.\n", consumeNum);
// up(&mutex);
// up(&empty);
consume_item(consumeNum, sharedMem);
sem_post(&sharedMem->semProducer);
break;
}
sem_wait(&sharedMem->mutex);
consumeNum = remove_item(sharedMem);
consume_item(consumeNum, sharedMem);
printf("%d was consumed.\n", consumeNum);
sem_post(&sharedMem->mutex);
return;
}
// Manage item taken from shared memory

View File

@ -6,7 +6,8 @@
#include "shared.h"
int GetNextFreeSlot(SharedStruct* sharedMem);
void Producer(SharedStruct* sharedMem);
void StartProducer(SharedStruct* sharedMem);
void Produce(SharedStruct* sharedMem);
int produce_item(void);
int insert_item(int item, SharedStruct* sharedMem);
@ -21,8 +22,7 @@ int main(int argc, char* argv[])
srandom((unsigned int) time(NULL));
int sharedID = StringToNumber(argv[1]);
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
Producer(sharedMem);
printf("Producer finished.\n");
StartProducer(sharedMem);
return 0;
}
@ -43,31 +43,28 @@ int GetNextFreeSlot(SharedStruct* sharedMem)
// Producer main function
// TODO: Add waiting for one producer at a time
// Add ability to use shared memory
void Producer(SharedStruct* sharedMem)
void StartProducer(SharedStruct* sharedMem)
{
int insertNum;
// 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);
// down(&empty);
// down(&mutex);
// if (insert_item(insertNum, sharedMem) == 0)
insert_item(insertNum, sharedMem);
sem_post(&sharedMem->semConsumer);
// {
// --i;
// continue;
// }
// up(&mutex);
// up(&full);
break;
sem_wait(&sharedMem->empty);
Produce(sharedMem);
sem_post(&sharedMem->full);
}
}
void Produce(SharedStruct* sharedMem)
{
int insertNum;
sem_wait(&sharedMem->mutex);
insertNum = produce_item();
insert_item(insertNum, sharedMem);
printf("%d is inserted.\n", insertNum);
sem_post(&sharedMem->mutex);
return;
}
// Generate a number between 0 and 9
int produce_item(void)
{

View File

@ -8,8 +8,11 @@ void SharedStructInit(SharedStruct* sharedMem)
sharedMem->buffer[i] = -1;
sharedMem->count[i] = 0;
}
sem_init(&sharedMem->semConsumer, 1, 1);
sem_init(&sharedMem->semProducer, 1, 1);
sem_init(&sharedMem->mutex, 1, 1);
sem_init(&sharedMem->empty, 1, 1);
sem_init(&sharedMem->full, 1, 1);
// sem_init(&sharedMem->semConsumer, 1, 1);
// sem_init(&sharedMem->semProducer, 1, 1);
}
int StringToNumber(char* argv)
@ -17,4 +20,4 @@ int StringToNumber(char* argv)
int splitAmount;
sscanf(argv, "%d", &splitAmount);
return splitAmount;
}
}