-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9BANKERS_ALGORITHM.cxx
153 lines (134 loc) · 3.07 KB
/
9BANKERS_ALGORITHM.cxx
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
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int p,r,i,j;
int allocated[20][20];
int max[20][20];
int needed[20][20];
int available[20];
int total_allocated[20];
int total_available[20];
int flag[20];
cout<<"\nEnter number of process and number of resources ";
cin>>p;
cin>>r;
//TELLING COMPILER THAT THIS PROCESS IS NOT EXECUTED YET
for(i=0;i<p;i++)
flag[i]=0;
cout<<"\nEnter total available resources of each type ";
for(i=0;i<r;i++)
cin>>total_available[i];
cout<<"\nEnter data of resources each process allocated with ";
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cin>>allocated[i][j];
}
}
cout<<"\nEnter data of resources each process needs maximum ";
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cin>>max[i][j];
}
}
//CALCULATING NEED MATRIX
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
needed[i][j]=max[i][j]-allocated[i][j];
}
}
cout<<"\n THE NEED MATRIX IS\n";
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<needed[i][j]<<"\t";
}
cout<<"\n";
}
int sum;
int k=0;
while(k<r)
{
sum=0;
for(i=0;i<p;i++)
{
sum=sum+allocated[i][k];
}
total_allocated[k]=sum;
k++;
}
cout<<"\nTotal number of resources allocated is ";
for(i=0;i<r;i++)
cout<<total_allocated[i]<<'\t';
cout<<"\nTotal resources available is ";
for(i=0;i<r;i++)
{
available[i]=total_available[i]-total_allocated[i];
cout<<available[i]<<"\t";
}
queue<int>sequence;
int count =0;
while(count!=p)
{
for(i=0;i<p;i++)
{
int k=0;
if(flag[i]==0)
{
int nota=1;
while(k<r)
{
if(needed[i][k]>available[k])
{
nota=0;
break;
}
k++;
}
if(nota==1)
{
k=0;
while(k<r)
{
available[k]=available[k]+allocated[i][k];
k++;
}
flag[i]=1;
sequence.push(i);
break; //NOT REQUIRED JUST WRITTEN FOR START EXECUTION FROM UPWARDS ALWAYS
}
}
}
count++;
}
int safe=1;
for(i=0;i<p;i++)
{
if(flag[i]==0)
safe=0;
}
if(safe==1)
{
cout<<"\nONE OF THE SAFE SEQUENCE IS ";
while(!sequence.empty())
{
cout<<sequence.front()<<" ";
sequence.pop();
}
cout<<"\nAT LAST AVAILABLE MATRIX IS ";
for(i=0;i<r;i++)
cout<<available[i]<<"\t";
}
else
{
cout<<"\nSAFE SEQUENCE COULD NOT BE FOUND";
}
}