Java实现银行家算法
原理代码测试结果原理
设 Request;是进程Pi的请求向量,如果 Requesti[j] = K,表示进程Pi需要K个Rj类型的资源。当Pi发出资源请求后,系统按下述步骤进行检査:
(1) 如果 Requesti[j] ≤ Need[i,j]便转向步骤(2);否则认为出错,因为它所需要的资源数已超过它所宣布的最大值。
(2) 如果 Requesti[j] ≤ Available[j],便转向步骤(3);否则,表示尚无足够资源,Pi须等待。
(3) 系统试探着把资源分配给进程Pi,并修改下面数据结构中的数值
Available[i] = Available[j] - Requesti[j];
Allocation[i,j] = Allocation[i,j] + Requesti[j];
Need[i,j] = Need[i,j] - Requesti[j];
(4) 系统执行安全性算法,检查此次资源分配后系统是否处于安全状态。若安全,才正式将资源分配给进程Pi,以完成本次分配;否则,将本次的试探分配作废,恢复原来的资源分配状态,让进程Pi等待
代码
/*** Copyright (c).-,* Filename: BankerAlgorithm1* Author: * Date:/11/6* Description:*/import java.util.Scanner;class Banker1 {int[] num_source;//银行家所拥有的资源数int[][] max_need_couse;//各进程所需要各资源的最大量int[][] have_num;//各进程已拥有各资源的量int[][] need_num;//各进程仍需要各资源的量int[] p;//已分配出的资源数int[] s;//系统剩余资源数public Banker1(int[] a,int[][]b,int [][]c,int[][]d,int[]p,int[] s){this.num_source = a;this.max_need_couse = b;this.have_num = c;this.need_num = d;this.p = p;this.s = s;}public void display(){for (int i = 0;i<need_num.length;i++){System.out.println("进程"+i+"仍需要各资源的量为:");for(int j= 0;j<need_num[i].length;j++){System.out.print(need_num[i][j]+" ");}System.out.println();}System.out.println("已分配出的资源数:");for(int i = 0;i<p.length;i++){System.out.print(p[i]+" ");}System.out.println();System.out.println("各剩余的资源数:");for(int i= 0;i<s.length;i++){System.out.print(s[i]+" ");if(s[i]<0){System.out.println("不安全");BankerAlgorithm1.set();}}System.out.println();}public int algorithm(int n,int m,int a){//银行家算法// boolean t = false;if(n<max_need_couse.length && m<need_num[0].length && a<=need_num[n][m]) {have_num[n][m] += a;need_num[n][m] -= a;for (int i = 0; i < need_num.length; i++) {for (int j = 0; j < need_num[i].length; j++) {if (need_num[i][j] > s[j]) {continue;}}//t = true;return i;}have_num[n][m] -= a;need_num[n][m] += a;return -1;}else {return -2;}}public boolean isNotEnd(int i){if(i<max_need_couse.length) {for (int j = 0; j < max_need_couse[0].length; j++) {if (have_num[i][j] != max_need_couse[i][j]) {return false;}}return true;}return false;}public boolean anquan(){for(int i = 0;i<max_need_couse.length;i++){for(int j = 0;j<max_need_couse[0].length;j++){if(s[j]<need_num[i][j]){continue;}}return true;}return false;}}public class BankerAlgorithm1 {public static void set(){Scanner sc = new Scanner(System.in);System.out.println("请输入资源数");int a = sc.nextInt();System.out.println("请输入进程数");int b = sc.nextInt();int []source = new int [a];//资源数System.out.println("请分别输入各资源的拥有总量");for (int i = 0;i<a;i++) {//System.out.println("请输入资源"+i+"的拥有总量:");int a_i = sc.nextInt();source[i] = a_i;}int [][]couse = new int[b][a];//进程对资源的最大需求量for (int i = 0;i<b;i++){System.out.println("请分别输入进程"+i+"对各资源的需求总量");for(int j = 0;j<a;j++){//System.out.println("请输入进程"+i+"对资源"+j+"的最大需求量:");int b_ij = sc.nextInt();couse[i][j] = b_ij;}}int [][]couse1 = new int [b][a];for (int i = 0;i<b;i++){System.out.println("请分别输入进程"+i+"各资源的已分配量");for(int j = 0;j<a;j++){//System.out.println("请输入进程"+i+"对资源"+j+"的已分配量:");int b_ij = sc.nextInt();couse1[i][j] = b_ij;}}int [][]couse2 = new int [b][a];for (int i = 0;i<b;i++){for(int j = 0;j<a;j++){couse2[i][j] = couse[i][j]-couse1[i][j];}}int []p = new int[a];for(int i = 0;i<a;i++){for(int j = 0;j<b;j++){p[i] += couse1[j][i];}}int []s = new int[a];for(int i = 0;i<a;i++){s[i] = source[i]-p[i];}Banker1 b1 = new Banker1(source,couse,couse1,couse2,p,s);b1.display();while (b!=0){if(!b1.anquan()){System.out.println("此系统不安全!");set();}System.out.println("请输入要操作的进程 资源号申请的资源数");int nc = sc.nextInt();int ns = sc.nextInt();int num = sc.nextInt();if(b1.algorithm(nc,ns,num)>=0){System.out.println("申请成功");}else if(b1.algorithm(nc,ns,num)==-1){System.out.println("操作不安全,已予以撤销!");}else if(b1.algorithm(nc,ns,num)==-2){System.out.println("操作不合法");}System.out.println("操作结束后的需求量");for(int i = 0;i<b;i++){System.out.print("进程"+i+":");for(int j = 0;j<a;j++){System.out.print(b1.need_num[i][j]+" ");}System.out.println();}System.out.println("操作结束后的已获得量");for(int i = 0;i<b;i++){System.out.print("进程"+i+":");for(int j = 0;j<a;j++){System.out.print(b1.have_num[i][j]+" ");}System.out.println();}if(b1.isNotEnd(nc)) {System.out.println("进程"+nc+"运行结束");b--;for(int i = 0;i<b1.max_need_couse[nc].length;i++){b1.num_source[i]+=b1.max_need_couse[nc][i];}}}if(b == 0){System.out.println("运行完毕");}}public static void main(String[] args) {set();}}