Added comments for readability
This commit is contained in:
parent
898851a185
commit
cbd8b5ceb0
@ -9,6 +9,7 @@ void Consume(SharedStruct* sharedMem);
|
||||
void consume_item(int item, SharedStruct* sharedMem);
|
||||
int remove_item(SharedStruct* sharedMem);
|
||||
|
||||
// Checks for SharedID, then begins producing numbers into shared memory.
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
@ -22,9 +23,7 @@ int main(int argc, char* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Consumer main function
|
||||
// TODO: Add waiting for one consumer at a time
|
||||
// Add ability to use shared memory
|
||||
// Starts the consumer process.
|
||||
void StartConsumer(SharedStruct* sharedMem)
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
@ -35,6 +34,7 @@ void StartConsumer(SharedStruct* sharedMem)
|
||||
}
|
||||
}
|
||||
|
||||
// Takes a number from the shared memory and consumes it.
|
||||
void Consume(SharedStruct* sharedMem)
|
||||
{
|
||||
int consumeNum;
|
||||
@ -46,14 +46,14 @@ void Consume(SharedStruct* sharedMem)
|
||||
return;
|
||||
}
|
||||
|
||||
// Manage item taken from shared memory
|
||||
// Increments the count of numbers generated so far in shared memory.
|
||||
void consume_item(int item, SharedStruct* sharedMem)
|
||||
{
|
||||
sharedMem->count[item]++;
|
||||
return;
|
||||
}
|
||||
|
||||
// Take item out of shared memory
|
||||
// Take item out of shared memory.
|
||||
int remove_item(SharedStruct* sharedMem)
|
||||
{
|
||||
int item = sharedMem->buffer[0];
|
||||
|
22
src/driver.c
22
src/driver.c
@ -8,8 +8,8 @@ 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 command line arguments for producer and consumer number
|
||||
// Checks for producer and consumer arguments, creates shared memory,
|
||||
// then creates forks sending the sharedID as an argument.
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int producerAmount = GetSplitAmount(argc, argv, 0);
|
||||
@ -22,13 +22,12 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
||||
SharedStructInit(sharedMem);
|
||||
// Create child forks
|
||||
// TODO: Add creating consumers and producers instead of general forks
|
||||
CreateChildForks(producerAmount, 0, sharedID);
|
||||
CreateChildForks(consumerAmount, 1, sharedID);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Checks for argument based on passed in letter.
|
||||
int CheckArgumentPosition(int argc, char* argv[], char letter)
|
||||
{
|
||||
char argumentHyphen = '-';
|
||||
@ -43,10 +42,10 @@ int CheckArgumentPosition(int argc, char* argv[], char letter)
|
||||
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
|
||||
// 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;
|
||||
@ -59,13 +58,10 @@ void CreateChildForks(int forkAmount, int forkType, int sharedID)
|
||||
exit(1);
|
||||
}
|
||||
if (forkID == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (forkID != 0)
|
||||
return;
|
||||
// TODO: Pass through sharedID
|
||||
char sharedIDStr[16];
|
||||
sprintf(sharedIDStr, "%d", sharedID);
|
||||
if (forkType == 0)
|
||||
@ -75,8 +71,8 @@ void CreateChildForks(int forkAmount, int forkType, int sharedID)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Takes in argc, argv, and type of fork to check for
|
||||
// forkType: 0 for creating producers, non-zero for consumers
|
||||
// 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)
|
||||
{
|
||||
char letter;
|
||||
|
@ -11,7 +11,7 @@ void Produce(SharedStruct* sharedMem);
|
||||
int produce_item(void);
|
||||
int insert_item(int item, SharedStruct* sharedMem);
|
||||
|
||||
|
||||
// Checks for SharedID, then begins producing numbers into shared memory.
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
@ -26,6 +26,8 @@ int main(int argc, char* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Checks for the next available slot in buffer and returns slot number.
|
||||
// TODO: Fix function to get next free slot and return slot number.
|
||||
int GetNextFreeSlot(SharedStruct* sharedMem)
|
||||
{
|
||||
int slot = -1;
|
||||
@ -40,9 +42,7 @@ int GetNextFreeSlot(SharedStruct* sharedMem)
|
||||
return slot;
|
||||
}
|
||||
|
||||
// Producer main function
|
||||
// TODO: Add waiting for one producer at a time
|
||||
// Add ability to use shared memory
|
||||
// Starts the producer process.
|
||||
void StartProducer(SharedStruct* sharedMem)
|
||||
{
|
||||
// TODO: For testing purposes, revert max to 100
|
||||
@ -54,6 +54,7 @@ void StartProducer(SharedStruct* sharedMem)
|
||||
}
|
||||
}
|
||||
|
||||
// Produces and puts a number into the next free slot in the buffer.
|
||||
void Produce(SharedStruct* sharedMem)
|
||||
{
|
||||
int insertNum;
|
||||
@ -65,19 +66,16 @@ void Produce(SharedStruct* sharedMem)
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate a number between 0 and 9
|
||||
// Generate a number between 0 and 9.
|
||||
int produce_item(void)
|
||||
{
|
||||
return random() % 10;
|
||||
}
|
||||
|
||||
// Insert a number into the shared memory
|
||||
// Insert a number into the shared memory.
|
||||
// TODO: Reimplement insertion using the next slot from GetNextFreeSlot().
|
||||
int insert_item(int item, SharedStruct* sharedMem)
|
||||
{
|
||||
// int slot = GetNextFreeSlot(sharedMem);
|
||||
// while (slot < 0)
|
||||
// slot = GetNextFreeSlot(sharedMem);
|
||||
// sharedMem->buffer[slot] = item;
|
||||
sharedMem->buffer[0] = item;
|
||||
return 1;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include "shared.h"
|
||||
|
||||
// Initializes the struct in shared memory
|
||||
void SharedStructInit(SharedStruct* sharedMem)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
@ -11,10 +12,9 @@ void SharedStructInit(SharedStruct* sharedMem)
|
||||
sem_init(&sharedMem->mutex, 1, 1);
|
||||
sem_init(&sharedMem->empty, 1, 1);
|
||||
sem_init(&sharedMem->full, 1, 1);
|
||||
// sem_init(&sharedMem->semConsumer, 1, 1);
|
||||
// sem_init(&sharedMem->semProducer, 1, 1);
|
||||
}
|
||||
|
||||
// Takes array of letters and turns it into a number
|
||||
int StringToNumber(char* argv)
|
||||
{
|
||||
int splitAmount;
|
||||
|
Loading…
Reference in New Issue
Block a user