银行家算法(java实现)

银行家算法(java实现)

1.什么是安全序列

image-20210807145513416

image-20210807145554354

  • 所谓安全序列,就是指如果系统按照这种序列分配资源,则每个进程都能顺利完成。只要能找出一个安全序列,系统就是安全状态。当然,安全序列可能有多个。
  • 如果分配了资源之后,系统中找不出任何一个安全序列,系统就进入了不安全状态。这就意味着之后可能所有进程都无法顺利的执行下去。当然,如果有进程提前归还了一些资源,那系统也有可能重新回到安全状态,不过我们在分配资源之前总是要考虑到最坏的情况。
  • 如果系统处于安全状态,就一定不会发生死锁。如果系统进入不安全状态,则可能会发生死锁。(不安全状态未必就是发生了死锁,但发生死锁时一定是在不安全状态)
  • 因此可以在资源分配之前预先判断这次分配是否会导致系统进入不安全状态,以此决定是否答应资源分配请求。这也是“银行家算法”的核心思想。
2.银行家算法

数据结构:
长度为m的一维数组 Available表示还有多少可用资源

nm矩阵Max表示各进程对资源的最大需求数

nm矩阵Allocation表示已经给各进程分配了多少资源

Max - Allocation = Need矩阵表示各进程最多还需要多少资源

用长度为m的一位数组Request表示进程此次申请的各种资源数

银行家算法步骤:
①检查此次申请是否超过了之前声明的最大需求数

②检查此时系统剩余的可用资源是否还能满足这次请求

③试探着分配,更改各数据结构

④用安全性算法检查此次分配是否会导致系统进入不安全状态

安全性算法步骤:
检查当前的剩余可用资源是否能满足某个进程的最大需求,如果可以,就把该进程加入安全序列,并把该进程持有的资源全部回收。
不断重复上述过程,看最终是否能让所有进程都加入安全序列。

3.代码实现
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);
    }
}
4.运行结果

image-20211029170550971

https://gitee.com/infiniteStars/picgoimages/raw/master/image-20211029170647213.png

  • 41
    点赞
  • 280
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
银行家算法是一种避免死锁的算法,它通过动态地分配资源来避免进程因争夺资源而产生死锁。下面是银行家算法Java实现: ```java import java.util.Scanner; public class BankerAlgorithm { private int[][] need; // 需求矩阵 private int[][] allocation; // 分配矩阵 private int[][] max; // 最大需求矩阵 private int[] available; // 可用资源向量 private int processNum; // 进程数 private int resourceNum; // 资源数 public BankerAlgorithm(int[][] need, int[][] allocation, int[][] max, int[] available) { this.need = need; this.allocation = allocation; this.max = max; this.available = available; this.processNum = allocation.length; this.resourceNum = available.length; } // 判断是否满足需求 private boolean check(int[] work, boolean[] finish, int[] need, int[] allocation) { for (int i = 0; i < resourceNum; i++) { if (need[i] > work[i]) { return false; } } for (int i = 0; i < resourceNum; i++) { work[i] += allocation[i]; } finish[processNum] = true; return true; } // 执行银行家算法 public boolean execute() { boolean[] finish = new boolean[processNum]; int[] work = new int[resourceNum]; for (int i = 0; i < resourceNum; i++) { work[i] = available[i]; } int count = 0; while (count < processNum) { boolean flag = false; for (int i = 0; i < processNum; i++) { if (!finish[i] && check(work, finish, need[i], allocation[i])) { flag = true; count++; } } if (!flag) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入进程数:"); int processNum = scanner.nextInt(); System.out.print("请输入资源数:"); int resourceNum = scanner.nextInt(); int[][] max = new int[processNum][resourceNum]; int[][] allocation = new int[processNum][resourceNum]; int[][] need = new int[processNum][resourceNum]; int[] available = new int[resourceNum]; System.out.println("请输入最大需求矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { max[i][j] = scanner.nextInt(); } } System.out.println("请输入分配矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { allocation[i][j] = scanner.nextInt(); need[i][j] = max[i][j] - allocation[i][j]; } } System.out.println("请输入可用资源向量:"); for (int i = 0; i < resourceNum; i++) { available[i] = scanner.nextInt(); } BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(need, allocation, max, available); if (bankerAlgorithm.execute()) { System.out.println("系统是安全的!"); } else { System.out.println("系统是不安全的!"); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值