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]; 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) { 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]; 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[]) { 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); 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("请选择合法操作"); } } } }
|