-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducer_Consumer.cpp
More file actions
52 lines (47 loc) · 1.26 KB
/
producer_Consumer.cpp
File metadata and controls
52 lines (47 loc) · 1.26 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
#include<bits/stdc++.h>
using namespace std;
void wait(int &x){
if(x>0){
x--;
}
}
void signal(int &x){
x++;
}
int main(){
int a[5];
int empty=5;
int full=0;
int mutex=1;
while(1){
//cout<<"Empty: "<<empty<<"\t"<<"Full: "<<full<<"\t"<<"Mutex: "<<mutex<<"\t"<<endl;
cout<<"Choose A Option: 1. Produce\t 2.Consume\t3.End"<<endl;
int n;
cin>>n;
if(n==1){
if(empty!=0 && mutex==1){
wait(empty);
wait(mutex);
cout<<"Enter the data you want to produce:"<<endl;
cin>>a[full];
signal(mutex);
signal(full);
}else{
cout<<"Buffer is full. You can't produce any data"<<endl;
}
}else if(n==2){
if(full>0 && mutex==1){
cout<<"Full:"<<full<<endl;
wait(full);
wait(mutex);
cout<<"The Data is: "<<a[full]<<endl;
signal(mutex);
signal(empty);
}else{
cout<<"Buffer is empty. You can't consume any data"<<endl;
}
}else if(n==3){
break;
}
}
}