-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_Binary_Semaphore.cxx
102 lines (76 loc) · 1.3 KB
/
10_Binary_Semaphore.cxx
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
101
102
#include<iostream>
#include<queue>
using namespace std;
int ct=0;
struct process
{
int id;
int bt;
}p1,p2,p3;
struct semaphore
{
int value;
queue <process>q1;
} s1;
void signal();
void critical_section(process a);
void wait(process a[],int n)
{
for(int i=1;i<n;i++)
(s1.q1).push(a[i]);
if(s1.value==1)
{
s1.value=0;
critical_section(a[0]);
}
}
void showqueue(queue<process>q)
{
process p;
while(!q.empty())
{
p=q.front();
q.pop();
cout<<" "<<p.id<<" ";
}
cout<<"\t"<<s1.value;
}
void critical_section(process a)
{
cout<<"\n "<<a.id<<"\t"<<a.bt<<"\t";
showqueue(s1.q1);
ct=ct+a.bt;
signal();
}
void signal()
{
process p;
if(!s1.q1.empty())
{
p=s1.q1.front();
s1.q1.pop();
critical_section(p);
}
else
s1.value=1;
}
int main()
{
int bt[20];int n;
process a[3];
s1.value=1;
p1.id=1;
p1.bt=2;
p2.id=2;
p2.bt=3;
p3.id=3;
p3.bt=4;
a[0]=p1;
a[1]=p2;
a[2]=p3;
cout<<"\n INITIALLY SEMPAHORE VALUE IS "<<s1.value;
cout<<"\nPRO ID\tBUR TI\tWAIT\tSEMAPHORE\n";
wait(a,3);
cout<<"\nFINALLY SEMAPHORE VALUE "<<s1.value;
cout<<"\nCURRENT TIME NOW BECOMES "<<ct;
}