问题描述
Write the general program of banker algorithm. Avoiding deadlock in resource allocation process.
Requests: There should be at least 4 kinds of resources and 6 processes. The program can decide whether allocate resources to the process according to banker algorithm. The process requests should be input by user manually.
编写了银行家算法的通用程序。避免资源分配过程中的死锁。
要求:至少有4种资源和6个进程。程序可以根据银行家算法来决定是否为进程分配资源。流程请求应由用户手动输入。
实现过程
First, we need to create a structure to record the need,allocation and max values of each process.
Before writing the main program, we need to write a function to check the security algorithm, and each time after we enter the data, we will focus on judging whether there is a security sequence to determine whether to carry out the following resource allocation test.
In the main function, we first need to enter the total amount of four resources, and then enter the allocation and max values for each process. Calculate the threshold value of each of our processes through the allocation and max values, and then pass in the security detection function to judge.
After passing the safe sequence function, we will give a safe sequence.
After judging the first few sequences, we will make a request for process resources and determine whether resources can be allocated.
首先,我们需要创建一个结构来记录每个进程的需求、分配和最大值。
在写主程序之前,我们需要编写一个函数来检查安全算法,每次输入数据后,我们都会重点判断是否有安全序列,以确定是否进行下面的资源分配测试。
在主函数中,我们首先需要输入四个资源的总量,然后再输入每个进程的分配值和最大值。通过分配和最大值计算我们每个进程的阈值,然后传入安全检测函数进行判断。
在通过安全序列函数后,我们将给出一个安全序列。
在判断前几个序列后,我们将请求进程资源,并确定是否可以分配资源。
代码 code
1 |
|
运行结果


本文作者:
王某某
发布时间: 2022-05-05
最后更新: 2024-06-13
本文标题: 银行家算法
本文链接: https://www.lifeandcode.top/2022/05/05/银行家算法/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
发布时间: 2022-05-05
最后更新: 2024-06-13
本文标题: 银行家算法
本文链接: https://www.lifeandcode.top/2022/05/05/银行家算法/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
