-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProducer-Consumer_problem.cpp
More file actions
60 lines (48 loc) · 1.13 KB
/
Producer-Consumer_problem.cpp
File metadata and controls
60 lines (48 loc) · 1.13 KB
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
#include<bits/stdc++.h>
int buffer,empty,full=0,mutex=1,op,x=0;
int wait(int s){
while(s<=0);
return --s;
}
int signal(int s){
return ++s;
}
void produce(){
if(mutex==1 && empty!=0){
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
mutex=signal(mutex);
++x;
printf("\n Producer produces an item %d \n\n",x);
}else
printf("\n Buffer is Full , cannot produce\n");
}
void consume(){
if(mutex==1 && full!=0){
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
mutex=signal(mutex);
printf("\n Consumer consumed an item %d \n\n",x);
--x;
}else
printf("\n Buffer is Empty , cannot consume\n");
}
int main(){
printf("Enter the buffer size ");
scanf("%d",&buffer);
empty=buffer;
while(1){
printf("\n \t choose the operation : ");
printf("\n1.produce\n2.consume\n3.exit\t>>>:");
scanf("%d",&op);
switch(op){
case 1: produce();
break;
case 2: consume();
break;
case 3: exit(0);
}
}
}