From a22970ff41ad3fd04ce810c03131798920229680 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 13 Oct 2022 23:10:33 -0500 Subject: [PATCH] Split program into three files, added arguments for fork amounts, and passes sharedID into forks for exec --- Makefile | 6 ++- include/consumer.h | 4 -- include/producer.h | 3 -- include/shared.h | 2 + src/consumer.c | 21 ++++++++++- src/driver.c | 92 ++++++++++++++++++++++++++++++++-------------- src/producer.c | 22 ++++++++++- src/shared.c | 10 ++++- 8 files changed, 121 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index bc2f0ed..fc7cb00 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,13 @@ compile: gcc $(INC) -c -o build/driver.o src/driver.c gcc $(INC) -c -o build/producer.o src/producer.c gcc $(INC) -c -o build/consumer.o src/consumer.c + gcc $(INC) -c -o build/shared.o src/shared.c link: - gcc -o bin/driver.out build/*.o + gcc -o bin/driver.out build/driver.o build/shared.o + gcc -o bin/producer.out build/producer.o build/shared.o + gcc -o bin/consumer.out build/consumer.o build/shared.o + exec: compile link ./bin/driver.out diff --git a/include/consumer.h b/include/consumer.h index e524d8b..7b64da4 100644 --- a/include/consumer.h +++ b/include/consumer.h @@ -1,8 +1,4 @@ #ifndef CONSUMER_H #define CONSUMER_H -void* Consumer(void* arg); -void consume_item(long int item); -long int remove_item(void); - #endif \ No newline at end of file diff --git a/include/producer.h b/include/producer.h index dbeb64e..43fb242 100644 --- a/include/producer.h +++ b/include/producer.h @@ -1,8 +1,5 @@ #ifndef PRODUCER_H #define PRODUCER_H -void* Producer(void* arg); -long int produce_item(void); -void insert_item(long int item); #endif \ No newline at end of file diff --git a/include/shared.h b/include/shared.h index 8dfc364..577028e 100644 --- a/include/shared.h +++ b/include/shared.h @@ -2,6 +2,8 @@ #define SHARED_H #include +int StringToNumber(char* argv); + typedef struct SharedStruct { int buffer[10]; diff --git a/src/consumer.c b/src/consumer.c index 8e17229..0c7c40d 100644 --- a/src/consumer.c +++ b/src/consumer.c @@ -1,6 +1,25 @@ -#include "consumer.h" +#include +#include +#include +#include #include "shared.h" +void* Consumer(void* arg); +void consume_item(long int item); +long int remove_item(void); + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: %s SharedID# \n", argv[0]); + exit(1); + } + printf("Consumer created.\n"); + int sharedID = StringToNumber(argv[1]); + return 0; +} + // Consumer main function // TODO: Add waiting for one consumer at a time // Add ability to use shared memory diff --git a/src/driver.c b/src/driver.c index 9148b3a..c5dfefe 100644 --- a/src/driver.c +++ b/src/driver.c @@ -1,32 +1,56 @@ -#include #include +#include #include -#include #include -#include "consumer.h" -#include "producer.h" #include "shared.h" -int GetSplitAmount(char* argv); -void TestProducerConsumer(void); +int CheckArgumentPosition(int argc, char* argv[], char letter); +void CreateChildForks(int forkAmount, int forkType, int sharedID); +int GetSplitAmount(int argc, char* argv[], int forkType); // 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() +// Add command line arguments for producer and consumer number int main(int argc, char* argv[]) { - srandom((unsigned int) time(NULL)); - int splitLimit = 1; - int forkID, sharedID; + int producerAmount = GetSplitAmount(argc, argv, 0); + int consumerAmount = GetSplitAmount(argc, argv, 1); + int sharedID; SharedStruct *sharedMem; - if (argc == 2) - splitLimit = GetSplitAmount(argv[1]); - sharedID = shmget(IPC_EXCL | (key_t)1122, sizeof(SharedStruct), IPC_CREAT | 666); + sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666); + if (sharedID < 0) + { + fprintf(stderr, "Shmget failed\n"); + exit(1); + } // Create child forks // TODO: Add creating consumers and producers instead of general forks - for (int i = 0; i < splitLimit; i++) + CreateChildForks(producerAmount, 0, sharedID); + CreateChildForks(consumerAmount, 1, sharedID); + return 0; +} + +int CheckArgumentPosition(int argc, char* argv[], char letter) +{ + char argumentHyphen = '-'; + char argumentLetter = letter; + for (int i = 1; i < argc; i++) + { + if ((*(argv[i]) == argumentHyphen) && (*(argv[i]+1) == argumentLetter)) + { + return i; + } + } + return -1; +} + +// Function creates an amount of children based on given type +// forkAmount: Amount of children to create +// forkType: 0 for creating producers, non-zero for consumers +// sharedID: ID number of shared memory +void CreateChildForks(int forkAmount, int forkType, int sharedID) +{ + int forkID; + for (int i = 0; i < forkAmount; i++) { forkID = fork(); if (forkID < 0) @@ -36,22 +60,34 @@ int main(int argc, char* argv[]) } if (forkID == 0) { - TestProducerConsumer(); break; } - printf("Fork number %d created successfully.\n", forkID); } + if (forkID != 0) + return; + // TODO: Pass through sharedID + char sharedIDStr[16]; + sprintf(sharedIDStr, "%d", sharedID); + if (forkType == 0) + execlp("./producer.out", "./producer.out", sharedIDStr, NULL); + if (forkType != 0) + execlp("./consumer.out", "./consumer.out", sharedIDStr, NULL); + exit(0); } -int GetSplitAmount(char* argv) +// Takes in argc, argv, and type of fork to check for +// forkType: 0 for creating producers, non-zero for consumers +int GetSplitAmount(int argc, char* argv[], int forkType) { - int splitAmount; - sscanf(argv, "%d", &splitAmount); - return splitAmount; -} + char letter; + int position; + if (forkType == 0) + letter = 'p'; + if (forkType != 0) + letter = 'c'; + position = CheckArgumentPosition(argc, argv, letter); + if (position == -1) + return 1; + return StringToNumber(argv[position+1]); -void TestProducerConsumer(void) -{ - Producer(NULL); - Consumer(NULL); } diff --git a/src/producer.c b/src/producer.c index 756b264..30b298d 100644 --- a/src/producer.c +++ b/src/producer.c @@ -1,8 +1,28 @@ #include #include -#include "producer.h" +#include +#include +#include #include "shared.h" +void* Producer(void* arg); +long int produce_item(void); +void insert_item(long int item); + + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: %s SharedID# \n", argv[0]); + exit(1); + } + srandom((unsigned int) time(NULL)); + printf("Producer created.\n"); + int sharedID = StringToNumber(argv[1]); + return 0; +} + // Producer main function // TODO: Add waiting for one producer at a time // Add ability to use shared memory diff --git a/src/shared.c b/src/shared.c index a036ba6..5463588 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1 +1,9 @@ -#include "shared.h" \ No newline at end of file +#include +#include "shared.h" + +int StringToNumber(char* argv) +{ + int splitAmount; + sscanf(argv, "%d", &splitAmount); + return splitAmount; +} \ No newline at end of file