Package Cn.hncu.mytext;import Java.util.scanner;public class Bankerofalgorithm {//bankerofalgorithm banker algorithm int Max[][]; int allocation[][];int need[][];int Available[];int work[]; String name[];int Temp[];int S = P = 100;int safequeue[];int request[]; Scanner sc;public bankerofalgorithm () {int text_temp = 100;int max[][] = new Int[text_temp][text_temp];int Allocation[][] = new Int[text_temp][text_temp];int need[][] = new Int[text_temp][text_temp];int available[] = new Int[text_temp];int Wor k[] = new Int[text_temp]; String name[] = new String[text_temp];int temp[] = new Int[text_temp];int S =, P = 100;int safequeue[] = new Int[text_ Temp];int request[] = new Int[text_temp];} public void Showdata () {int I, j, K, L; System.out.println ("\ t resource allocation status \ n"); System.out.println ("\tmax\t has been assigned \tneed"); System.out.print ("\ T"), for (j = 0, J < 3; J + +) {for (i = 0; i < S; i++) {System.out.print (Name[i] + "");} System.out.print ("\ t");} System.out.println (); for (i = 0; i < P; i++) {System.out.print (i+"\ t"); for (j = 0; J < S; J + +) {System.out.print (Max[i][j] + "");} System.out.print ("\ t"); for (k = 0; k < S; k++) {System.out.print (Allocation[i][k] + "");} System.out.print ("\ t"); for (l = 0; l < S; l++) {System.out.print (Need[i][l] + "");} System.out.println ();} System.out.println ("\navailable"); for (i = 0; i < S; i++) {System.out.print (Name[i] + "");} System.out.println (); for (i = 0; i < S; i++) {System.out.print (Available[i] + "");} System.out.println ();} public int Judgesafe () {int[][] tempwork = new Int[100][100];int I, x, k = 0, M, apply;int[] Finish = new int[100];int[] t EMP = new Int[100];int J;int flag = 0; Work = new Int[100];for (i = 0; i < S; i++) {work[i] = available[i];} for (i = 0; i < P; i++) {apply = 0;for (j = 0; J < S; J + +) {if (finish[i] = = 0 && need[i][j] <= work[j]) {apply++;if (apply = = s) {for (M = 0; m < S; m++) {tempwork[i][m] = work[m]; WORK[M] = Work[m] + allocation[i][m];} Finish[i] = 1;temp[k] = I;i = -1;k++;flag++;}}} for (i = 0; i < P; i++) {if (finish[i] = = 0) {System.out.println ("system insecure"); return-1;}} SYSTEM.OUT.PRINTLN ("System is safe"); System.out.print ("Assigned sequence:"); for (i = 0; i < P; i++) {System.out.print (Temp[i]), if (I < P-1) {System.out.print (",");}} System.out.println (); return 0;} public void Changedata (Int. flag) {for (int i = 0; i < S; i++) {available[i] = Available[i]-request[i]; Allocation[flag][i] = Allocation[flag][i] + request[i]; Need[flag][i] = Need[flag][i]-request[i];}} public void Share () {sc = new Scanner (system.in); int i, Flag;char ch = ' Y '; SYSTEM.OUT.PRINTLN ("input process for requesting resources:"); flag = Sc.nextint (); Request = new Int[100];if (flag >= P) {System.out.println ("This process does not exist!");} else {System.out.println ("Enter the number of requests for this process for each resource:"); for (i = 0; i < S; i++) {Request[i] = Sc.nextint ();} for (i = 0; i < S; i++) {if (Request[i] > Need[flag][i]) {System.out.println ("process" + flag + "requested resource is greater than it needs!"); SYSTEM.OUT.PRINTLN ("Distribution unreasonable, not assigned!"); ch = ' N '; break;} else if (Request[i] > Available[i]) {SYSTEM.OUT.PRINTLN (process + flag +) The requested resource is greater than the available resources. "); SYSTEM.OUT.PRINTLN ("Unreasonable allocation, non-allocation!"); ch = ' N '; break;}} if (ch = = ' Y ') {changedata (flag); if (judgesafe () = =-1) {System.out.println ("process" + flag + "Request resources, the system goes to deadlock state, allocation fails!"); for (int j = 0; J < S; J + +) {Available[j] = Available[j] + request[j]; ALLOCATION[FLAG][J] = allocation[flag][j]-request[j]; NEED[FLAG][J] = Need[flag][j] + request[j];}}}} public static void Main (string[] args) {bankerofalgorithm mytext = new Bankerofalgorithm (); Scanner sc = new Scanner (system.in); int i, J, p, s, number; String Choice, tempstring; SYSTEM.OUT.PRINTLN ("\t\t operating system Experiment Six"); SYSTEM.OUT.PRINTLN ("Input resource type:"); s = Sc.nextint (); mytext. s = s; System.out.println ("Enter the name and quantity of the resource:"); mytext.name = new String[100];mytext. Available = new Int[100];for (i = 0; i < s; i++) {tempstring = Sc.next (); number = Sc.nextint (); Mytext.name[i] = TempStr Ing;mytext. Available[i] = number;} System.out.println ("Number of input processes:");p = Sc.nextint (); mytext. p = p; System.out.println ("Enter maximum demand for each process resource:"); mytext. Max = new Int[100][100];for (i = 0; i < P; i++) {for (j = 0; J < S; j + +) {mytext. MAX[I][J] = Sc.nextint ();}} System.out.println ("Enter the amount of resources allocated for each process:"); mytext. Allocation = new Int[100][100];for (i = 0; i < P; i++) {for (j = 0; J < S; j + +) {mytext. ALLOCATION[I][J] = Sc.nextint (); mytext. AVAILABLE[J] = mytext. available[j]-MyText. ALLOCATION[I][J];}} MyText. Need = new Int[100][100];for (i = 0; i < P; i++) {for (j = 0; J < S; j + +) {mytext. NEED[I][J] = mytext. MAX[I][J]-MyText. ALLOCATION[I][J];}} MyText. Showdata (); mytext. Judgesafe (); while (true) {System.out.println ("Whether resources are allocated (n is off):"), choice = Sc.next (), char Choice_char = Choice.charat (0); Switch (Choice_char) {case ' n ': system.exit (0);d Efault:mytext. Share (); break;} MyText. Showdata (); mytext. Judgesafe ();}}}
Banker algorithm (Java language Implementation)