银行家算法(Banker Algorithm)

This is famous banker’s algorithm. It is used in advoiding Deadlock.

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

#include<iostream>
using namespace std;
int **allocate,*total,**need,*require;
int num_process,num_resource;
bool isDeadLock(int c=0);
bool findFinish();
void printALL(int**b_allocate,int** b_need,int* b_total);
bool isAllFinish(int**);
int main()
{
cin >> num_process >> num_resource;
need = new int* [num_process];
allocate = new int*[num_process];
total = new int [num_resource];
require = new int[num_resource];
int i,q;
for(i=0;i<num_process;i++)
{
need[i] = new int[num_resource];
allocate[i] = new int[num_resource];
}
//define the number of each resource
for(i=0;i<num_resource;i++)
cin >> total[i];
for(i=0;i<num_resource;i++)
cout << total[i];
//define the processes get how much resource
for(i=0;i<num_process;i++)
for(q=0;q<num_resource;q++)
cin >> allocate[i][q];
//cheak the resource has enough to allocate
for(i=0;i<num_resource;i++)
for(q=0;q<num_process;q++)
{ if(total[i]-allocate[q][i]>=0)
total[i]-=allocate[q][i];
else
{
cerr << "error!";
return 1;
}
}

//define how much resources will each process still need to get to finish itself now
for(i=0;i<num_process;i++)
for(q=0;q<num_resource;q++)
cin >> need[i][q];
printALL(allocate,need,total);
//cheak the allocate is deadlock or not
if(isDeadLock())
{
cerr << "dead Lock" ;
return 1;
}
else
cout << "System is safe"<<endl;
//auto test
int test;
cout << "choose 1:auto test, else:custom test. ";
cin >> test;
if(test==1)
{
isDeadLock(test);
}
//custom test
else
{
while(isAllFinish(need))
{
for(i=0;i<num_process;i++)
{
cout << i<<": require how much resource?"<<endl;
for(q=0;q<num_resource;q++)
cin >> require[q];
//determine the data of require is right
for(q=0;q<num_resource;q++)
{
if(total[q] < require[q] || need[i][q] < require[q])
{
cout << "error data information"<<endl;
i--;
break;
}
total[q]-=require[q];
need[i][q]-=require[q];
allocate[i][q]+=require[q];
}
if(isDeadLock(1))
{
i--;
cout << "System is in deadlock state"<<endl;
}
else
cout << "System is safe......"<<endl;
}
}
}
return 0;
}
//if all the processes are finished,then return true;
bool isAllFinish(int** b_need)
{
int i,q;
for(i=0;i<num_process;i++)
for(q=0;q<num_resource;q++)
{
if(b_need[i][q]!=0)
return false;
}
return true;
}
//if it is deadlock ,then return true;else return false
bool isDeadLock(int c)//c is a flag to show the information or not.
{
int **backup_allocate,**backup_need,*backup_total;
backup_need = new int* [num_process];
backup_allocate = new int*[num_process];
backup_total = new int[num_resource];
int i,q;
for(i=0;i<num_process;i++)
{
backup_need[i] = new int[num_resource];
backup_allocate[i] = new int[num_resource];
}
for(i=0;i<num_process;i++)
for(q=0;q<num_resource;q++)
{
backup_need[i][q] = need[i][q];
backup_allocate[i][q] = allocate[i][q];
}
for(i=0;i<num_resource;i++)
backup_total[i] = total[i];
// return findFinish();
if(c!=0)
printALL(backup_allocate,backup_need,backup_total);
int flag=0,count=0;
while(!isAllFinish(backup_need))
{
count=0;
for(i=0;i<num_process;i++)
{ for(q=0;q<num_resource;q++)
{
if(backup_need[i][q]>backup_total[q])
{
flag = 0;
break;
}
flag=1;
}
// find a finishing process and collect the resource
if(flag==1)
{
count++;
for(q=0;q<num_resource;q++)
{
backup_total[q]+=backup_allocate[i][q];
backup_allocate[i][q]=0;
backup_need[i][q]=0;
}

}
printALL(backup_allocate,backup_need,backup_total);
}
// if(c!=0)
// printALL(backup_allocate,backup_need,backup_total);
//if no process is finished,it means no resource is collect once.Then the deadlock appears.
if(count==0)
{
delete []backup_total;
for(i=0;i<num_process;i++)
{ delete[]backup_allocate[i];
delete[]backup_need[i];
}
delete []backup_allocate;
delete []backup_need;
return true;
}
}

return false;
}
void printALL(int**b_allocate,int** b_need,int* b_total)
{
int i,q;
cout << "Allocate need"<<endl;
for(i=0;i<num_process;i++)
{
for(q=0;q<num_resource;q++)
{
cout << b_allocate[i][q]<<" ";
}
cout << " ";
for(q=0;q<num_resource;q++)
cout << b_need[i][q]<<" ";
cout << endl;
}
cout << "total"<<endl;
for(i=0;i<num_resource;i++)
cout << b_total[i]<<" ";
cout << endl;
}

/*
bool findFinish()
{
int i,q,flag=0,count=0;
for(i=0;i<num_process;i++)
{ for(q=0;q<num_resource;q++)
{
if(need[i][q]>total[q])
{
flag = 0;
break;
}
flag=1;
}
if(flag==1)
{
count++;
for(q=0;q<num_resource;q++)
{
total[q]+=allocate[i][q];
allocate[i][q]=0;
}
}
}
if(count!=0)
return true;
return false;
}*/