-
Notifications
You must be signed in to change notification settings - Fork 0
/
prg3.c
58 lines (58 loc) · 867 Bytes
/
prg3.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
#include<stdio.h>
#include<stdlib.h>
int mutex=1;
int full=0;
int empty=10;
void producer();
void consumer();
int main()
{
printf("Press 1 for Producer\n");
printf("Press 2 for Consumer\n");
printf("Press 3 for exit\n");
while(1){
again: printf("Enter your choice\n");
int n;
scanf("%d",&n);
switch(n){
case 1:
if((mutex==1) && (empty!=0)){
producer();
printf("Item Produced\n");
}
else{
printf("The Buffer is Full\n");
}
break;
case 2:
if((mutex==1) && (full!=0)){
consumer();
printf("Item Consumed\n");
}
else{
printf("The Buffer is empty\n");
}
break;
case 3:
exit(0);
break;
default:
printf("Invalid Input\n");
goto again;
}
}
}
void producer()
{
--mutex;
++full;
--empty;
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
++mutex;
}