📄 banker.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Bankers;import java.util.Scanner;/** * * @author RUMI */public class Banker { public int Process, Resource; String [] name = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; int[] Avaiable=new int[10]; int[][] Allocation=new int[10][10]; int[][] Maximum=new int [10][10]; int[][] Need=new int[10][10]; int[] Work=new int [10]; int[][] Tallocation=new int[10][10]; int[][] Tneed=new int[10][10]; int[] Tavaiable=new int[10]; int[] Answer=new int[10]; int[] Request=new int[10]; boolean[] Finish=new boolean[10]; int init () { for (int i=0;i<6;i++) { Avaiable[i]=0; Work[i]=0; Answer[i]=0; Request[i]=0; for (int j=0;j<6;j++) { Maximum[i][j]=Allocation[i][j]=Need[i][j]=0; } } return 0; } boolean safe_sequence() { int i,j,k,get=0; for(i=0;i<Resource;i++) { Work[i]=Avaiable[i]; } for(i=0;i<Process;i++) { Finish[i]=false; } boolean flag; for(i=0;i<Process;i++) { for(j=0;j<Process;j++) { if(Finish[j]==false) { flag=true; for(k=0;k<Resource;k++) { if(Need[j][k]>Work[k]) { flag=false; break; } } if(flag==true) { Answer[get]=j; get++; for(k=0;k<Resource;k++) { Work[k]=Work[k]+Allocation[j][k]; } Finish[j]=true; //break; } } } } boolean tag=true; for(i=0;i<Process;i++) { if(Finish[i]==false) { tag=false; break; } } if(tag==false) { System.out.printf("\n\n----NO SAFE SEQUENCE----\n\n"); return false; } else { //int temp; System.out.printf("\n\n----THERE IS A SAFE SEQUENCE----\n"); System.out.printf(" "); for(i=0;i<Process;i++) { //System.out.printf("<P%ld>", Answer[i]); //temp = Answer [i]-1; System.out.printf("<P"+Answer[i]+">"); } System.out.printf("\n"); return true; } } void input() { Scanner sc = new Scanner(System.in); int i; int j; System.out.printf("\nBANKER'S ALGORITHM\n"); System.out.printf("==================\n"); System.out.printf("\nHow many Process: "); Process = sc.nextInt(); System.out.printf("\nHow many Resource: "); Resource = sc.nextInt(); System.out.printf("\n\nAvaiable Resources:"); System.out.printf("\n===================\n"); for (i = 0; i < Resource; i++) { System.out.printf("Resource "+name[i]+": "); Avaiable[i]= sc.nextInt(); Tavaiable[i] = Avaiable[i]; } System.out.printf("\n\nAllocate Resource for Processes :"); System.out.printf("\n================================"); for (i = 0; i < Process; i++) { System.out.printf("\nProcess P"+i+" --- \n"); for (j = 0; j < Resource; j++) { System.out.printf("Allocation "+name[j]+": "); Allocation[i][j]= sc.nextInt(); Tallocation[i][j] = Allocation[i][j]; } } System.out.printf("\n\nMaximum Resource Allocation :"); System.out.printf("\n============================"); for (i = 0; i < Process; i++) { System.out.printf("\nProcess P"+i+" --- \n"); for (j = 0; j < Resource; j++) { System.out.printf("Maximum "+name[j]+" : "); Maximum[i][j] = sc.nextInt(); } } for (i = 0; i < Process; i++) { for (j = 0; j < Resource; j++) { Tneed[i][j] = Need[i][j] = Maximum[i][j] - Allocation[i][j]; } } System.out.printf("\n\nNeed :"); System.out.printf("\n======"); System.out.printf("\n "); for(int k=0;k<Resource;k++){ System.out.printf(name[k]+" "); } for(i=0;i<Process;i++){ System.out.printf("\nProcess P"+i+" --- "); for (j = 0; j < Resource; j++) { System.out.print(Need[i][j]+" "); } } } public void request() { int i,j,k; Scanner sc1 = new Scanner(System.in); String ch = ""; int reqProcess =0; boolean req; while(true) { init(); input(); req = safe_sequence(); while(true) { System.out.printf("Any other request? [y/n] : "); ch = sc1.next(); req=true; if(ch.equals("y")) { System.out.printf("Request for Process : "); reqProcess = sc1.nextInt(); System.out.printf("Enter Resource Request for P"+reqProcess+": \n"); for(i=0;i<Resource;i++) { System.out.printf("Resource "+name[i]+": "); Request[i]= sc1.nextInt(); } for(i=0;i<Resource;i++) { if(Need[reqProcess][i]<Request[i]) { req=false; break; } } for(i=0;i<Resource;i++) { if(Avaiable[i]<Request[i]) { req=false; break; } } if(req==true) { for(i=0;i<Resource;i++) { Avaiable[i]=Avaiable[i]-Request[i]; Allocation[reqProcess][i]=Allocation[reqProcess][i]+Request[i]; Need[reqProcess][i]=Need[reqProcess][i]-Request[i]; } if(safe_sequence()==false) { System.out.printf("----REQUEST REJECTED----\n"); for(i=0;i<Process;i++) { for(j=0;j<Resource;j++) { Avaiable[i]=Tavaiable[i]; Allocation[reqProcess][i]=Tallocation[reqProcess][i]; Need[reqProcess][i]=Tneed[reqProcess][i]; } } } else { for(i=0;i<Process;i++) { for(j=0;j<Resource;j++) { Tavaiable[i]=Avaiable[i]; Tallocation[reqProcess][i]=Allocation[reqProcess][i]; Tneed[reqProcess][i]=Need[reqProcess][i]; } } } } else { System.out.printf("----REQUEST REJECTED----\n"); } } else { break; } } } } // return 0; public static void main(String[] args) { Banker bnk = new Banker(); bnk.request(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -