-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankers.cpp
89 lines (84 loc) · 1.43 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
#include<iostream>
using namespace std;
int main()
{
int nop,nor;
int finish[nop],work[nor];
cout<<"enter the no of processes";
cin>>nop;
cout<<"enter the no of resources";
cin>>nor;
int avail[nor];
cout<<"enter the no of available resources of each type";
for(int i=0;i<nor;i++)
{
cin>>avail[i];
work[i]=avail[i];
}
cout<<"enter the maximum demand of each resource of each type";
int max[nop][nor];
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
cin>>max[i][j];
}
finish[i]=0;
}
cout<<"enter the allocated number of each resource of each type";
int alloc[nop][nor];
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
cin>>alloc[i][j];
}
}
cout<<"the need matrix is\n";
int need[nop][nor];
for(int i=0;i<nop;i++)
{
for(int j=0;j<nor;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
cout<<need[i][j]<<" ";
}
cout<<"\n";
}
int count=0,c=0;
while(c<nop)
{
for(int i=0;i<nop;i++)
{
count=0;
if(finish[i]==0)
{
int j;
for( j=0;j<nor;j++)
{
if(need[i][j]>work[j])
{
break;
}
//count++;
}
if(j==nor)
{
for(int k=0;k<nor;k++)
{
work[k]+=alloc[i][k];
}
finish[i]=1;
c++;
}
}
}
if(c!=nop)
{
cout<<"system is not in safe state";
return 0;
}
}
cout<<"system is not in safe state";
return 0;
}