#include #include #include #include #include "shared.h" 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 int main(int argc, char* argv[]) { int producerAmount = GetSplitAmount(argc, argv, 0); int consumerAmount = GetSplitAmount(argc, argv, 1); 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); 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) { fprintf(stderr, "fork failed\n"); exit(1); } if (forkID == 0) { break; } } 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); } // 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; 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]); }