银行家算法代码

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
package 操作系统;

import java.util.Arrays;
import java.util.Scanner;

public class 课程设计银行家算法 {
static Scanner in = new Scanner(System.in);
static final int N = 6, M = 6;
static int Max[][] = new int[N][M];//进程最大所需资源矩阵
static int Allocation[][] = new int[N][M];//已分配资源矩阵
static int Need[][] = new int[N][M];//进程还需要到资源分配矩阵
static int Available[] = new int[M];//当前所拥有资源矩阵
static int safePath[] = new int[N];//保存安全序列

static int Request[] = new int[M];//请求资源向量
static boolean st[] = new boolean[5];//标记各个输入是否已经输入,nm,Max,Allocation,Need,Available
static int n, m;//记录进程数和每个进程需要的资源数量

static void myInformation() {
System.out.printf("姓名:%s 班级:%s 学号:%s\n", "朱浩然", "20计科8班", "20220310835");
}

static void menu() {
System.out.println("1.输入进程数量与每个资源所需的资源");
System.out.println("2.输入Max矩阵");
System.out.println("3.输入Allocation矩阵");
System.out.println("4.输出Max矩阵");
System.out.println("5.输出Allocation矩阵");
System.out.println("6.用Max矩阵和Allocation构造Need矩阵");
System.out.println("7.输出Need矩阵");
System.out.println("8.输入Available");
System.out.println("9.输出Available");
System.out.println("10.判断系统是否安全");
System.out.println("11.执行银行家算法,申请资源");
System.out.println("12.重置所有内容");
System.out.println("13.打印进程状态表");
System.out.println("14.打印安全序列");
System.out.println("15.重新打印选项菜单");
System.out.println("16.退出");
}

static void inputMatrix(int a[][], int n, int m) {//输入矩阵
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = in.nextInt();
}
}
}

static void outputMatrix(int a[][], int n, int m) {//输出矩阵
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}

static void inputVector(int a[], int m) {//输入向量
for (int i = 0; i < m; i++) {
a[i] = in.nextInt();
}
}

static void outputVector(int a[], int m) {//输出向量
for (int i = 0; i < m; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}

static void initNeed(int Max[][], int Allocation[][], int n, int m) {//初始化Need数组
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
}

static boolean check(int work[], int i) {//判断该进程是否可以被完全分配资源
for (int j = 0; j < m; j++) {
if (work[j] < Need[i][j]) return false;
}
return true;
}

static boolean is_OK() {//寻找安全序列,验证安全性算法
int work[] = new int[M];
for (int i = 0; i < M; i++) work[i] = Available[i];//将可使用数组赋值给work
boolean vis[] = new boolean[N];//判断哪个进程被使用
Arrays.fill(vis, false);
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 0; j < n; j++) {
if (!vis[j] && check(work, j)) {//如果该进程未被使用,且可以满足
t = j;
break;
}
}
if (t == -1) {
System.out.println("系统不安全!");
return false;
}
vis[t] = true;
safePath[i] = t;
for (int j = 0; j < m; j++) {
work[j] += Allocation[t][j];//加上该进程已分配的资源
}
}

return true;
}

static void solve() {//银行家算法
System.out.println("请输入你要请求资源的进程");
int processId = in.nextInt();
System.out.println("请输入你想要请求的资源");
for (int i = 0; i < m; i++) {
Request[i] = in.nextInt();
}
int t = processId;
System.out.println("进程" + t + "执行中");
for (int j = 0; j < m; j++) {
if (Request[j] > Need[t][j]) {
System.out.println("申请资源大于进程所需资源");
return;
}
if (Available[j] < Request[j]) {
System.out.println("申请资源大于系统所需资源");
return;
}
}
for (int j = 0; j < m; j++) {
Available[j] = Available[j] - Request[j];//
Allocation[t][j] = Allocation[t][j] + Request[j];//已分配资源+新分配资源
Need[t][j] = Need[t][j] - Request[j];//进程得到了一部分资源,所需资源减少
}
boolean flag = is_OK();
if (!flag) {
System.out.println("分配资源失败,退出本次分配");
for (int j = 0; j < m; j++) {//撤销已分配
Available[j] = Available[j] + Request[j];
Allocation[t][j] = Allocation[t][j] - Request[j];
Need[t][j] = Need[t][j] + Request[j];
}
return;
}
System.out.println("进程" + t + "执行完毕");

int i = 0;
for (; i < m; i++) {//如果一个进程所有请求被满足,释放其所有资源
if (Need[t][i] != 0) {
break;
}
}
if (i == m) {//进程执行完毕
for (int j = 0; j < m; j++) {
Available[j] += Allocation[t][j];//进程执行完成,释放其资源
}
}
}

