forked from iedcbootcampcec/letshack-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_consumer.c
69 lines (62 loc) · 1.04 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
66
67
68
69
//Producer Consumer
#include <stdio.h>
#include <stdlib.h>
int mutex=1, full=0, empty,x=0;
int main()
{
int n;
printf("Enter the Size: ");
empty=n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer \n2.Consumer \n3. Exit");
while(1)
{
printf("\n Enter Your Choice");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&& (empty!=0))
producer();
else
printf("Buffer Is Full");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer Is empty");
break;
case 3: exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n Producer Produces the Item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=signal(full);
empty=signal(empty);
printf("\n Consumer Consume item %d", x);
x--;
mutex=signal(mutex);
}