diff --git a/include/shared.h b/include/shared.h index 3b1a7d3..8dfc364 100644 --- a/include/shared.h +++ b/include/shared.h @@ -2,12 +2,12 @@ #define SHARED_H #include -typedef struct sharedMem +typedef struct SharedStruct { int buffer[10]; sem_t semConsumer; sem_t semProducer; -} sharedMem; +} SharedStruct; #endif \ No newline at end of file diff --git a/src/consumer.c b/src/consumer.c index 365b7b7..8e17229 100644 --- a/src/consumer.c +++ b/src/consumer.c @@ -1,6 +1,9 @@ #include "consumer.h" #include "shared.h" +// Consumer main function +// TODO: Add waiting for one consumer at a time +// Add ability to use shared memory void* Consumer(void* arg) { long int consumeNum; @@ -16,11 +19,13 @@ void* Consumer(void* arg) } } +// Manage item taken from shared memory void consume_item(long int item) { ; } +// Take item out of shared memory long int remove_item() { ; diff --git a/src/driver.c b/src/driver.c index 8c4392f..9148b3a 100644 --- a/src/driver.c +++ b/src/driver.c @@ -10,15 +10,37 @@ int GetSplitAmount(char* argv); void TestProducerConsumer(void); +// TODO: Clean up main +// Add ability to send different consumer and producer numbers +// Split producer and consumer into different programs +// Use exec() to run an instance of producer or consumer on programs +// Allow sharedID to be given to other programs through exec() int main(int argc, char* argv[]) { - srand(time(NULL)); - sharedMem testMemory; - + srandom((unsigned int) time(NULL)); int splitLimit = 1; + int forkID, sharedID; + SharedStruct *sharedMem; if (argc == 2) splitLimit = GetSplitAmount(argv[1]); - TestProducerConsumer(); + sharedID = shmget(IPC_EXCL | (key_t)1122, sizeof(SharedStruct), IPC_CREAT | 666); + // Create child forks + // TODO: Add creating consumers and producers instead of general forks + for (int i = 0; i < splitLimit; i++) + { + forkID = fork(); + if (forkID < 0) + { + fprintf(stderr, "fork failed\n"); + exit(1); + } + if (forkID == 0) + { + TestProducerConsumer(); + break; + } + printf("Fork number %d created successfully.\n", forkID); + } } int GetSplitAmount(char* argv) diff --git a/src/producer.c b/src/producer.c index 4b29cab..756b264 100644 --- a/src/producer.c +++ b/src/producer.c @@ -3,9 +3,13 @@ #include "producer.h" #include "shared.h" +// Producer main function +// TODO: Add waiting for one producer at a time +// Add ability to use shared memory void* Producer(void* arg) { long int insertNum; + SharedStruct* sharedMem; while (1) { insertNum = produce_item(); @@ -21,11 +25,13 @@ void* Producer(void* arg) } } +// Generate a number between 0 and 9 long int produce_item(void) { - return rand() % 10; + return random() % 10; } +// Insert a number into the shared memory void insert_item(long int item) { ;