银行家算法java代码
生活随笔
收集整理的這篇文章主要介紹了
银行家算法java代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Scanner;public class banker {private int Process = 0; // 定義最大進程數目private int Resource = 0; // 定義最大資源類數private int Work[]; // 定義系統可提供給進程繼續運行所需的各類資源數目private int MAX[][]; // 定義進程最大資源需求private int Allocation[][]; // 定義進程當前已用資源數目private int need[][]; // 定義進程需要資源數目private int Request[][]; // 定義進程請求資源向量private boolean finish[];// 定義進程完成標志private int Available[];private int P[];private Scanner in = new Scanner(System.in); // 定義全局輸入流public void close() { in.close(); } // 構造函數,初始化各向量public banker() throws Exception {int i, j;System.out.print("請輸入當前進程的個數:");Process = in.nextInt();System.out.print("請輸入系統資源種類的類數:");Resource = in.nextInt(); // 初始化各個數組Available = new int[Resource];Work = new int[Resource];MAX = new int[Process][Resource];Allocation = new int[Process][Resource];need = new int[Process][Resource];Request = new int[Process][Resource];finish = new boolean[Process];P = new int[Process];System.out.println("請輸入每個進程最大資源需求量,按" + Process + "*" + Resource + "矩陣輸入:");for (i = 0; i < Process; i++) { System.out.print("請輸入P" + (i + 1) + "進程各類資源最大需求量:");for (j = 0; j < Resource; j++) MAX[i][j] = in.nextInt(); }System.out.println("請輸入每個進程已分配的各資源數,也按照" + Process + "*" + Resource + "矩陣輸入:");for (i = 0; i < Process; i++) { System.out.print("請輸入P" + (i + 1) + "進程各類資源已分配 的資源數:");for (j = 0; j < Resource; j++){ Allocation[i][j] = in.nextInt();need[i][j] = MAX[i][j] - Allocation[i][j];if (need[i][j] < 0) {System.out.println("您輸入的第" + (i + 1) + " 個進程所擁有的第" + (j + 1) + "個資源數錯誤,請重新輸入:");j--;continue; } } }System.out.print("請輸入系統各類資源可用的數目:");for (i = 0; i < Resource; i++) { Available[i] = in.nextInt(); } }public void Bank() throws Exception {int j, i;int tempAvailable[] = new int[Resource];int tempAllocation[] = new int[Resource];int tempNeed[] = new int[Resource];System.out.println("----------------------------------------------------------");System.out.print("請輸入要申請資源的進程號(當前共有" + Process+ "個進程,如為進程P1申請,請輸入1,以此類推)");i = in.nextInt() - 1;System.out.print("請輸入P" + (i + 1) + "進程申請的各資源的數量");for (j = 0; j < Resource; j++){ Request[i][j] = in.nextInt();}for (j = 0; j < Resource; j++){ if (Request[i][j] > need[i][j]){ System.out.println("您輸入的申請的資源數超過進程的需求量!請重新輸入!");continue;}if (Request[i][j] > Available[j]){ System.out.println("您輸入的申請數超過系統可用的資源數!請重新輸入!"); continue;}}for (j = 0; j < Resource; j++) { tempAvailable[j] = Available[j];tempAllocation[j] = Allocation[i][j];tempNeed[j] = need[i][j];Available[j] = Available[j] - Request[i][j];Allocation[i][j] = Allocation[i][j] + Request[i][j];need[i][j] = need[i][j] - Request[i][j]; }if (Safe()) {System.out.println("分配給P" + i + "進程成功!");System.out.print("分配前系統可用資源:");for (int k = 0; k < Resource; k++)System.out.print(tempAvailable[k] + " ");System.out.print("\n分配前進程P" + i + "各類資源已分配數量:");for (int k = 0; k < Resource; k++)System.out.print(tempAllocation[k] + " ");System.out.print("\n分配前進程P" + i + "各類資源需求數量:");for (int k = 0; k < Resource; k++)System.out.print(tempNeed[k] + " ");System.out.print("\n分配后系統可用資源: ");for (int k = 0; k < Resource; k++)System.out.print(Available[k] + " ");System.out.print("\n分配后進程P" + i + "各類資源已分配數量:");for (int k = 0; k < Resource; k++)System.out.print(Allocation[i][k] + " ");System.out.print("\n分配后進程P" + i + "各類資源需求數量:");for (int k = 0; k < Resource; k++)System.out.print(need[i][k] + " "); System.out.println(); }else { System.out.println("申請資源失敗!");for (j = 0; j < Resource; j++){ Available[j] = Available[j] + Request[i][j];Allocation[i][j] = Allocation[i][j] + Request[i][j];need[i][j] = need[i][j] + Request[i][j]; } }for (i = 0; i < Process; i++){ finish[i] = false; } } // 安全性算法public boolean Safe(){ int i, j, k, t = 0;Work = new int[Resource];for (i = 0; i < Resource; i++)Work[i] = Available[i];for (i = 0; i < Process; i++){ finish[i] = false; }for (i = 0; i < Process; i++){ if (finish[i] == true){ continue; } else{ for (j = 0; j < Resource; j++){ if (need[i][j] > Work[j]){ break;}}if (j == Resource){ finish[i] = true;for (k = 0; k < Resource; k++){ Work[k] += Allocation[i][k];}P[t++] = i;i = -1;}else { continue;}}if (t == Process) {System.out.print("當前系統是安全的,存在一安全序 列:");for (i = 0; i < t; i++){System.out.print("P" + P[i]);if (i != t - 1) {System.out.print("---"); }}System.out.println(); return true; }}System.out.println("當前系統是不安全的,不存在安全序列");return false; }public static void main(String[] args) {try { banker b = new banker();b.Safe();for (int i = 0; i < 200; i++)b.Bank();b.close(); }catch (Exception e) {}}
}
總結
以上是生活随笔為你收集整理的银行家算法java代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 存储管理算法java代码
- 下一篇: 进程调度算法Java代码