Split program into three files, added arguments for fork amounts, and passes sharedID into forks for exec
This commit is contained in:
parent
8339af50c4
commit
a22970ff41
6
Makefile
6
Makefile
@ -6,9 +6,13 @@ compile:
|
|||||||
gcc $(INC) -c -o build/driver.o src/driver.c
|
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/producer.o src/producer.c
|
||||||
gcc $(INC) -c -o build/consumer.o src/consumer.c
|
gcc $(INC) -c -o build/consumer.o src/consumer.c
|
||||||
|
gcc $(INC) -c -o build/shared.o src/shared.c
|
||||||
|
|
||||||
link:
|
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
|
exec: compile link
|
||||||
./bin/driver.out
|
./bin/driver.out
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
#ifndef CONSUMER_H
|
#ifndef CONSUMER_H
|
||||||
#define CONSUMER_H
|
#define CONSUMER_H
|
||||||
|
|
||||||
void* Consumer(void* arg);
|
|
||||||
void consume_item(long int item);
|
|
||||||
long int remove_item(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,8 +1,5 @@
|
|||||||
#ifndef PRODUCER_H
|
#ifndef PRODUCER_H
|
||||||
#define PRODUCER_H
|
#define PRODUCER_H
|
||||||
|
|
||||||
void* Producer(void* arg);
|
|
||||||
long int produce_item(void);
|
|
||||||
void insert_item(long int item);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -2,6 +2,8 @@
|
|||||||
#define SHARED_H
|
#define SHARED_H
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
int StringToNumber(char* argv);
|
||||||
|
|
||||||
typedef struct SharedStruct
|
typedef struct SharedStruct
|
||||||
{
|
{
|
||||||
int buffer[10];
|
int buffer[10];
|
||||||
|
@ -1,6 +1,25 @@
|
|||||||
#include "consumer.h"
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "shared.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
|
// Consumer main function
|
||||||
// TODO: Add waiting for one consumer at a time
|
// TODO: Add waiting for one consumer at a time
|
||||||
// Add ability to use shared memory
|
// Add ability to use shared memory
|
||||||
|
92
src/driver.c
92
src/driver.c
@ -1,32 +1,56 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "consumer.h"
|
|
||||||
#include "producer.h"
|
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
int GetSplitAmount(char* argv);
|
int CheckArgumentPosition(int argc, char* argv[], char letter);
|
||||||
void TestProducerConsumer(void);
|
void CreateChildForks(int forkAmount, int forkType, int sharedID);
|
||||||
|
int GetSplitAmount(int argc, char* argv[], int forkType);
|
||||||
|
|
||||||
// TODO: Clean up main
|
// TODO: Clean up main
|
||||||
// Add ability to send different consumer and producer numbers
|
// Add command line arguments for producer and consumer number
|
||||||
// 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()
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
srandom((unsigned int) time(NULL));
|
int producerAmount = GetSplitAmount(argc, argv, 0);
|
||||||
int splitLimit = 1;
|
int consumerAmount = GetSplitAmount(argc, argv, 1);
|
||||||
int forkID, sharedID;
|
int sharedID;
|
||||||
SharedStruct *sharedMem;
|
SharedStruct *sharedMem;
|
||||||
if (argc == 2)
|
sharedID = shmget(IPC_CREAT | (key_t)1243, sizeof(SharedStruct), IPC_CREAT | 0666);
|
||||||
splitLimit = GetSplitAmount(argv[1]);
|
if (sharedID < 0)
|
||||||
sharedID = shmget(IPC_EXCL | (key_t)1122, sizeof(SharedStruct), IPC_CREAT | 666);
|
{
|
||||||
|
fprintf(stderr, "Shmget failed\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
// Create child forks
|
// Create child forks
|
||||||
// TODO: Add creating consumers and producers instead of general 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();
|
forkID = fork();
|
||||||
if (forkID < 0)
|
if (forkID < 0)
|
||||||
@ -36,22 +60,34 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
if (forkID == 0)
|
if (forkID == 0)
|
||||||
{
|
{
|
||||||
TestProducerConsumer();
|
|
||||||
break;
|
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;
|
char letter;
|
||||||
sscanf(argv, "%d", &splitAmount);
|
int position;
|
||||||
return splitAmount;
|
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);
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,28 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "producer.h"
|
#include <sys/shm.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "shared.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
|
// Producer main function
|
||||||
// TODO: Add waiting for one producer at a time
|
// TODO: Add waiting for one producer at a time
|
||||||
// Add ability to use shared memory
|
// Add ability to use shared memory
|
||||||
|
@ -1 +1,9 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
|
int StringToNumber(char* argv)
|
||||||
|
{
|
||||||
|
int splitAmount;
|
||||||
|
sscanf(argv, "%d", &splitAmount);
|
||||||
|
return splitAmount;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user