-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.bounded buffer using semaphpore.cpp
More file actions
100 lines (78 loc) · 2.43 KB
/
Copy path2.bounded buffer using semaphpore.cpp
File metadata and controls
100 lines (78 loc) · 2.43 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<bits/stdc++.h>
using namespace std;
int s = 1;
int buffer_size = 3;
int emp = 3;
int filled = 0;
queue<int>producer_waiting;
queue<int>consumer_waiting;
void consumer(int n2);
void producer(int n1){
for(int i = 0; i<n1; i++){
//wait
//check buffer avaibility
if(emp>=1){ //min n1 ta slot empty thakte hbe
if(s == 1){
//add to buffer
emp--;
s = 0;
cout<<"product working on buffer"<<endl;
}
}
else{
// emp<=0, since buffer has no empty slot
producer_waiting.push(i);
cout<<"product added to producer waiting queue"<<endl;
}
// now signal part
if(filled <3){ //i.e min 1 ta slot faka ache
s = 1;// i.e release resource
filled++;
cout<<"product added to buffer"<<endl;
// consumer waiting portion
// filled = 3, so consumer ekhn service dite parbo
if(!consumer_waiting.empty()){
consumer_waiting.pop();
cout<<"consumer request granted from waiting"<<endl;
consumer(1); //consume only one peace
}
}
}
}
void consumer(int n2){
for(int i = 0; i<n2; i++){
//wait
//check buffer avaibility
if(filled>=1){ //min 1 ta slot filled
if(s == 1){
//acquire resource
filled--;
s = 0;
cout<<"product working"<<endl;
}
}
else{
// filled <=0, since buffer has full
consumer_waiting.push(i);
cout<<"product added to consumer waiting queue"<<endl;
}
// now signal part
if(emp <3){ //1,2 min 1ta pos3 can be empty
s = 1; //release resourse
emp++;
cout<<"product consumed"<<endl;
//producer waiting portion
//lets say emp = 1, producer request grant
if(!producer_waiting.empty()){
producer_waiting.pop();
cout<<"producer request granted from waiting"<<endl;
producer(1); // only one product add to buffer
}
}
}
}
int main(){
int n1 = 5, n2 = 2;
producer(n1);
consumer(n2);
}