static void display() {//打印进程需求表
int tmp = 0;
System.out.printf("|—————————————————————————————————————————-|\n");
System.out.printf("%-5s%-19s%-19s%-19s%-20s", "|进\\资源", "| Max ", "| Allocation ", "| Need ", "| Available |\n");
System.out.printf("| \\情 |—————————|—————————|—————————|—————————|\n");
System.out.printf("|程 \\况");
for (int i = 0; i < 4; i++) {
System.out.printf("|");
for (int j = 0; j < m; j++) {
System.out.printf(" %s ", (char) ('A' + j));
}
tmp = (M - m) * 3;
printVoid(tmp, ' ');
}
System.out.printf("|\n");
System.out.printf("|-------|—————————|—————————|—————————|—————————|\n");

for (int i = 0; i < n; i++) {
System.out.printf("| P%-3d |", i);
printProcessTableMatrix(Max, i);
printProcessTableMatrix(Allocation, i);
printProcessTableMatrix(Need, i);
printProcessTableVector(Available);

System.out.printf("|\n");
System.out.printf("|-------|—————————|—————————|—————————|—————————|\n");
}

}

static void printProcessTableMatrix(int a[][], int i) {//打印表格中的矩阵数据
for (int k = 0; k < m; k++) {
System.out.printf(" %d ", a[i][k]);
}
printVoid((M - m) * 3, ' ');
System.out.printf("|");
}

static void printProcessTableVector(int a[]) {//打印Available数据
for (int j = 0; j < m; j++) {
System.out.printf(" %d ", Available[j]);
}
printVoid((M - m) * 3, ' ');
}

static void printVoid(int s, char ch) {//用来循环打印空格
while (s-- > 0) {
System.out.printf("%s", ch);
}

}

static void clear() {//重置
n = 0;
m = 0;
for (int i = 0; i < N; i++) {
Arrays.fill(Max[i], 0);
Arrays.fill(Allocation[i], 0);
Arrays.fill(Need[i], 0);
}
Arrays.fill(Available, 0);
Arrays.fill(st, false);
}

public static void main(String[] args) {
myInformation();
menu();
int op = 0;
while (true) {
System.out.println("请选择:");
op = in.nextInt();
if (op == 1) {
System.out.println("请输入进程数n和每个进程所需要的资源种类m");
n = in.nextInt();
m = in.nextInt();
System.out.println("输入成功");
st[0] = true;
} else if (op == 2) {
if (!st[0]) {
System.out.println("进程数n和每个进程所需要的资源种类m未输入");
return;
}
System.out.printf("请输入%d行%d列的Max矩阵\n", n, m);
inputMatrix(Max, n, m);//输入
st[1] = true;
System.out.println("输入成功");
} else if (op == 3) {
if (!st[1]) {
System.out.println("Max矩阵未被初始化");
continue;
}
System.out.printf("请输入%d行%d列的Allocation矩阵\n", n, m);
inputMatrix(Allocation, n, m);//输入
st[2] = true;
System.out.println("输入成功");
} else if (op == 4) {
if (!st[1]) {
System.out.println("Max矩阵未被初始化");
continue;
}
System.out.println("输出Max矩阵");
outputMatrix(Max, n, m);
} else if (op == 5) {
if (!st[2]) {
System.out.println("Allocation矩阵未被初始化");
continue;
}
System.out.println("输出Allocation矩阵");
outputMatrix(Allocation, n, m);
} else if (op == 6) {
if (!st[1] || !st[2]) {
System.out.println("Max矩阵或Allocation矩阵还未初始化");
continue;
}
initNeed(Max, Allocation, n, m);//初始化Need矩阵
st[3] = true;
System.out.println("Need矩阵初始化成功");
} else if (op == 7) {
if (!st[3]) {
System.out.println("Need矩阵未初始化");
continue;
}
System.out.println("输出Need矩阵");
outputMatrix(Need, n, m);
} else if (op == 8) {
System.out.println("输入Available序列");
inputVector(Available, m);
st[4] = true;
System.out.println("输入成功");
} else if (op == 9) {
if (!st[4]) {
System.out.println("Available未被初始化");
continue;
}
System.out.println("输出Available矩阵");
outputVector(Available, m);
} else if (op == 10) {
if (!(st[0] && st[1] && st[2] && st[3] && st[4])) {
System.out.println("前置资源未初始化完毕");
continue;
}
boolean flag = is_OK();
if (flag) {
System.out.println("系统安全");
}
} else if (op == 11) {
if (!(st[0] && st[1] && st[2] && st[3] && st[4])) {
System.out.println("前置资源未初始化完毕");
continue;
}
solve();
} else if (op == 12) {
clear();
System.out.println("重置成功");
} else if (op == 13) {
if (!(st[0] && st[1] && st[2] && st[3] && st[4])) {
System.out.println("前置资源未初始化完毕");
continue;
}
display();
} else if (op == 14) {
System.out.println("打印安全序列");
boolean flag = is_OK();
if (flag) {
System.out.println("安全序列为:");
outputVector(safePath, n);
}
} else if (op == 15) {
menu();
} else if (op == 16) {
System.out.println("退出成功");
break;
} else {
System.out.println("请选择合法操作");
}
}
}
}