-
Notifications
You must be signed in to change notification settings - Fork 0
/
Banker_algo
70 lines (70 loc) · 1.04 KB
/
Banker_algo
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
#include<iostream>
using namespace std;
int main()
{
int nop;
cin>>nop;
int nor;
cin>>nor;
int allocation[nop][nor];
int max[nop][nor];
int need[nop][nor];
int available[nor];
char finish[nop];
int deadlock=0;
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
cin>>allocation[i][j];
}
}
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
cin>>max[i][j];
need[i][j] = max[i][j]- allocation[i][j];
}
}
for(int j=0;j<nor;j++)
cin>>available[j];
for(int j=0;j<nop;j++)
finish[j]='F';
int t=0;
int checker=0;
while(t<nop)
{
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
if(available[j]>=need[i][j] && finish[i]=='F')
checker=1;
else
checker=0;
}
if(checker==1)
{
finish[i] = 'T';
for(int k=0;k<nor;k++)
available[k] = available[k] + allocation[i][k];
}
}
t++;
}
int i=0;
for(i=0;i<nop;i++)
{
if(finish[i]=='F')deadlock=1;
}
if(deadlock==1)
{
cout<<"Not deadlock free. Unsafe state!";
}
else
{
cout<<"Deadlock free. Safe state!";
}
return 0;
}