欢迎光临散文网 会员登陆 & 注册

,,,

2023-06-12 21:08 作者:乔丹GOAT_  | 我要投稿

import java.util.Scanner; public class BankerAlgorithm { private int[] available; // 可用资源数组 private int[][] maximum; // 最大需求矩阵 private int[][] allocation; // 分配矩阵 private int[][] need; // 需求矩阵 private int numberOfProcesses; // 进程数量 private int numberOfResources; // 资源数量 public void init() { Scanner scanner = new Scanner(System.in); System.out.print("请输入资源的数量: "); numberOfResources = scanner.nextInt(); System.out.print("请输入进程的数量: "); numberOfProcesses = scanner.nextInt(); available = new int[numberOfResources]; maximum = new int[numberOfProcesses][numberOfResources]; allocation = new int[numberOfProcesses][numberOfResources]; need = new int[numberOfProcesses][numberOfResources]; // 输入可用资源数量 System.out.println("请输入可用资源数量:"); for (int i = 0; i < numberOfResources; i++) { System.out.print("资源" + i + "的数量: "); available[i] = scanner.nextInt(); } // 输入最大需求矩阵 System.out.println("请输入各进程的最大需求矩阵的值[Max]:"); for (int i = 0; i < numberOfProcesses; i++) { System.out.println("进程" + i + "的最大需求:"); for (int j = 0; j < numberOfResources; j++) { System.out.print("资源" + j + "的最大需求: "); maximum[i][j] = scanner.nextInt(); } } // 输入已分配资源矩阵,并计算需求矩阵 System.out.println("请输入各进程已经分配的资源量[Allocation]:"); for (int i = 0; i < numberOfProcesses; i++) { System.out.println("进程" + i + "的已分配资源:"); for (int j = 0; j < numberOfResources; j++) { System.out.print("资源" + j + "的已分配数量: "); allocation[i][j] = scanner.nextInt(); need[i][j] = maximum[i][j] - allocation[i][j]; } } } public void showData() { System.out.println("系统可用资源[Available]:"); for (int i = 0; i < numberOfResources; i++) { System.out.print("资源" + i + ": " + available[i] + " "); } System.out.println(); System.out.println("系统当前的资源分配情况如下:"); System.out.println(" Max Allocation Need"); System.out.print("进程名 "); // 输出与进程名同行的资源名,Max、Allocation、Need下分别对应 for (int j = 0; j < 3; j++) { for (int i = 0; i < numberOfResources; i++) { System.out.print("资源" + i + " "); } System.out.print(" "); } System.out.println(); // 输出每个进程的Max、Allocation、Need for (int i = 0; i < numberOfProcesses; i++) { System.out.print("进程" + i + " "); for (int j = 0; j < numberOfResources; j++) { System.out.print(maximum[i][j] + " "); } System.out.print(" "); for (int j = 0; j < numberOfResources; j++) { System.out.print(allocation[i][j] + " "); } System.out.print(" "); for (int j = 0; j < numberOfResources; j++) { System.out.print(need[i][j] + " "); } System.out.println(); } } public boolean isSafeState() { int[] work = new int[numberOfResources]; // 工作数组,表示系统可提供给进程的各类资源数量 int[] finish = new int[numberOfProcesses]; // 标记系统是否有足够的资源分配给各个进程 int[] safeSequence = new int[numberOfProcesses]; // 存放安全序列 // 初始化work和finish数组 for (int i = 0; i < numberOfResources; i++) { work[i] = available[i]; } for (int i = 0; i < numberOfProcesses; i++) { finish[i] = 0; } int count = 0; // 完成进程的计数 while (count < numberOfProcesses) { boolean found = false; for (int i = 0; i < numberOfProcesses; i++) { if (finish[i] == 0) { boolean canAllocate = true; for (int j = 0; j < numberOfResources; j++) { if (need[i][j] > work[j]) { canAllocate = false; break; } } if (canAllocate) { // 分配资源 for (int j = 0; j < numberOfResources; j++) { work[j] += allocation[i][j]; } safeSequence[count] = i; finish[i] = 1; count++; found = true; } } } if (!found) { return false; // 没有找到满足条件的进程 } } System.out.println("系统安全!"); System.out.print("存在一个安全序列:"); for (int i = 0; i < numberOfProcesses; i++) { System.out.print("P" + safeSequence[i]); if (i < numberOfProcesses - 1) { System.out.print(" -> "); } } System.out.println(); return true; } public void handleResourceRequest() { Scanner scanner = new Scanner(System.in); System.out.print("请输入请求分配资源的进程号(0~" + (numberOfProcesses - 1) + "): "); int processId = scanner.nextInt(); System.out.println("请输入进程P" + processId + "要申请的资源个数:"); int[] request = new int[numberOfResources]; for (int i = 0; i < numberOfResources; i++) { System.out.print("资源" + i + ": "); request[i] = scanner.nextInt(); } // 判断请求是否合理 boolean isValidRequest = true; for (int i = 0; i < numberOfResources; i++) { if (request[i] > need[processId][i]) { System.out.println("进程P" + processId + "申请的资源大于该进程还需要的资源量,分配不合理,不予分配!"); isValidRequest = false; break; } else if (request[i] > available[i]) { System.out.println("进程P" + processId + "申请的资源大于系统现在可利用的资源量,系统尚无足够资源,不予分配!"); isValidRequest = false; break; } } if (isValidRequest) { // 尝试分配资源 for (int i = 0; i < numberOfResources; i++) { available[i] -= request[i]; allocation[processId][i] += request[i]; need[processId][i] -= request[i]; } showResourceAllocation(); if (!isSafeState()) { // 回滚分配 for (int i = 0; i < numberOfResources; i++) { available[i] += request[i]; allocation[processId][i] -= request[i]; need[processId][i] += request[i]; } showResourceAllocation(); } } } public void run() { Scanner scanner = new Scanner(System.in); String choice; System.out.println("银行家算法的实现"); System.out.print("系统可用资源种类为: "); numberOfResources = scanner.nextInt(); available = new int[numberOfResources]; for (int i = 0; i < numberOfResources; i++) { System.out.print("资源" + i + "名称为: "); NAME[i] = scanner.next(); System.out.print("资源" + NAME[i] + "初始化个数为: "); available[i] = scanner.nextInt(); } System.out.print("请输入进程的数量: "); numberOfProcesses = scanner.nextInt(); maximum = new int[numberOfProcesses][numberOfResources]; allocation = new int[numberOfProcesses][numberOfResources]; need = new int[numberOfProcesses][numberOfResources]; System.out.println("请输入各进程的最大需求矩阵的值[Max]:"); boolean flag; do { flag = false; for (int i = 0; i < numberOfProcesses; i++) { for (int j = 0; j < numberOfResources; j++) { maximum[i][j] = scanner.nextInt(); if (maximum[i][j] > available[j]) { flag = true; } } } if (flag) { System.out.println("资源最大需求量大于系统中资源最大量,请重新输入!"); } } while (flag); do { flag = false; System.out.println("请输入各进程已经分配的资源量[Allocation]:"); for (int i = 0; i < numberOfProcesses; i++) { for (int j = 0; j < numberOfResources; j++) { allocation[i][j] = scanner.nextInt(); if (allocation[i][j] > maximum[i][j]) { flag = true; } need[i][j] = maximum[i][j] - allocation[i][j]; } } if (flag) { System.out.println("分配的资源大于最大量,请重新输入!"); } } while (flag); showData(); isSafeState(); do { System.out.println("请选择操作:"); System.out.println("1. 请求分配资源"); System.out.println("2. 退出"); System.out.print("输入您的选择: "); choice = scanner.next(); switch (choice) { case "1": handleResourceRequest(); break; case "2": System.out.println("退出系统!"); break; default: System.out.println("无效的选择,请重新输入!"); } } while (!choice.equals("2")); } public static void main(String[] args) { BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(); bankerAlgorithm.run(); } }

,,,的评论 (共 条)

分享到微博请遵守国家法律