⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 banker.java

📁 银行家算法 java版 运行 java Banker < a.txt
💻 JAVA
字号:
import java.io.*;
public class Banker{
  int available[];
  int max[][];
  int need[][];
  int allocated[][];
  int resourceType;
  int processCount;

  boolean echo=true;

  BufferedReader br;
  public static void main(String args[]){
      Banker banker=new Banker();
      banker.init();
      banker.display();
      banker.allocateResource();
  }
  void init(){
      try{
        br=new BufferedReader(new InputStreamReader(System.in));
      }catch(Exception e){
        System.out.println(e);
        System.exit(0);
      }
      System.out.println("===银行家算法演示程序===");
      System.out.print("请输入资源种类数:");
      resourceType=readInt();
      available=new int[resourceType];
      System.out.print("请输入资源个数向量,共"+resourceType+"个整数,以空格分隔:\n");
      available=readIntArray();

      System.out.print("请输入进程个数:");
      processCount=readInt();
      max=new int[processCount][resourceType];
      allocated=new int[processCount][resourceType];
      need=new int[processCount][resourceType];
      System.out.print("请输入进程的最大资源需求量矩阵(max矩阵,每行"+resourceType+"个整数,以空格分隔,共"+processCount+"行):\n");
      for(int i=0;i <processCount;i++)
        max[i]=readIntArray();
      System.out.print("请输入进程的已分配资源量矩阵(allocated矩阵,每行"+resourceType+"个整数,以空格分隔,共"+processCount+"行):\n");
      for(int i=0;i <processCount;i++)
        allocated[i]=readIntArray();
      for(int i=0;i <resourceType;i++)
        for(int j=0;j <processCount;j++)
            need[j][i]=max[j][i]-allocated[j][i];

      for(int i=0;i <resourceType;i++)
        for(int j=0;j <processCount;j++){
            available[i]-=allocated[j][i];
            if (available[i] <0){
              System.out.println("错误:第"+(i+1)+"个可用资源数不足以分配给进程,请检查资源总量和议分配资源矩阵!");
              System.exit(0);
            }
        }
  }
  void display(){
      System.out.print("===当前状态:");
      if (safeOrNot())
        System.out.println("安全");
      else
        System.out.println("不安全");
      System.out.println("Max矩阵:");
      printMatrix(max);
      System.out.println("Allocated矩阵:");
      printMatrix(allocated);
      System.out.println("Need矩阵:");
      printMatrix(need);
      System.out.println("available向量:");
      printMatrix(available);
      System.out.println("安全状态:"+safeOrNot());
  }

  void printMatrix(int matrix[]){
      if (matrix==null) return;
      System.out.print("[");
      for(int i=0;i <matrix.length;i++){
        System.out.print(matrix[i]);
        if (i+1 <matrix.length)
          System.out.print("\t");
      }
      System.out.println("]");
  }
  void printMatrix(int matrix[][]){
      if (matrix==null) return;
      for(int i=0;i <matrix.length;i++){
        System.out.print("[");
        for(int j=0;j <matrix[0].length;j++){
            System.out.print(matrix[i][j]);
            if (j+1 <matrix[i].length)
              System.out.print("\t");
        }
        System.out.println("]");
      }
  }

  void allocateResource(){
      while(true){
        System.out.print("请输入资源请求进程编号:\n");
        int processID=readInt();
        System.out.print("请输入资源请求向量,共"+resourceType+"个整数,以空格分隔:\n");
        int request[]=readIntArray();
        if (!smallerVector(request,need[processID])){
            System.out.println("资源请求不符该进程的需求量.");
        }else if (!smallerVector(request,available)){
            System.out.println("可用资源不足以满足请求,进程阻塞等待.");
        }else{
            subVector(available,request);
            addVector(allocated[processID],request);
            subVector(need[processID],request);
            if(safeOrNot()){
              display();
            }else{
              System.out.println("不安全!");
              addVector(available,request);
              subVector(allocated[processID],request);
              addVector(need[processID],request);
            }
        }
        System.out.print("继续?(y/n)");
        if (readString().toLowerCase().equals("n"))
            break;
      }
  }

  void copyVector(int[] v1,int[] v2){
      for(int i=0;i <v1.length;i++)
        v1[i]=v2[i];
  }

  void addVector(int[] v1,int[] v2){
      for(int i=0;i <v1.length;i++)
        v1[i]+=v2[i];
  }

  void subVector(int[] v1,int[] v2){
      for(int i=0;i <v1.length;i++)
        v1[i]-=v2[i];
  }

  boolean smallerVector(int[] v1,int[] v2){
      boolean value=true;
      for(int i=0;i <v1.length;i++)
        if (v1[i]>v2[i]){
            value=false;
            break;
        }
      return value;
  }

  boolean allEquals(boolean[] v,boolean b){
      boolean value=true;
      for(int i=0;i <v.length;i++)
        if (v[i]!=b){
            value=false;
            break;
        }
      return value;
  }

  boolean safeOrNot(){
      int work[]=new int[resourceType];
      boolean finished[]=new boolean[processCount];
      copyVector(work,available);
      while(true){
        int i;
        for(i=0;i <processCount;i++)
            if (!finished[i] && smallerVector(need[i],work)){
              addVector(work,allocated[i]);
              finished[i]=true;
              break;
            }
        if(i==processCount)
            if (allEquals(finished,true))
              return true;
            else
              return false;
      }
  }

  public String readString(){
      try{
        String s=br.readLine();
        if (echo)
            System.out.println(s);
        return s;
      }catch(Exception e){
        System.out.println(e);
        return null;
      }
  }

  public int readInt(){
      try{
        String s=br.readLine();
        if (echo)
            System.out.println(s);
        return Integer.parseInt(s);
      }catch(Exception e){
        System.out.println(e);
        return 0;
      }
  }

  public int[] readIntArray(){
      try{
        String s=br.readLine();
        if (echo)
            System.out.println(s);
        String tmp[]=s.split(" ");
        int value[]=new int[tmp.length];
        for(int i=0;i <value.length;i++)
            value[i]=Integer.parseInt(tmp[i]);
        return value;
      }catch(Exception e){
        System.out.println(e);
        return null;
      }
  }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -