producers-consumers/src/producer.c

38 lines
838 B
C
Raw Normal View History

2022-10-11 20:18:38 -05:00
#include <stdlib.h>
2022-10-12 21:11:15 -05:00
#include <stdio.h>
2022-10-11 19:53:52 -05:00
#include "producer.h"
2022-10-12 15:44:12 -05:00
#include "shared.h"
2022-10-11 19:53:52 -05:00
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
{
;
}