-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeadlock_Detection.cpp
More file actions
226 lines (198 loc) · 4.49 KB
/
Copy pathDeadlock_Detection.cpp
File metadata and controls
226 lines (198 loc) · 4.49 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Shabbir Mahmood ID: 14015439 Part-3 Even Operating System Lab( Dead Lock Algorithm) Algorithm Date: 05-04-2017*/
#include<bits/stdc++.h>
using namespace std;
int no_resource,no_process;
int instance_vector[100];
int allocation[100][100];
int maxx[100][100];
int request[100][100];
int available[100];
bool finish[100];
int work[100];
int cc=0,c=0,cnt=0;
int x=0;
int safe_sequence[50];
int check_finish() // Checking Finish[i] = true for all i
{
c=0;//*************
for(int i=0; i<no_process; i++)
{
if(finish[i] == true)
{
c++;
}
}
if (c==no_process)
{
return 1;
}
return 0;
}
int compare_request_work(int p) // Checking request[i] <= Work[i]
{
int cnt=0;//*************************
for(int i=0; i<no_resource; i++)
{
if(request[p][i] <= work[i])
{
cnt++;
}
}
if(cnt==no_resource)
{
return 1;
}
else
return 0;
}
int safety_algorithm() // Safety Algorithm
{
for(int i=0; i<no_process; i++)
{
finish[i] = false;
}
for(int i=0; i<no_resource; i++)
{
work[i] = available[i];
}
TOP: for(int i=0; i<no_process; i++)
{
if(finish[i]== false && compare_request_work(i))
{
for(int j=0; j<no_resource; j++)
{
work[j] += allocation[i][j];
}
finish[i] =true;
//cnt++;
safe_sequence[x++] = i;
}
}
if(check_finish())
{
return 1; // Finish[i] is true for all i, Complete
}
else if(cc<no_process*no_process) //************* (m x n*n) order to detect dead lock ---- Book- 335 Page
{
cc++;
goto TOP; // Goto the top label, But it will repeat up to no_process number
}
else
{
return 0; // Deadlock Occur
}
}
int main()
{
cout<<"Enter the No. of Resources: ";
cin>>no_resource;
cout<<"Enter the No. of Instances of Resource Vector: "<<endl;
for(int i=0; i<no_resource; i++)
{
cout<<"R"<<i<<": ";
cin>>instance_vector[i];
}
cout<<"Enter the No. of Processes: ";
cin>>no_process;
cout<<"Enter the Allocation Matrix: "<<endl;
for(int i=0; i<no_process; i++)
{
for(int j=0; j<no_resource; j++)
{
cin>>allocation[i][j];
}
}
cout<<"Enter the Max Matrix: "<<endl;
for(int i=0; i<no_process; i++)
{
for(int j=0; j<no_resource; j++)
{
cin>>maxx[i][j];
}
}
cout<<"The Available Instance Vector is: "<<endl;
for(int j=0; j<no_resource; j++)
{
for(int i=0; i<no_process; i++)
{
available[j] += allocation[i][j];
}
available[j] = instance_vector[j] - available[j];
cout<<available[j]<<" ";
}
cout<<endl;
cout<<"The request Matrix is: "<<endl;
for(int i=0; i<no_process; i++)
{
for(int j=0; j<no_resource; j++)
{
request[i][j] = maxx[i][j] - allocation[i][j];
cout<<request[i][j]<<" ";
}
cout<<endl;
}
if(safety_algorithm()) // if this function return 1 then Safe Sequence Exits
{
cout<<"There is a Safe Sequence & the Sequence is: ";
for(int i=0; i<no_process; i++)
{
cout<<"P"<<safe_sequence[i]<<" ";
}
}
else // if this function return 0 then Safe Sequence does not Exit (Deadlock)
{
cout<<"***Deadlock has been Detected***\nDeadlocked Processes are: ";
for(int i=0; i<no_process; i++)
{
if(finish[i] == false)
{
cout<<"P"<<i<<" ";
}
}
}
cout<<endl;
return 0;
}
/*
3
10 5 7
5
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
/////////////////
3
7 2 6
5
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
0 1 0
4 0 2
3 0 3
3 1 1
0 0 4
////////// Deadlock
3
7 2 6
5
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
0 1 0
4 0 2
3 0 4
3 1 1
0 0 4
*/