2022-10-11 19:50:07 -05:00
|
|
|
#include <stdio.h>
|
2022-10-12 21:11:15 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <time.h>
|
2022-10-11 19:50:07 -05:00
|
|
|
#include <unistd.h>
|
2022-10-12 15:44:12 -05:00
|
|
|
#include "consumer.h"
|
|
|
|
#include "producer.h"
|
|
|
|
#include "shared.h"
|
2022-10-11 20:18:38 -05:00
|
|
|
|
2022-10-11 19:50:07 -05:00
|
|
|
int GetSplitAmount(char* argv);
|
2022-10-12 21:11:15 -05:00
|
|
|
void TestProducerConsumer(void);
|
2022-10-11 19:50:07 -05:00
|
|
|
|
2022-10-12 22:59:06 -05:00
|
|
|
// 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()
|
2022-10-11 19:50:07 -05:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-10-12 22:59:06 -05:00
|
|
|
srandom((unsigned int) time(NULL));
|
2022-10-11 19:50:07 -05:00
|
|
|
int splitLimit = 1;
|
2022-10-12 22:59:06 -05:00
|
|
|
int forkID, sharedID;
|
|
|
|
SharedStruct *sharedMem;
|
2022-10-11 19:50:07 -05:00
|
|
|
if (argc == 2)
|
|
|
|
splitLimit = GetSplitAmount(argv[1]);
|
2022-10-12 22:59:06 -05:00
|
|
|
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);
|
|
|
|
}
|
2022-10-11 19:50:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int GetSplitAmount(char* argv)
|
|
|
|
{
|
|
|
|
int splitAmount;
|
|
|
|
sscanf(argv, "%d", &splitAmount);
|
|
|
|
return splitAmount;
|
|
|
|
}
|
2022-10-12 21:11:15 -05:00
|
|
|
|
|
|
|
void TestProducerConsumer(void)
|
|
|
|
{
|
|
|
|
Producer(NULL);
|
|
|
|
Consumer(NULL);
|
|
|
|
}
|