os.银行家算法--Java实现

源代码如下:

package os课设;
import java.util.ArrayList;
import java.util.Scanner;
public class Banker_Algorithm {
    /**
     3 5

     3 3 2

     0 1 0
     2 0 0
     3 0 2
     2 1 1
     0 0 2

     7 4 3
     1 2 2
     6 0 0
     0 1 1
     4 3 1

     request[i]

     1

     1 0 2

     4

     3 3 0

     */
    public static void main (String[] args) {
        Scanner sc = new Scanner ( System.in );
        int R_num = 0;  //资源个数
        int P_num = 0; //进程个数
        System.out.println ("资源个数和进程个数:");
        R_num = sc.nextInt ();
        P_num = sc.nextInt ();
        int[] Available = new int[R_num];   //可用资源个数
        int[] Work = new int[R_num];   //工作向量
        int[][] Max = new int[P_num][R_num];    //最大需求矩阵
        int[][] Allocation = new int[P_num][R_num]; //分配矩阵
        int[][] Need = new int[P_num][R_num];   //需求矩阵
        Boolean[] Finish = new Boolean[P_num];  //完成矩阵
        boolean tag = false;    //保存试分配后的状态 false 为不安全, true 为安全

        //初始化
        init ( P_num,R_num,Available,Work,Max,Allocation,Need,Finish );

        while (true){
            System.out.println ("1.安全性算法\t2.银行家算法\t3.退出");
            int option = sc.nextInt ();
            switch (option){
                        //安全性算法
                case 1: Security_algorithm(P_num,R_num,Available,Work,Max,Allocation,Need,Finish);
                    break;
                        //请求
                case 2:Req(P_num,R_num,Available,Work,Max,Allocation,Need,Finish,tag);
                    break;
                case 3:System.exit ( -1 );
                default:
                    System.out.println ("请输入1-4之间的数!");
            }
        }
    }

    private static void Req (int p_num, int r_num, int[] available, int[] work, int[][] max,
                             int[][] allocation,int[][] need, Boolean[] finish,boolean tag) {
        Scanner sc = new Scanner ( System.in );
        System.out.println ("第i个进程申请资源:");
        int i = sc.nextInt ();  //表示第 i 个进程

        System.out.println ("初始化request[i]:");
        //初始化request[i]
        int[][] request = new int[p_num][r_num];
        for (int j=0; j<r_num;j++){
            request[i][j] = sc.nextInt ();
        }

        tag = req(request,i,need,available,allocation);
        if (tag){
            Security_algorithm ( p_num,r_num,available,work,max,allocation,need,finish );
        }else {
            System.out.println ("第"+i+"个进程阻塞!");
        }
    }

    /**
     *  1.request[i] <= need[i]
     *  2.request[i] <= available
     *  3.试分配
     */
    private static boolean req (int[][] request, int i, int[][] need, int[] available,int[][] allocation) {
        int[] temp;
        if (compareTo(request,i,need) && compare(request,i,available)){
            temp = subtraction(available,request,i);
            one_assignment ( temp,available );
            allocation[i] = one_addition ( request[i],allocation,i );
            need[i] = subtraction ( need[i],request,i );
            return true;
        }else {
            return false;
        }
    }

    //减法
    private static int[] subtraction (int[] available, int[][] request, int i) {
        int[] temp = new int[available.length];
        for (int k =0; k<available.length; k++){
            temp[k] = available[k] - request[i][k];
        }
        return temp;
    }
    //request[i] <= available
    private static boolean compare (int[][] request, int i, int[] available) {
        int count = 0;
        for (int j = 0; j<available.length; j++){
            if (request[i][j] <= available[j]){
                count++;
            }
        }
        if (count == available.length){
            return true;
        }else {
            return false;
        }
    }
    //request[i] <= need[i]
    private static boolean compareTo (int[][] request, int i, int[][] need) {
        int count = 0;
        for (int j = 0; j<need[i].length; j++){
            if (request[i][j] <= need[i][j]){
                count++;
            }
        }
        if (count == need[i].length){
            return true;
        }else {
            return false;
        }
    }

