From cbd8b5ceb061ffc85fc785ac45fc65ca429d2cf1 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 15 Oct 2022 20:29:52 -0500 Subject: [PATCH] Added comments for readability --- src/consumer.c | 10 +++++----- src/driver.c | 22 +++++++++------------- src/producer.c | 18 ++++++++---------- src/shared.c | 4 ++-- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/consumer.c b/src/consumer.c index a75dfd0..a73365e 100644 --- a/src/consumer.c +++ b/src/consumer.c @@ -9,6 +9,7 @@ void Consume(SharedStruct* sharedMem); void consume_item(int item, SharedStruct* sharedMem); int remove_item(SharedStruct* sharedMem); +// Checks for SharedID, then begins producing numbers into shared memory. int main(int argc, char* argv[]) { if (argc != 2) @@ -22,9 +23,7 @@ int main(int argc, char* argv[]) return 0; } -// Consumer main function -// TODO: Add waiting for one consumer at a time -// Add ability to use shared memory +// Starts the consumer process. void StartConsumer(SharedStruct* sharedMem) { for (int i = 0; i < 5; i++) @@ -35,6 +34,7 @@ void StartConsumer(SharedStruct* sharedMem) } } +// Takes a number from the shared memory and consumes it. void Consume(SharedStruct* sharedMem) { int consumeNum; @@ -46,14 +46,14 @@ void Consume(SharedStruct* sharedMem) return; } -// Manage item taken from shared memory +// Increments the count of numbers generated so far in shared memory. void consume_item(int item, SharedStruct* sharedMem) { sharedMem->count[item]++; return; } -// Take item out of shared memory +// Take item out of shared memory. int remove_item(SharedStruct* sharedMem) { int item = sharedMem->buffer[0]; diff --git a/src/driver.c b/src/driver.c index 8331a70..d9326ef 100644 --- a/src/driver.c +++ b/src/driver.c @@ -8,8 +8,8 @@ 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 command line arguments for producer and consumer number +// Checks for producer and consumer arguments, creates shared memory, +// then creates forks sending the sharedID as an argument. int main(int argc, char* argv[]) { int producerAmount = GetSplitAmount(argc, argv, 0); @@ -22,13 +22,12 @@ int main(int argc, char* argv[]) } 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); CreateChildForks(consumerAmount, 1, sharedID); return 0; } +// Checks for argument based on passed in letter. int CheckArgumentPosition(int argc, char* argv[], char letter) { char argumentHyphen = '-'; @@ -43,10 +42,10 @@ int CheckArgumentPosition(int argc, char* argv[], char letter) 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 +// 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; @@ -59,13 +58,10 @@ void CreateChildForks(int forkAmount, int forkType, int sharedID) exit(1); } if (forkID == 0) - { break; - } } if (forkID != 0) return; - // TODO: Pass through sharedID char sharedIDStr[16]; sprintf(sharedIDStr, "%d", sharedID); if (forkType == 0) @@ -75,8 +71,8 @@ void CreateChildForks(int forkAmount, int forkType, int sharedID) exit(0); } -// Takes in argc, argv, and type of fork to check for -// forkType: 0 for creating producers, non-zero for consumers +// 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) { char letter; diff --git a/src/producer.c b/src/producer.c index 78acac0..44711ec 100644 --- a/src/producer.c +++ b/src/producer.c @@ -11,7 +11,7 @@ void Produce(SharedStruct* sharedMem); int produce_item(void); int insert_item(int item, SharedStruct* sharedMem); - +// Checks for SharedID, then begins producing numbers into shared memory. int main(int argc, char* argv[]) { if (argc != 2) @@ -26,6 +26,8 @@ int main(int argc, char* argv[]) return 0; } +// Checks for the next available slot in buffer and returns slot number. +// TODO: Fix function to get next free slot and return slot number. int GetNextFreeSlot(SharedStruct* sharedMem) { int slot = -1; @@ -40,9 +42,7 @@ int GetNextFreeSlot(SharedStruct* sharedMem) return slot; } -// Producer main function -// TODO: Add waiting for one producer at a time -// Add ability to use shared memory +// Starts the producer process. void StartProducer(SharedStruct* sharedMem) { // TODO: For testing purposes, revert max to 100 @@ -54,6 +54,7 @@ void StartProducer(SharedStruct* sharedMem) } } +// Produces and puts a number into the next free slot in the buffer. void Produce(SharedStruct* sharedMem) { int insertNum; @@ -65,19 +66,16 @@ void Produce(SharedStruct* sharedMem) return; } -// Generate a number between 0 and 9 +// Generate a number between 0 and 9. int produce_item(void) { return random() % 10; } -// Insert a number into the shared memory +// Insert a number into the shared memory. +// TODO: Reimplement insertion using the next slot from GetNextFreeSlot(). int insert_item(int item, SharedStruct* sharedMem) { - // int slot = GetNextFreeSlot(sharedMem); - // while (slot < 0) - // slot = GetNextFreeSlot(sharedMem); - // sharedMem->buffer[slot] = item; sharedMem->buffer[0] = item; return 1; } \ No newline at end of file diff --git a/src/shared.c b/src/shared.c index b203313..a375a41 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1,6 +1,7 @@ #include #include "shared.h" +// Initializes the struct in shared memory void SharedStructInit(SharedStruct* sharedMem) { for (int i = 0; i < 10; i++) @@ -11,10 +12,9 @@ void SharedStructInit(SharedStruct* sharedMem) 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); } +// Takes array of letters and turns it into a number int StringToNumber(char* argv) { int splitAmount;