-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSafety_Algorithm.cpp
More file actions
170 lines (148 loc) · 3.9 KB
/
Copy pathSafety_Algorithm.cpp
File metadata and controls
170 lines (148 loc) · 3.9 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
/* Shabbir Mahmood ID: 14015439 Part-3 Even Operating System Lab Safety Algorithm (Banker Algorithm) Algorithm Date: 02-02-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 need[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_need_work(int p) // Checking Need[i] <= Work[i]
{
int cnt=0;//*************************
for(int i=0; i<no_resource; i++)
{
if(need[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_need_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)
{
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 Need Matrix is: "<<endl;
for(int i=0; i<no_process; i++)
{
for(int j=0; j<no_resource; j++)
{
need[i][j] = maxx[i][j] - allocation[i][j];
cout<<need[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<<"No Safe Sequence(Deadlock)"<<endl;
}
cout<<endl;
return 0;
}