Added structure plans
This commit is contained in:
parent
20039ede09
commit
8339af50c4
@ -2,12 +2,12 @@
|
|||||||
#define SHARED_H
|
#define SHARED_H
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
|
||||||
typedef struct sharedMem
|
typedef struct SharedStruct
|
||||||
{
|
{
|
||||||
int buffer[10];
|
int buffer[10];
|
||||||
sem_t semConsumer;
|
sem_t semConsumer;
|
||||||
sem_t semProducer;
|
sem_t semProducer;
|
||||||
} sharedMem;
|
} SharedStruct;
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,6 +1,9 @@
|
|||||||
#include "consumer.h"
|
#include "consumer.h"
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
|
// Consumer main function
|
||||||
|
// TODO: Add waiting for one consumer at a time
|
||||||
|
// Add ability to use shared memory
|
||||||
void* Consumer(void* arg)
|
void* Consumer(void* arg)
|
||||||
{
|
{
|
||||||
long int consumeNum;
|
long int consumeNum;
|
||||||
@ -16,11 +19,13 @@ void* Consumer(void* arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Manage item taken from shared memory
|
||||||
void consume_item(long int item)
|
void consume_item(long int item)
|
||||||
{
|
{
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Take item out of shared memory
|
||||||
long int remove_item()
|
long int remove_item()
|
||||||
{
|
{
|
||||||
;
|
;
|
||||||
|
28
src/driver.c
28
src/driver.c
@ -10,15 +10,37 @@
|
|||||||
int GetSplitAmount(char* argv);
|
int GetSplitAmount(char* argv);
|
||||||
void TestProducerConsumer(void);
|
void TestProducerConsumer(void);
|
||||||
|
|
||||||
|
// 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()
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srandom((unsigned int) time(NULL));
|
||||||
sharedMem testMemory;
|
|
||||||
|
|
||||||
int splitLimit = 1;
|
int splitLimit = 1;
|
||||||
|
int forkID, sharedID;
|
||||||
|
SharedStruct *sharedMem;
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
splitLimit = GetSplitAmount(argv[1]);
|
splitLimit = GetSplitAmount(argv[1]);
|
||||||
|
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();
|
TestProducerConsumer();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("Fork number %d created successfully.\n", forkID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetSplitAmount(char* argv)
|
int GetSplitAmount(char* argv)
|
||||||
|
@ -3,9 +3,13 @@
|
|||||||
#include "producer.h"
|
#include "producer.h"
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
|
// Producer main function
|
||||||
|
// TODO: Add waiting for one producer at a time
|
||||||
|
// Add ability to use shared memory
|
||||||
void* Producer(void* arg)
|
void* Producer(void* arg)
|
||||||
{
|
{
|
||||||
long int insertNum;
|
long int insertNum;
|
||||||
|
SharedStruct* sharedMem;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
insertNum = produce_item();
|
insertNum = produce_item();
|
||||||
@ -21,11 +25,13 @@ void* Producer(void* arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate a number between 0 and 9
|
||||||
long int produce_item(void)
|
long int produce_item(void)
|
||||||
{
|
{
|
||||||
return rand() % 10;
|
return random() % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert a number into the shared memory
|
||||||
void insert_item(long int item)
|
void insert_item(long int item)
|
||||||
{
|
{
|
||||||
;
|
;
|
||||||
|
Loading…
Reference in New Issue
Block a user