-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path银行家算法.cpp
363 lines (353 loc) · 7.19 KB
/
银行家算法.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#define MAXPROCESS 50
#define MAXRESOURCE 100
int AVAILABLE[MAXRESOURCE];
int MAX[MAXPROCESS][MAXRESOURCE];
int ALLOCATION[MAXPROCESS][MAXRESOURCE];
int NEED[MAXPROCESS][MAXRESOURCE];
int REQUEST[MAXPROCESS][MAXRESOURCE];
bool FINISH[MAXPROCESS];
int p[MAXPROCESS];
int Work[MAXRESOURCE];
int m,n;
string showdata1[4] = { "max","allo","need","aval" };
string showdata2[5] = { "work","need","allo","w+al","finish" };
void iShow()
{
int size, size2;
cout << "系统";
for (int k = 0; k < 4; k++)
{
size = showdata1[k].length();
size2 = (n * 3 + 5 - size) / 2;
cout << setw(size2 + size) << showdata1[k] << setw(size2) << " ";
}
cout << endl;
cout << "资源";
for (int k = 0; k < 4; k++)
{
char sourcename = 'A';
cout << " ";
for (int kk = 0; kk < n; kk++)
{
cout << " " << sourcename;
sourcename++;
}
cout << " ";
}
cout << endl;
for (int ii = 0; ii < m; ii++)
{
cout << "p" << ii << " ";
for (int jj = 0; jj < n; jj++)
cout << setw(3) << MAX[ii][jj];
cout << " ";
for (int jj = 0; jj < n; jj++)
cout << setw(3) << ALLOCATION[ii][jj];
cout << " ";
for (int jj = 0; jj < n; jj++)
cout << setw(3) << NEED[ii][jj];
cout << " ";
if (ii == 0)
{
for (int iii = 0; iii < n; iii++)
cout << setw(3) << AVAILABLE[iii];
}
cout << " " << endl;
}
}
void fShow()
{
cout << "系统";
for (int k = 0; k < 5; k++)
{
int size = showdata2[k].length();
int size2 = (n*3+5 - size) / 2;
cout << setw(size2 + size) << showdata2[k] << setw(size2) << " ";
}
cout << endl;
cout << "资源";
for (int k = 0; k < 4; k++)
{
char sourcename = 'A';
cout << " ";
for (int kk = 0; kk < n; kk++)
{
cout << setw(3) << sourcename;
sourcename++;
}
cout << " ";
}
cout << endl;
}
void Init()
{
int i, j;
cout << "请输入进程的数目:";
cin >> m;
cout << "请输入资源的种类:";
cin >> n;
cout << "请输入每个进程最多所需的各资源数,按照" << m << "x" << n << "矩阵输入 "<< endl;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> MAX[i][j];
cout<<"请输入每个进程已分配的各资源数,也按照" << m << "x" << n << "矩阵输入" << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> ALLOCATION[i][j];
NEED[i][j] = MAX[i][j] - ALLOCATION[i][j];
if (NEED[i][j] < 0)
{
cout << "您输入的第" << i + 1 << "个进程所拥有的第" << j + 1 << "个资源数错误,请重新输入:" << endl;
j--;
continue;
}
}
}
cout << "请输入各个资源现有的数目:" << endl;
for (i = 0; i < n; i++)
{
cin >> AVAILABLE[i];
}
iShow();
}
bool Safe()
{
fShow();
int i, j, k, l = 0;
for (i = 0; i < n; i++)
Work[i] = AVAILABLE[i];
for (i = 0; i < m; i++)
{
FINISH[i] = false;
}
for (i = 0; i < m; i++)
{
if (FINISH[i] == true)
{
continue;
}
else
{
for (j = 0; j < n; j++)
{
if (NEED[i][j] > Work[j])
{
break;
}
}
if (j == n)
{
FINISH[i] = true;
cout << "P" << i << " ";
for (k = 0; k < n; k++)
cout << setw(3) << Work[k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << NEED[i][k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << ALLOCATION[i][k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << Work[k] + ALLOCATION[i][k];
cout << " ";
cout << setw(3) << "true" << endl;
for (k = 0; k < n; k++)
{
Work[k] += ALLOCATION[i][k];
}
p[l++] = i;
i = -1;
}
else
{
continue;
}
}
if (l == m)
{
cout << "系统是安全的" << endl;
cout << "安全序列" << endl;
for (i = 0; i < l; i++)
{
cout << p[i];
if (i != l - 1)
{
cout << "-->";
}
}
cout << "" << endl;
return true;
}
}
cout << "\n无法继续找到可满足的进程!" << endl;
cout << "系统是不安全的" << endl;
return false;
}
bool Bank1()
{
int i, cusneed;
char again;
while (1)
{
cout << "请输入要申请资源的进程号(注:第个进程号为,依次类推):p";
cin >> cusneed;
cout << "\n请输入进程所请求的各资源的数量" << endl;
for (i = 0; i < n; i++)
{
cin >> REQUEST[cusneed][i];
}
for (i = 0; i < n; i++)
{
int i, j, k, l = 0;
for (i = 0; i < n; i++)
Work[i] = AVAILABLE[i];
for (i = 0; i < m; i++)
{
FINISH[i] = false;
}
for (i = 0; i < m; i++)
{
if (FINISH[i] == true)
{
continue;
}
else
{
for (j = 0; j < n; j++)
{
if (NEED[i][j] > Work[j])
{
break;
}
}
if (j == n)
{
FINISH[i] = true;
cout << "P" << i << " ";
for (k = 0; k < n; k++)
cout << setw(3) << Work[k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << NEED[i][k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << ALLOCATION[i][k];
cout << " ";
for (k = 0; k < n; k++)
cout << setw(3) << Work[k] + ALLOCATION[i][k];
cout << " ";
cout << setw(3) << "true" << endl;
for (k = 0; k < n; k++)
{
Work[k] += ALLOCATION[i][k];
}
p[l++] = i;
i = -1;
}
else
{
continue;
}
}
if (l == m)
{
cout << "系统是安全的" << endl;
cout << "安全序列" << endl;
for (i = 0; i < l; i++)
{
cout << p[i];
if (i != l - 1)
{
cout << "-->";
}
}
cout << "" << endl;
return true;
}
}
cout << "\n无法继续找到可满足的进程!" << endl;
cout << "系统是不安全的" << endl;
return false;
}
}
}
void Bank2()
{
int i, cusneed;
char again;
while (1)
{
cout << "请输入要申请资源的进程号(注:第个进程号为,依次类推):p";
cin >> cusneed;
cout << "\n请输入进程所请求的各资源的数量" << endl;
for (i = 0; i < n; i++)
{
cin >> REQUEST[cusneed][i];
}
for (i = 0; i < n; i++)
{
if (REQUEST[cusneed][i] > NEED[cusneed][i])
{
cout << "您输入的请求数超过进程的需求量!请重新输入!" << endl;
continue;
}
if (REQUEST[cusneed][i] > AVAILABLE[i])
{
cout << "您输入的请求数超过系统有的资源数!请重新输入!" << endl;
continue;
}
}
for (i = 0; i < n; i++)
{
AVAILABLE[i] -= REQUEST[cusneed][i];
ALLOCATION[cusneed][i] += REQUEST[cusneed][i];
NEED[cusneed][i] -= REQUEST[cusneed][i];
}
if (Safe())
{
cout << "同意分配请求!" << endl;
}
else
{
cout << "您的请求被拒绝!" << endl;
for (i = 0; i < n; i++)
{
AVAILABLE[i] += REQUEST[cusneed][i];
ALLOCATION[cusneed][i] -= REQUEST[cusneed][i];
NEED[cusneed][i] += REQUEST[cusneed][i];
}
}
for (i = 0; i < m; i++)
{
FINISH[i] = false;
}
cout << "您还想再次请求分配吗?是请按y/Y,否请按其它键" << endl;
cin >> again;
if (again == 'y' || again == 'Y')
{
continue;
}
break;
}
}
int main()
{
while (true)
{
Init();
if (Safe()) break;
cout << "当前系统不安全,请重新初始化系统" << endl;
}
if (Bank1())
{
Bank2();
}
return 0;
}