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