银行家算法-java

import java.util.Scanner;
public class Bank1 {
    static int pcb_nums;  // 进程数量
    static int res_nums;  // 资源种类数量
    static int max[][];     // 最大需求资源向量
    static int alloc[][];   // 拥有资源数向量
    static int need[][];    // 还需要资源数向量
    static int ava[];     // 可用资源数向量
    static int request[];  //本次申请的资源量
    static int safe_seq[]; //安全序列数组
    static void bank_init()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入进程数量:");
        pcb_nums = in.nextInt();
        System.out.println("请输入资源数量");
        res_nums = in.nextInt();
        safe_seq = new int[pcb_nums+1];
        max = new int[pcb_nums][res_nums];
        alloc = new int[pcb_nums][res_nums];
        need = new int[pcb_nums][res_nums];
        ava = new int[res_nums];     // 可用资源
        System.out.println("请输入最大资源:");
        for (int i = 0; i < pcb_nums; i++) {
            for (int j = 0; j < res_nums; j++) {
                max[i][j] = in.nextInt();
            }
        }
        System.out.println("请输入分配资源:");
        for (int i = 0; i < pcb_nums; i++) {
            for (int j = 0; j < res_nums; j++) {
                alloc[i][j] = in.nextInt();
            }
        }
        //计算需求矩阵
        for (int i = 0; i < pcb_nums; i++) {
            for (int j = 0; j < res_nums; j++) {
                need[i][j] = max[i][j] - alloc[i][j];
            }
        }
        System.out.println("请输入可用资源:");
        for (int j = 0; j < res_nums; j++)
        {
            ava[j] = in.nextInt();
        }
    }
    //比较进程为m中的元素全大于n中的元素
    static boolean compare(int m[],int n[]){
        for (int i = 0; i < res_nums; i++) {
            if (m[i]<n[i])
                return false;
        }
        return true;
    }
    //安全性检验函数,检测是否存在安全序列
    static boolean safe(){
        boolean finish[] = new boolean[pcb_nums];
        int work[] = new int[res_nums];
        int num = 0;
        for (int i = 0; i < res_nums; i++) {
            work[i] = ava[i];
        }
        for (int i = 0; i < pcb_nums; i++) {
            if (num == pcb_nums )
                break;
            for (int j = 0; j < pcb_nums; j++) {
                if (finish[j])
                    continue;
                else{
                    if (compare(work,need[j])){
                        finish[j] = true;
                        safe_seq[num] = j+1;
                        num++;
                        //释放进程资源
                        for (int k = 0; k < res_nums; k++) {
                            work[k] = work[k] + alloc[j][k];
                        }
                    }
                }
            }
        }
        for (int i = 0; i < pcb_nums; i++) {
            if (!finish[i])
                return false;
        }
        //输出安全序列
        for (int i = 0; i < pcb_nums; i++) {
            System.out.print(safe_seq[i]+" ");
        }
        return true;
    }
    //申请进程后的安全性检验函数  n代表进程号
    static void resafe(int n){
        //available>=request 并且 need >=request
        if (compare(ava,request) && compare(need[n-1],request)){
            for (int i = 0; i < res_nums; i++) {
                alloc[n-1][i] =alloc[n-1][i] + request[i];
                need[n-1][i] = need[n-1][i] - request[i];
                ava[i] = ava[i] - request[i];
            }
            if (safe()){
                System.out.println("允许进程"+n+"申请资源");
            }
            else{
                System.out.println("不允许进程"+n+"申请资源");
                for (int i = 0; i < res_nums; i++) {
                    alloc[n-1][i] = alloc[n-1][i] - request[i];
                    need[n-1][i] = need[n-1][i] + request[i];
                    ava[i] = ava[i] + request[i];
                }
            }
        }
        else
            System.out.println("申请资源量越界");
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //进程编号
        int n;
        bank_init();
        if (safe()){
            System.out.println("  存在安全序列,初始状态安全。");
        }
        else
            System.out.println("不存在安全序列,初始状态不安全。");
        System.out.println("请输入发出请求向量request的进程编号:");
        n = in.nextInt();
        request = new int[res_nums];
        System.out.println("请输入请求向量request:");
        for (int i = 0; i < res_nums; i++) {
            request[i] = in.nextInt();
        }
        resafe(n);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优化大师傅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值