-
Notifications
You must be signed in to change notification settings - Fork 1
/
Producer Consumer using Semaphores.c
68 lines (57 loc) · 1.41 KB
/
Producer Consumer using Semaphores.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#define BUFFER_SIZE 5
typedef struct {
sem_t empty;
sem_t full;
sem_t mutex;
int buffer[BUFFER_SIZE];
int in, out;
} psshm;
int main()
{
int shmid, pid;
psshm *ptr;
shmid = shmget(IPC_PRIVATE, sizeof(psshm), IPC_CREAT | 0666);
ptr = (psshm*) shmat(shmid, 0, 0);
sem_init(&ptr->empty, 1, BUFFER_SIZE);
sem_init(&ptr->full, 1, 0);
sem_init(&ptr->mutex, 1, 1);
ptr->in = 0;
ptr->out = 0;
pid = fork();
if (pid > 0)
{
while (1)
{
int item = rand() % 20;
sem_wait(&ptr->empty);
sem_wait(&ptr->mutex);
ptr->buffer[ptr->in] = item;
printf("Produced: %d\n", item);
ptr->in = (ptr->in + 1) % BUFFER_SIZE;
sem_post(&ptr->mutex);
sem_post(&ptr->full);
sleep(rand()%3);
}
}
else if (pid == 0)
{
while (1)
{
sem_wait(&ptr->full);
sem_wait(&ptr->mutex);
int item = ptr->buffer[ptr->out];
printf("Consumed: %d\n", item);
ptr->out = (ptr->out + 1) % BUFFER_SIZE;
sem_post(&ptr->mutex);
sem_post(&ptr->empty);
sleep(rand()%3);
}
}
return 0;
}