银行家算法

本文介绍了使用Java实现银行家算法的过程,通过实验展示了如何通过合理资源分配防止死锁,同时强调了该算法在操作系统中的应用和对编程技能提升的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实验目的:

本实验的目的是实现银行家算法,一种用于避免系统因资源分配不当而发生死锁的算法。通过实现银行家算法,可以实现对进程请求资源的合理分配和安全执行,从而确保系统运行的稳定性和安全性。

 

实验设备与实验环境:

计算机,Java编译系统,idea,ChatGPT

 

二、实验程序设计内容:

本实验的设计内容包括实现银行家算法的主要功能,包括对资源的最大需求矩阵、分配矩阵、可用资源向量的输入,最终确定系统是否处于安全状态。

 

三、实验程序设计思路及流程图

  1. 通过输入进程数和资源类型数,以及最大需求矩阵、分配矩阵和可用资源向量,初始化系统的资源状态。
  2. 通过银行家算法的安全性检测函数(stest())判断系统当前状态是否安全,如果存在安全序列,则输出安全序列;否则,输出系统状态为不安全。

 

四、实验源程序及注释:

package homework.os;

  

  /**

 * Date:2024/4/22  10:09

 * Description:TODO

 *

 * @author Leon

 * @version 1.0

 */

  

  import java.util.Scanner;

  

public class exm4_0 {

    static int p;

    static int r;

    static int[][] maxs;

    static int[][] allocation;

    static int[][] need;

    static int[] available;

    static int[] request;

    static int[] safeSequence;

  

    public static void main(String[] args) {

        /*

        4 3

        3 2 2

        6 1 3

        3 1 4

        4 2 2

        1 0 0

        5 1 1

        2 1 1

        0 0 2

        1 1 2

         */

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of processes: ");

        p = scanner.nextInt();

        System.out.print("Enter the number of resource types: ");

        r = scanner.nextInt();

  

        maxs = new int[p][r];

        allocation = new int[p][r];

        need = new int[p][r];

        available = new int[r];

        request = new int[r];

        safeSequence = new int[p];

  

        infInput(scanner);

  

        if (stest()) {

            System.out.println("Safe sequence exists. Initial state is safe.");

            System.out.print("Safe sequence: ");

            for (int i = 0; i < p; i++) {

                System.out.print(safeSequence[i] + " ");

            }

            System.out.println();

        } else {

            System.out.println("No safe sequence exists. Initial state is unsafe.");

        }

  

        scanner.close();

    }

  

    public static void infInput(Scanner scanner) {

        System.out.println("Enter the maximum demand matrix:");

        for (int i = 0; i < p; i++) {

            for (int j = 0; j < r; j++) {

                maxs[i][j] = scanner.nextInt();

            }

        }

  

        System.out.println("Enter the allocation matrix:");

        for (int i = 0; i < p; i++) {

            for (int j = 0; j < r; j++) {

                allocation[i][j] = scanner.nextInt();

            }

        }

  

        for (int i = 0; i < p; i++) {

            for (int j = 0; j < r; j++) {

                need[i][j] = maxs[i][j] - allocation[i][j];

            }

        }

  

        System.out.println("Enter the available resource vector:");

        for (int i = 0; i < r; i++) {

            available[i] = scanner.nextInt();

        }

    }

  

    public static boolean compare(int[] m, int[] n) {

        for (int i = 0; i < r; i++) {

            if (m[i] < n[i]) {

                return false;

            }

        }

        return true;

    }

  

    public static boolean stest() {

        int[] finish = new int[p];

        int[] work = new int[r];

        int count = 0;

  

        for (int i = 0; i < p; i++) {

            finish[i] = 0;

        }

  

        for (int i = 0; i < r; i++) {

            work[i] = available[i];

        }

  

        System.out.println("分配序列:");

        System.out.println("\t\tallocation\t\tneed\t\t\tavailable");

        while (count < p) {

            boolean found = false;

            for (int i = 0; i < p; i++) {

  

                if (finish[i] == 1) continue;

                if (compare(work, need[i])) {//available>need

  

                    finish[i] = 1;

                    safeSequence[count] = i + 1;

                    System.out.print("进程" + (i + 1) + "\t\t");

                    for (int j = 0; j < r; j++) {

                        System.out.printf("%2d ", allocation[i][j]);

                    }

                    System.out.print("\t\t");

                    for (int j = 0; j < r; j++) {

                        System.out.printf("%2d ", need[i][j]);

                    }

                    System.out.print("\t\t");

                    for (int j = 0; j < r; j++) {

                        System.out.printf("%2d ", work[j] + allocation[i][j]);

                    }

                    System.out.println("");

                    count++;

                    found = true;

                    for (int j = 0; j < r; j++) {

                        work[j] += allocation[i][j];

                    }

                    break;

                }

  

            }

            if (!found) {

                break;

            }

        }

  

        return count == p;

    }

  }

五、实验程序测试过程及解释说明

进程数和资源种类数输入

Enter the number of processes: 4

Enter the number of resource types: 3

输入最大矩阵分配矩阵(代码计算需求矩阵) 可用资源

Enter the maximum demand matrix:

3 2 2

6 1 3

3 1 4

4 2 2

Enter the allocation matrix:

1 0 0

5 1 1

2 1 1

0 0 2

Enter the available resource vector:

1 1 2

 

六、实验程序测试过程与结果分析、

分配序列:

                      allocation               need                        available

进程2              5  1  1                 1  0  2                 6  2  3

进程1              1  0  0                 2  2  2                 7  2  3

进程3              2  1  1                 1  0  3                 9  3  4

进程4              0  0  2                 4  2  0                 9  3  6

Safe sequence exists. Initial state is safe.

Safe sequence: 2 1 3 4
 

七、理论学习与实践能力锻炼方面的个人心得体会

通过本次实验,我深入理解了银行家算法的原理和实现过程,了解了如何通过合理的资源分配和请求处理来避免系统死锁的发生。同时,通过编写代码实现银行家算法,提高了对Java编程语言的熟练程度,加深了对多线程编程和资源管理的理解。对操作系统的资源管理机制有了更深入的认识,对系统安全性和稳定性的重要性有了更深刻的体会。

实验评价及结论:

实验目的明确、设计内容符合要求,独立完成了操作系统银行家算法程序设计任务且源程序与注释、测试过程记录完整正确,能够很好地将课程理论运用于解决实际问题;实验报告内容完整,态度认真,总体质量优秀。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

vⅤ_Leon

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

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

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

打赏作者

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

抵扣说明:

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

余额充值