2022-10-12 21:11:15 -05:00
|
|
|
#include <stdlib.h>
|
2022-10-13 23:10:33 -05:00
|
|
|
#include <stdio.h>
|
2022-10-12 21:11:15 -05:00
|
|
|
#include <sys/shm.h>
|
2022-10-11 19:50:07 -05:00
|
|
|
#include <unistd.h>
|
2022-10-12 15:44:12 -05:00
|
|
|
#include "shared.h"
|
2022-10-11 20:18:38 -05:00
|
|
|
|
2022-10-13 23:10:33 -05:00
|
|
|
int CheckArgumentPosition(int argc, char* argv[], char letter);
|
|
|
|
void CreateChildForks(int forkAmount, int forkType, int sharedID);
|
|
|
|
int GetSplitAmount(int argc, char* argv[], int forkType);
|
2022-10-11 19:50:07 -05:00
|
|
|
|
2022-10-12 22:59:06 -05:00
|
|
|
// TODO: Clean up main
|
2022-10-13 23:10:33 -05:00
|
|
|
// Add command line arguments for producer and consumer number
|
2022-10-11 19:50:07 -05:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-10-13 23:10:33 -05:00
|
|
|
int producerAmount = GetSplitAmount(argc, argv, 0);
|
|
|
|
int consumerAmount = GetSplitAmount(argc, argv, 1);
|
|
|
|
int sharedID;
|
2022-10-13 23:25:16 -05:00
|
|
|
SharedStruct* sharedMem;
|
2022-10-13 23:10:33 -05:00
|
|
|
sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666);
|
|
|
|
if (sharedID < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Shmget failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-10-12 22:59:06 -05:00
|
|
|
// Create child forks
|
|
|
|
// TODO: Add creating consumers and producers instead of general forks
|
2022-10-13 23:10:33 -05:00
|
|
|
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++)
|
2022-10-12 22:59:06 -05:00
|
|
|
{
|
|
|
|
forkID = fork();
|
|
|
|
if (forkID < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (forkID == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 23:10:33 -05:00
|
|
|
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);
|
2022-10-11 19:50:07 -05:00
|
|
|
}
|
|
|
|
|
2022-10-13 23:10:33 -05:00
|
|
|
// 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)
|
2022-10-11 19:50:07 -05:00
|
|
|
{
|
2022-10-13 23:10:33 -05:00
|
|
|
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]);
|
2022-10-12 21:11:15 -05:00
|
|
|
|
|
|
|
}
|