2022-10-11 20:18:38 -05:00
|
|
|
#include <stdlib.h>
|
2022-10-12 21:11:15 -05:00
|
|
|
#include <stdio.h>
|
2022-10-13 23:10:33 -05:00
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2022-10-12 15:44:12 -05:00
|
|
|
#include "shared.h"
|
2022-10-11 19:53:52 -05:00
|
|
|
|
2022-10-13 23:10:33 -05:00
|
|
|
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));
|
|
|
|
int sharedID = StringToNumber(argv[1]);
|
2022-10-13 23:25:16 -05:00
|
|
|
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
|
|
|
|
printf("Adding 5 to shared struct.\n");
|
|
|
|
sharedMem->buffer[0] = 5;
|
|
|
|
sem_post(&sharedMem->semConsumer);
|
2022-10-13 23:10:33 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-12 22:59:06 -05:00
|
|
|
// Producer main function
|
|
|
|
// TODO: Add waiting for one producer at a time
|
|
|
|
// Add ability to use shared memory
|
2022-10-12 21:11:15 -05:00
|
|
|
void* Producer(void* arg)
|
2022-10-11 20:18:38 -05:00
|
|
|
{
|
2022-10-12 21:11:15 -05:00
|
|
|
long int insertNum;
|
2022-10-12 22:59:06 -05:00
|
|
|
SharedStruct* sharedMem;
|
2022-10-11 20:18:38 -05:00
|
|
|
while (1)
|
|
|
|
{
|
2022-10-12 21:11:15 -05:00
|
|
|
insertNum = produce_item();
|
|
|
|
printf("The random number is %li\n", insertNum);
|
2022-10-12 15:44:12 -05:00
|
|
|
// sem_wait(&semProducer);
|
|
|
|
// down(&empty);
|
|
|
|
// down(&mutex);
|
2022-10-12 21:11:15 -05:00
|
|
|
insert_item(insertNum);
|
2022-10-12 15:44:12 -05:00
|
|
|
// sem_post(&semProducer);
|
|
|
|
// up(&mutex);
|
|
|
|
// up(&full);
|
|
|
|
break;
|
2022-10-11 20:18:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 22:59:06 -05:00
|
|
|
// Generate a number between 0 and 9
|
2022-10-11 20:18:38 -05:00
|
|
|
long int produce_item(void)
|
|
|
|
{
|
2022-10-12 22:59:06 -05:00
|
|
|
return random() % 10;
|
2022-10-11 20:18:38 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 22:59:06 -05:00
|
|
|
// Insert a number into the shared memory
|
2022-10-12 15:44:12 -05:00
|
|
|
void insert_item(long int item)
|
2022-10-11 19:50:07 -05:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|