Added initialization of SharedStruct
This commit is contained in:
parent
26011d7d74
commit
0395a88a63
@ -12,5 +12,6 @@ typedef struct SharedStruct
|
||||
sem_t semProducer;
|
||||
} SharedStruct;
|
||||
|
||||
void SharedStructInit(SharedStruct* sharedMem);
|
||||
|
||||
#endif
|
@ -4,9 +4,9 @@
|
||||
#include <unistd.h>
|
||||
#include "shared.h"
|
||||
|
||||
void* Consumer(void* arg);
|
||||
void consume_item(int item);
|
||||
int remove_item(void);
|
||||
void Consumer(SharedStruct* sharedMem);
|
||||
void consume_item(int item, SharedStruct* sharedMem);
|
||||
int remove_item(SharedStruct* sharedMem);
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@ -17,38 +17,43 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
int sharedID = StringToNumber(argv[1]);
|
||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||
sem_wait(&sharedMem->semConsumer);
|
||||
Consumer(sharedMem);
|
||||
// TODO: Fix buffer not printing correctly
|
||||
printf("%d\n", sharedMem->buffer[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Consumer main function
|
||||
// TODO: Add waiting for one consumer at a time
|
||||
// Add ability to use shared memory
|
||||
void* Consumer(void* arg)
|
||||
void Consumer(SharedStruct* sharedMem)
|
||||
{
|
||||
int consumeNum;
|
||||
while (1)
|
||||
{
|
||||
sem_wait(&sharedMem->semConsumer);
|
||||
// down(&full);
|
||||
// down(&mutex);
|
||||
consumeNum = remove_item();
|
||||
consumeNum = remove_item(sharedMem);
|
||||
printf("%d was consumed.\n", consumeNum);
|
||||
// up(&mutex);
|
||||
// up(&empty);
|
||||
consume_item(consumeNum);
|
||||
consume_item(consumeNum, sharedMem);
|
||||
sem_post(&sharedMem->semProducer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Manage item taken from shared memory
|
||||
void consume_item(int item)
|
||||
void consume_item(int item, SharedStruct* sharedMem)
|
||||
{
|
||||
;
|
||||
sharedMem->count[item]++;
|
||||
return;
|
||||
}
|
||||
|
||||
// Take item out of shared memory
|
||||
int remove_item(void)
|
||||
int remove_item(SharedStruct* sharedMem)
|
||||
{
|
||||
;
|
||||
int item = sharedMem->buffer[0];
|
||||
sharedMem->buffer[0] = -1;
|
||||
return item;
|
||||
}
|
@ -14,14 +14,14 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
int producerAmount = GetSplitAmount(argc, argv, 0);
|
||||
int consumerAmount = GetSplitAmount(argc, argv, 1);
|
||||
int sharedID;
|
||||
SharedStruct* sharedMem;
|
||||
sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666);
|
||||
int sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666);
|
||||
if (sharedID < 0)
|
||||
{
|
||||
fprintf(stderr, "Shmget failed\n");
|
||||
exit(1);
|
||||
}
|
||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||
SharedStructInit(sharedMem);
|
||||
// Create child forks
|
||||
// TODO: Add creating consumers and producers instead of general forks
|
||||
CreateChildForks(producerAmount, 0, sharedID);
|
||||
|
@ -22,7 +22,7 @@ int main(int argc, char* argv[])
|
||||
int sharedID = StringToNumber(argv[1]);
|
||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||
Producer(sharedMem);
|
||||
sem_post(&sharedMem->semConsumer);
|
||||
printf("Producer finished.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -49,6 +49,7 @@ void Producer(SharedStruct* sharedMem)
|
||||
// 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);
|
||||
@ -56,6 +57,7 @@ void Producer(SharedStruct* sharedMem)
|
||||
// down(&mutex);
|
||||
// if (insert_item(insertNum, sharedMem) == 0)
|
||||
insert_item(insertNum, sharedMem);
|
||||
sem_post(&sharedMem->semConsumer);
|
||||
// {
|
||||
// --i;
|
||||
// continue;
|
||||
@ -75,9 +77,10 @@ int produce_item(void)
|
||||
// Insert a number into the shared memory
|
||||
int insert_item(int item, SharedStruct* sharedMem)
|
||||
{
|
||||
int slot = GetNextFreeSlot(sharedMem);
|
||||
if (slot == -1)
|
||||
return 0;
|
||||
// int slot = GetNextFreeSlot(sharedMem);
|
||||
// while (slot < 0)
|
||||
// slot = GetNextFreeSlot(sharedMem);
|
||||
// sharedMem->buffer[slot] = item;
|
||||
sharedMem->buffer[0] = item;
|
||||
return 1;
|
||||
}
|
11
src/shared.c
11
src/shared.c
@ -1,6 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include "shared.h"
|
||||
|
||||
void SharedStructInit(SharedStruct* sharedMem)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
sharedMem->buffer[i] = -1;
|
||||
sharedMem->count[i] = 0;
|
||||
}
|
||||
sem_init(&sharedMem->semConsumer, 1, 1);
|
||||
sem_init(&sharedMem->semProducer, 1, 1);
|
||||
}
|
||||
|
||||
int StringToNumber(char* argv)
|
||||
{
|
||||
int splitAmount;
|
||||
|
Loading…
Reference in New Issue
Block a user