银行家算法

import java.util.Scanner;

public class Banker {
    
    
    //Available向量
    public static int[] Available(int m,Scanner sc){
        
    int available[]=new int[m]; //资源数
    System.out.println("输入各类资源Available");
    for(int i = 0;i < m;i++) {
        available[i] = sc.nextInt();
    }

    return available;
    }
    
    //Allocation矩阵
    public static int[][] Allocation(int n,int m,Scanner sc){
        int[][] Allocation = new int[n][m];
        for(int i = 0;i < n;i++) {
            System.out.println("输入进程"+i+"已占有各资源数量:");
            for(int j = 0;j < m;j++) {
                Allocation[i][j] = sc.nextInt();
            }
        }

        return Allocation;
    }
    
    
    //Need矩阵
    public static int[][] Need(int n,int m,Scanner sc){
        int[][] Need = new int[n][m];
        for(int i = 0;i < n;i++) {
            System.out.println("输入进程"+i+"的Need向量:");
            for(int j = 0;j < m;j++) {
                Need[i][j] = sc.nextInt();            }
        }

        return Need;
            
    }
    //请求向量    
    public static int[] Request(int m,Scanner sc,int p,int[][] Need) {
        System.out.println("输入该进程的资源请求向量Request");
        int[] Request = new int[m];
        
        for(int i = 0;i < m;i++) {
            int a = sc.nextInt();
                if(a <= Need[p][i]) {                //控制Request不大于Need
                    Request[i] = a;
                }else {
                    i--;
                    System.out.println("请求过量,重新输入!");
                }
        }
        return Request;
    }
    
    //判断进程是否安全,如果安全则输出安全队列    
    public static void safe(int n,int m,int[] q,int[] Available,int[][] Need,int[][] Allocation) {
        boolean finish[] = new boolean[n];
        int work[] = new int[m];
        int k = 0;                                    //安全队列下标                         
        int j = 0;                                    //要判断的线程数
        int i;
        for(i = 0;i < n;i++) {                            //finish数组全部初始化为false
            finish[i] = false;
        }
        for(i = 0;i < m;i++){
            work[i] = Available[i];                    //work数组初始化为Available
        }
        while(j < n){
            for(i = 0;i < m;i++){    
                if(finish[j]){                            //如果进程j的finish为true,j++判断下一进程
                    j++;
                    break;
                }else if(Need[j][i] > work[i]){            //如果need不能被满足,j++判断下一进程
                    j++;
                    break;
                }else if(i == m-1){
                    for(int a = 0;a < m;a++){
                        work[a] += Allocation[j][a];        //如果进程j的need全部被work满足,更改need
                    }
                    finish[j] = true;
                    q[k] = j;
                    k++;
                    j = 0;                                   //从最小线程再开始判断
                }
            }
        }
            
                                                         //判断是否都属于安全状态
        for(int a = 0;a < n;a++) {
            if(finish[a] == false) {
                System.out.println("申请后状态不安全!");
                break;
            }else if(a == n-1) {                        //当所有进程的finish全为true,输出安全队列
                System.out.println("申请后状态安全,安全队列为:");
                for(int b = 0;b < n;b++) {
                    System.out.println(q[b]);
                }
            }
        }
    }    
    
    public static void main(String[] args) {
        System.out.println("输入资源种类数目:");
        Scanner sc=new Scanner(System.in);
        int m = sc.nextInt();
        int[] Available = Available(m,sc);
        System.out.println("输入进程数目:");
        int n = sc.nextInt();
        int[][] Allocation = Allocation(n,m,sc);
        int[][] Need = Need(n,m,sc);
        int[] q = new int[n];
        
        
        System.out.println("选一个进程作为当前进程:");
        
        int p = sc.nextInt();
                
        int[] Request = Request(m,sc,p,Need);
        
        for(int i = 0;i < m; i++) {
            Allocation[p][i] += Request[i];
            Available[i] -= Request[i];
            Need[p][i] -= Request[i];
        }
            
        System.out.println("分配资源后的Allocation矩阵");
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                System.out.print(Allocation[i][j]+"  ");
            }
            System.out.println();
        }
        
        System.out.println("分配资源后的Need矩阵:");
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                System.out.print(Need[i][j]+"  ");
            }
            System.out.println();
        }
        
        System.out.println("分配资源后的Available向量:");
        for(int i = 0;i < m;i++) {
            System.out.print(Available[i]+"  ");
        }
        System.out.println();
                
        safe(n,m,q,Available,Need,Allocation);
        
        sc.close();        
    }
    
}

 

posted @ 2019-04-28 15:49  tianqibucuo  阅读(228)  评论(0)    收藏  举报