,,,
import java.util.Scanner; public class BankerAlgorithm { private int[] available; // 可用資源數(shù)組 private int[][] maximum; // 最大需求矩陣 private int[][] allocation; // 分配矩陣 private int[][] need; // 需求矩陣 private int numberOfProcesses; // 進(jìn)程數(shù)量 private int numberOfResources; // 資源數(shù)量 public void init() { Scanner scanner = new Scanner(System.in); System.out.print("請輸入資源的數(shù)量: "); numberOfResources = scanner.nextInt(); System.out.print("請輸入進(jìn)程的數(shù)量: "); numberOfProcesses = scanner.nextInt(); available = new int[numberOfResources]; maximum = new int[numberOfProcesses][numberOfResources]; allocation = new int[numberOfProcesses][numberOfResources]; need = new int[numberOfProcesses][numberOfResources]; // 輸入可用資源數(shù)量 System.out.println("請輸入可用資源數(shù)量:"); for (int i = 0; i < numberOfResources; i++) { System.out.print("資源" + i + "的數(shù)量: "); available[i] = scanner.nextInt(); } // 輸入最大需求矩陣 System.out.println("請輸入各進(jìn)程的最大需求矩陣的值[Max]:"); for (int i = 0; i < numberOfProcesses; i++) { System.out.println("進(jìn)程" + i + "的最大需求:"); for (int j = 0; j < numberOfResources; j++) { System.out.print("資源" + j + "的最大需求: "); maximum[i][j] = scanner.nextInt(); } } // 輸入已分配資源矩陣,并計算需求矩陣 System.out.println("請輸入各進(jìn)程已經(jīng)分配的資源量[Allocation]:"); for (int i = 0; i < numberOfProcesses; i++) { System.out.println("進(jìn)程" + i + "的已分配資源:"); for (int j = 0; j < numberOfResources; j++) { System.out.print("資源" + j + "的已分配數(shù)量: "); allocation[i][j] = scanner.nextInt(); need[i][j] = maximum[i][j] - allocation[i][j]; } } } public void showData() { System.out.println("系統(tǒng)可用資源[Available]:"); for (int i = 0; i < numberOfResources; i++) { System.out.print("資源" + i + ": " + available[i] + " "); } System.out.println(); System.out.println("系統(tǒng)當(dāng)前的資源分配情況如下:"); System.out.println(" Max Allocation Need"); System.out.print("進(jìn)程名 "); // 輸出與進(jìn)程名同行的資源名,Max、Allocation、Need下分別對應(yīng) for (int j = 0; j < 3; j++) { for (int i = 0; i < numberOfResources; i++) { System.out.print("資源" + i + " "); } System.out.print(" "); } System.out.println(); // 輸出每個進(jìn)程的Max、Allocation、Need for (int i = 0; i < numberOfProcesses; i++) { System.out.print("進(jìn)程" + 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]; // 工作數(shù)組,表示系統(tǒng)可提供給進(jìn)程的各類資源數(shù)量 int[] finish = new int[numberOfProcesses]; // 標(biāo)記系統(tǒng)是否有足夠的資源分配給各個進(jìn)程 int[] safeSequence = new int[numberOfProcesses]; // 存放安全序列 // 初始化work和finish數(shù)組 for (int i = 0; i < numberOfResources; i++) { work[i] = available[i]; } for (int i = 0; i < numberOfProcesses; i++) { finish[i] = 0; } int count = 0; // 完成進(jìn)程的計數(shù) 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; // 沒有找到滿足條件的進(jìn)程 } } System.out.println("系統(tǒng)安全!"); 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("請輸入請求分配資源的進(jìn)程號(0~" + (numberOfProcesses - 1) + "): "); int processId = scanner.nextInt(); System.out.println("請輸入進(jìn)程P" + processId + "要申請的資源個數(shù):"); 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("進(jìn)程P" + processId + "申請的資源大于該進(jìn)程還需要的資源量,分配不合理,不予分配!"); isValidRequest = false; break; } else if (request[i] > available[i]) { System.out.println("進(jìn)程P" + processId + "申請的資源大于系統(tǒng)現(xiàn)在可利用的資源量,系統(tǒng)尚無足夠資源,不予分配!"); 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("銀行家算法的實現(xiàn)"); System.out.print("系統(tǒng)可用資源種類為: "); 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] + "初始化個數(shù)為: "); available[i] = scanner.nextInt(); } System.out.print("請輸入進(jìn)程的數(shù)量: "); numberOfProcesses = scanner.nextInt(); maximum = new int[numberOfProcesses][numberOfResources]; allocation = new int[numberOfProcesses][numberOfResources]; need = new int[numberOfProcesses][numberOfResources]; System.out.println("請輸入各進(jìn)程的最大需求矩陣的值[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("資源最大需求量大于系統(tǒng)中資源最大量,請重新輸入!"); } } while (flag); do { flag = false; System.out.println("請輸入各進(jìn)程已經(jīng)分配的資源量[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("退出系統(tǒng)!"); break; default: System.out.println("無效的選擇,請重新輸入!"); } } while (!choice.equals("2")); } public static void main(String[] args) { BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(); bankerAlgorithm.run(); } }