用c语言写出银行家算法模拟,能够根据给定的资源分配情况,及某进程提出的资源请求,通过算法得出是否能够进行分配。
时间: 2024-05-15 20:19:33 浏览: 9
以下是用C语言实现银行家算法模拟的示例代码:
```c
#include <stdio.h>
#define MAX_PROCESS 10
#define MAX_RESOURCE 10
int available[MAX_RESOURCE];
int maximum[MAX_PROCESS][MAX_RESOURCE];
int allocation[MAX_PROCESS][MAX_RESOURCE];
int need[MAX_PROCESS][MAX_RESOURCE];
int work[MAX_RESOURCE];
int finish[MAX_PROCESS];
int n, m;
void init()
{
printf("请输入进程数和资源数:\n");
scanf("%d%d", &n, &m);
printf("请输入每个进程需要的资源数:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &maximum[i][j]);
}
}
printf("请输入每个进程已分配的资源数:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
printf("请输入当前可用的资源数:\n");
for (int i = 0; i < m; i++) {
scanf("%d", &available[i]);
}
}
int safety_check()
{
for (int i = 0; i < m; i++) {
work[i] = available[i];
}
for (int i = 0; i < n; i++) {
finish[i] = 0;
}
int count = 0;
while (count < n) {
int found = 0;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == m) {
for (int k = 0; k < m; k++) {
work[k] += allocation[i][k];
}
finish[i] = 1;
found = 1;
count++;
}
}
}
if (!found) {
return 0;
}
}
return 1;
}
int request_check(int pid, int request[])
{
for (int i = 0; i < m; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
return 0;
}
}
for (int i = 0; i < m; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (safety_check()) {
return 1;
} else {
for (int i = 0; i < m; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
return 0;
}
}
void show()
{
printf("进程\t最大需求\t已分配\t需求\t可用\n");
for (int i = 0; i < n; i++) {
printf("%d\t", i);
for (int j = 0; j < m; j++) {
printf("%d ", maximum[i][j]);
}
printf("\t");
for (int j = 0; j < m; j++) {
printf("%d ", allocation[i][j]);
}
printf("\t");
for (int j = 0; j < m; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
printf("可用资源:");
for (int i = 0; i < m; i++) {
printf("%d ", available[i]);
}
printf("\n");
}
int main()
{
init();
show();
int pid;
int request[MAX_RESOURCE];
while (1) {
printf("请输入请求的进程号(-1表示退出):\n");
scanf("%d", &pid);
if (pid == -1) {
break;
}
printf("请输入进程所需的资源数:\n");
for (int i = 0; i < m; i++) {
scanf("%d", &request[i]);
}
if (request_check(pid, request)) {
printf("请求成功,安全序列为:");
for (int i = 0; i < n; i++) {
if (finish[i]) {
printf("%d ", i);
}
}
printf("\n");
} else {
printf("请求失败,无法分配资源\n");
}
show();
}
return 0;
}
```
该程序首先要求用户输入进程数和资源数,以及每个进程需要的资源数和已分配的资源数,以及当前可用的资源数。然后,用户可以输入进程号和进程所需的资源数,程序会根据银行家算法检查是否能够满足该请求。
在程序运行过程中,用户可以随时输入“-1”来退出程序。每次请求结束后,程序会显示当前的资源分配情况和可用资源数。如果请求成功,程序会输出安全序列,表示当前系统处于安全状态。如果请求失败,程序会提示无法分配资源。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)