-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankers.cpp
91 lines (75 loc) · 1.51 KB
/
bankers.cpp
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
#include<iostream>
using namespace std;
int alloc[50][50];
int maxi[50][50];
int need[50][50];
int avail[50];
int check_safety(int j,int nr)
{
for(int i=0;i<nr;i++)
{
if(need[j][i]>avail[i])
return 0;
}
return 1;
}
int check(bool a[],int n)
{
for(int i=0;i<n;i++)
{
if(a[i]==false)
return 0;
}
return 1;
}
int main()
{
int np=100;
int nr=100;
cout<<"\nEnter the no of processes : ";
cin>>np;
cout<<"\nEnter the no of resources : ";
cin>>nr;
cout<<"\nEnter the allocation data : \n";
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
cin>>alloc[i][j];
cout<<"\nEnter the requirement data : \n";
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
cin>>maxi[i][j];
//Calculation of need matrix
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
need[i][j]=maxi[i][j]-alloc[i][j];
cout<<"\nEnter the availability matrix : \n";
for(int i=0;i<nr;i++)
cin>>avail[i];
int ex_it=nr;
int flg;
bool completed[np];
while(10)
{
for(int i=0;i<np;i++)
{
if(!completed[i] && check_safety(i,nr))
{
for(int j=0;j<nr;j++)
avail[j]+=alloc[i][j];
}
completed[i]=true;
}
flg=check(completed,np);
ex_it--;
if(flg==1 || ex_it==0)
break;
}
cout<<"\nThe final availability matrix \n";
for(int i=0;i<nr;i++)
cout<<avail[i]<<" ";
cout<<"\n --------- Result ------------- \n";
if(flg==1)
cout<<"There is no deadlock";
else
cout<<"Sorry there is a possibility of deadlock";
return 0;