Added structure plans

This commit is contained in:
TriantaTV 2022-10-12 22:59:06 -05:00
parent 20039ede09
commit 8339af50c4
4 changed files with 40 additions and 7 deletions

View File

@ -2,12 +2,12 @@
#define SHARED_H
#include <semaphore.h>
typedef struct sharedMem
typedef struct SharedStruct
{
int buffer[10];
sem_t semConsumer;
sem_t semProducer;
} sharedMem;
} SharedStruct;
#endif

View File

@ -1,6 +1,9 @@
#include "consumer.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)
{
long int consumeNum;
@ -16,11 +19,13 @@ void* Consumer(void* arg)
}
}
// Manage item taken from shared memory
void consume_item(long int item)
{
;
}
// Take item out of shared memory
long int remove_item()
{
;

View File

@ -10,15 +10,37 @@
int GetSplitAmount(char* argv);
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[])
{
srand(time(NULL));
sharedMem testMemory;
srandom((unsigned int) time(NULL));
int splitLimit = 1;
int forkID, sharedID;
SharedStruct *sharedMem;
if (argc == 2)
splitLimit = GetSplitAmount(argv[1]);
TestProducerConsumer();
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);
}
}
int GetSplitAmount(char* argv)

View File

@ -3,9 +3,13 @@
#include "producer.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)
{
long int insertNum;
SharedStruct* sharedMem;
while (1)
{
insertNum = produce_item();
@ -21,11 +25,13 @@ void* Producer(void* arg)
}
}
// Generate a number between 0 and 9
long int produce_item(void)
{
return rand() % 10;
return random() % 10;
}
// Insert a number into the shared memory
void insert_item(long int item)
{
;