    //安全性算法
    private static void Security_algorithm (int p_num, int r_num, int[] available, int[] work,
                                            int[][] max, int[][] allocation, int[][] need, Boolean[] finish) {

        //初始化 并且 根据need给finish赋值
//        init ( p_num,r_num,available,work,max,allocation,need,finish );

        //Available 一维数组给 Work赋值
        one_assignment(available,work);
        ArrayList arr = new ArrayList (  );   //用于保存数组索引
        while (arr.size () != p_num){
            int sum = 0;    //记录 need[i] > work 的次数
            //寻找第 i 个进程
            for (int i=0; i<p_num; i++){
                if (!finish[i] && row(need,i,work)){
                    work = one_addition(work,allocation,i);
                    finish[i] = true;
                    arr.add ( i );  //找到之后就保存该进程的索引
                }else {
                    sum++;
                }
            }
            if (sum == p_num){
                break;
            }
        }

        int count = 0;
        for (int i=0; i<finish.length; i++){
            if (finish[i]){
                count++;
            }
        }

        if (count == p_num){
            //返回安全序列
            System.out.println ("序列:");
            for (int i=0;i<p_num;i++){
                System.out.print (arr.get ( i )+"\t");
            }
            System.out.println ();
        }else {
            System.out.println ("该状态不安全!");
        }
    }
    //一维向量相加 work = work + allocation[i]
    private static int[] one_addition (int[] work, int[][] allocation, int i) {
        int[] temp = new int[work.length];
        for (int k = 0; k<work.length; k++){
            temp[k] = work[k] + allocation[i][k];
        }
        return temp;
    }
    //need[i] <= work 的方法
    private static boolean row (int[][] need, int i, int[] work) {
        int count = 0;
        for (int j = 0; j<need[i].length; j++){
            if (need[i][j] <= work[j]){
                count++;
            }
        }
        if (count == work.length){
            return true;
        }else {
            return false;
        }
    }

    private static void init (int p_num, int r_num, int[] available, int[] work, int[][] max,
                                int[][] allocation, int[][] need, Boolean[] finish) {
        Scanner sc = new Scanner ( System.in );
        System.out.println ("初始化资源矩阵:");
        //初始化资源矩阵
        for (int i = 0; i<r_num; i++){
            available[i] = sc.nextInt ();
        }

        System.out.println ("初始化已分配矩阵:");
        //初始化已分配矩阵
        for (int i=0; i<p_num; i++){
            for (int j=0; j<r_num; j++){
                allocation[i][j] = sc.nextInt ();
            }
        }

        System.out.println ("初始化需求矩阵:");
        //初始化需求矩阵
        for (int i=0; i<p_num; i++){
            for (int j=0; j<r_num; j++){
                need[i][j] = sc.nextInt ();
            }
        }

        //Max矩阵由Need矩阵和Allocation矩阵相加得到
//        max = two_addition(need,allocation);

        //根据Need矩阵决定Finish ,Need[i]={0} 则Finish[i] = true,否则为false
        for (int i = 0; i<p_num; i++){
            if (row_vector(need,i) == 0){
                finish[i] = true;
            }else {
                finish[i] = false;
            }
        }
    }

    //一维矩阵赋值
    public static void one_assignment (int[] available, int[] work) {
        for (int i=0; i<available.length; i++){
            work[i] = available[i];
        }
    }

    //表示二维向量中的一行都为 0的方法
    public static int row_vector (int[][] need, int i) {
        int count = 0;  //计数器
        for (int j = 0 ; j<need[i].length; j++){
            if (need[i][j] == 0){
                count++;
            }
        }
        if (count == need[i].length){
            return 0;
        }else {
            return 1;
        }
    }
}

备注:测试用例的顺序就是控制台要求输入的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值