-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeadlock-detection.cpp
More file actions
112 lines (101 loc) · 3.07 KB
/
deadlock-detection.cpp
File metadata and controls
112 lines (101 loc) · 3.07 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
#include <bits/stdc++.h>
using namespace std;
struct process
{
string id;
vector<int> allocation, request;
};
bool deadlockDetection(vector<int> work, vector<process> x)
{
string sequence = ""; //... Safe state sequence
int no_of_resource = work.size();
int no_of_process = x.size();
vector<bool> finish(no_of_process, false);
for (int i = 0; i < no_of_process; i++)
{
int count = accumulate(x[i].allocation.begin(), x[i].allocation.end(), 0);
if (!count) finish[i] = true;
}
while (true)
{
bool found = false;
for (int i = 0; i < no_of_process; i++)
{
if (finish[i]) continue;
bool ok = true;
for (int j = 0; j < no_of_resource; j++)
{
if (x[i].request[j] > work[j])
{
ok = false;
break;
}
}
if (ok)
{
found = true;
finish[i] = true;
for (int j = 0; j < no_of_resource; j++)
{
work[j] += x[i].allocation[j];
}
sequence += x[i].id + " ";
}
}
if (!found) break;
}
for (int i = 0; i < no_of_process; i++)
{
if (!finish[i]) return false;
}
cout << sequence << "\n";
return true;
}
int main()
{
int no_of_resource; //... Total number of resources
cin >> no_of_resource;
vector<int> available(no_of_resource);
for (int &value: available)
{
cin >> value; //... Total number of each available resource instances
}
int no_of_process; //... Total number of processes
cin >> no_of_process;
vector<process> x(no_of_process); //... Process descriptions
for (int i = 0; i < no_of_process; i++)
{
x[i].allocation.resize(no_of_resource); //... Vector max_need size = no_of_resource
x[i].request = x[i].allocation;
cin >> x[i].id;
for (int j = 0; j < no_of_resource; j++)
{
cin >> x[i].allocation[j]; //... Total already allocated resources
}
for (int j = 0; j < no_of_resource; j++)
{
cin >> x[i].request[j]; //... Total resource requests
}
}
if (deadlockDetection(available, x))
{
cout << "No Deadlock. The system is in safe state";
}
else cout << "The system is in deadlock state!";
}
/*//... Sample Input-Output:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
3
0 0 0
5
T0 0 1 0 0 0 0
T1 2 0 0 2 0 2
T2 3 0 2 0 0 0
T3 2 1 1 1 0 0
T4 0 0 2 0 0 2
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
T0 T2 T3 T4 T1
No Deadlock. The system is in safe state
*///...