-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.c
85 lines (57 loc) · 1.66 KB
/
producer.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
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<stdlib.h>
int main()
{
int full=0;
int empty=5;
int mutex=1,choice;
int x=0;
int wait(int n)
{
n=n-1;
return n;
}
int signal(int k)
{
k=k+1;
return k;
}
void producer()
{
empty=wait(empty);
mutex=wait(mutex);
mutex=signal(mutex);
full=signal(full);
printf(" item %d produced by producer\n",++x);
}
void consumer()
{
full=wait(full);
mutex=wait(mutex);
mutex=signal(mutex);
empty=signal(empty);
printf(" item %d consumed by consumer\n",x--);
}
do
{
printf("1.PRODUCER 2.CONSUMER 3.EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: if(mutex==1&& empty!=0)
producer();
else
printf("BUFFER IS FULL\n\n");
break;
case 2: if(mutex==1&& full!=0)
consumer();
else
printf("BUFFER IS EMPTY\n\n");
break;
case 3: exit(0);
break;
}
}
while(choice!=3);
}