-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.c
58 lines (58 loc) · 1.27 KB
/
6.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
// PRODUCER-CONSUMER PROBLEM USING SEMAPHORES
#include <stdio.h>
#include <stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int signal(int s)
{
return (++s);
}
int wait(int s)
{
return (--s);
}
void producer()
{
empty = wait(empty);
mutex = wait(mutex);
x++;
printf("\nProducer Produces the item %d ", x);
mutex = signal(mutex);
full = signal(full);
}
void consumer()
{
full = wait(full);
mutex = wait(mutex);
printf("\nConsumer Consumes the item %d ", x);
x--;
mutex = signal(mutex);
empty = signal(empty);
}
void main()
{
int n;
while (1)
{
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
printf("\nENTER YOUR CHOICE\n");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("\nBUFFER IS FULL\n");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("\nBUFFER IS EMPTY\n");
break;
case 3:
exit(0);
break;
}
}
}