C语言银行家算法代码
时间: 2023-11-17 14:07:02 浏览: 40
银行家算法是一种避免死锁的算法,下面是一个简单的C语言实现:
```c
#include <stdio.h>
#define MAX_PROCESS 10
#define MAX_RESOURCE 10
int available[MAX_RESOURCE];
int max[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 < m; i++) {
scanf("%d", &available[i]);
}
printf("请输入进程资源已分配量:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &allocation[i][j]);
}
}
printf("请输入进程最大需求量:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &max[i][j]);
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int safe() {
int i, j, k;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
for (i = 0; i < m; i++) {
work[i] = available[i];
}
for (i = 0; i < n; i++) {
if (finish[i] == 0) {
for (j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == m) {
finish[i] = 1;
for (k = 0; k < m; k++) {
work[k] += allocation[i][k];
}
i = -1;
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 0) {
return 0;
}
}
return 1;
}
void bank() {
int i, j, k, request[MAX_RESOURCE], pid;
printf("请输入请求资源的进程编号:\n");
scanf("%d", &pid);
printf("请输入请求的资源量:\n");
for (i = 0; i < m; i++) {
scanf("%d", &request[i]);
}
for (i = 0; i < m; i++) {
if (request[i] > need[pid][i]) {
printf("请求的资源量大于进程最大需求量!\n");
return;
}
if (request[i] > available[i]) {
printf("没有足够的资源可分配!\n");
return;
}
}
for (i = 0; i < m; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (safe()) {
printf("分配成功!\n");
} else {
printf("分配失败!\n");
for (i = 0; i < m; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
}
void show() {
printf("当前资源分配情况:\n");
printf("可用资源:");
for (int i = 0; i < m; i++) {
printf("%d ", available[i]);
}
printf("\n");
printf("进程资源已分配量:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}
printf("进程最大需求量:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
printf("进程还需要的资源量:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
}
int main() {
init();
show();
if (safe()) {
printf("当前状态安全!\n");
} else {
printf("当前状态不安全!\n");
return 0;
}
bank();
show();
return 0;
}
```
相关推荐
![doc](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)