-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.c
More file actions
69 lines (60 loc) · 889 Bytes
/
9.c
File metadata and controls
69 lines (60 loc) · 889 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#define VAL 5
#define SIZE 3
int mutex=1,full=0,empty=SIZE;
int x=VAL;
int wait(int s)
{
return --s;
}
int signal(int s)
{
return ++s;
}
void producer()
{
empty=wait(empty);
mutex=wait(mutex);
x++;
printf("Producer producing : %d\n",x);
mutex=signal(mutex);
full=signal(full);
}
void consumer()
{
full=wait(full);
mutex=wait(mutex);
printf("Consumer consuming %d\n",x);
x--;
mutex=signal(mutex);
empty=signal(empty);
}
int main()
{
int num,c=0;
while(1)
{
printf("\nEnter 1-Producer\n2-Consumer\n3-exit\n");
scanf("%d",&num);
switch(num)
{
case 1: if(mutex && empty>0)
producer();
else
printf("full buffer\n");
break;
case 2: if(mutex && full>0)
consumer();
else
printf("empty buffer\n");
break;
case 3:
default: c=1;
exit(0);
}
if(c)
break;
}
return 0;
}