Split program into three files, added arguments for fork amounts, and passes sharedID into forks for exec

This commit is contained in:
TriantaTV 2022-10-13 23:10:33 -05:00
parent 8339af50c4
commit a22970ff41
8 changed files with 121 additions and 39 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -2,6 +2,8 @@
#define SHARED_H
#include <semaphore.h>
int StringToNumber(char* argv);
typedef struct SharedStruct
{
int buffer[10];

View File

@ -1,6 +1,25 @@
#include "consumer.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>
#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

View File

@ -1,32 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <time.h>
#include <unistd.h>
#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);
}

View File

@ -1,8 +1,28 @@
#include <stdlib.h>
#include <stdio.h>
#include "producer.h"
#include <sys/shm.h>
#include <time.h>
#include <unistd.h>
#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

View File

@ -1 +1,9 @@
#include "shared.h"
#include <stdio.h>
#include "shared.h"
int StringToNumber(char* argv)
{
int splitAmount;
sscanf(argv, "%d", &splitAmount);
return splitAmount;
}