-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathproducer_consumer.c
65 lines (62 loc) · 1.05 KB
/
producer_consumer.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
#include<stdio.h>
int full=0;
int empty=5;
int mutex=1;
int x=0;
int buffer[5];
int in=0;
int out=0;
void producer()
{
--mutex;
--empty;
x++;
buffer[in]=x;
printf("\nproducer produced %d",buffer[in]);
in=(in+1)%5;
++full;
++mutex;
}
void consumer()
{
--mutex;
--full;
printf("\nconsumer consumed %d",buffer[out]);
out=(out+1)%5;
++mutex;
++empty;
}
void main()
{
int i,n;
int option=0;
printf("\n1 for producer""\n2 for consumer""\n3 for exit");
while(option!=3)
{
printf("\nenter the choice");
scanf("%d",&n);
switch(n)
{
case 1:
if(mutex==1 && empty!=0)
{
producer();
}
else{
printf("\nfull");
}
break;
case 2:
if(mutex==1&&full!=0)
{
consumer();
}
else{
printf("\nempty");
}
break;
case 3:
exit(1);
}
}
}