银行家算法 c语言代码 自己输入 文字是中文
时间: 2024-05-09 22:15:17 浏览: 49
以下是银行家算法的C语言代码,可以自己输入:
```c
#include <stdio.h>
int main() {
int n, m, i, j, k;
printf("请输入进程数:");
scanf("%d", &n);
printf("请输入资源种类数:");
scanf("%d", &m);
int max[n][m], allocation[n][m], need[n][m], available[m], work[m], finish[n], safeSeq[n];
for(i = 0; i < n; i++) {
printf("请输入第%d个进程的最大需求量:", i + 1);
for(j = 0; j < m; j++) {
scanf("%d", &max[i][j]);
}
}
for(i = 0; i < n; i++) {
printf("请输入第%d个进程已分配资源量:", i + 1);
for(j = 0; j < m; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] = max[i][j] - allocation[i][j];
}
finish[i] = 0;
}
printf("请输入系统可用资源量:");
for(i = 0; i < m; i++) {
scanf("%d", &available[i]);
work[i] = available[i];
}
int count = 0, safe = 0;
while(count < n) {
safe = 0;
for(i = 0; i < n; i++) {
if(finish[i] == 0) {
int flag = 1;
for(j = 0; j < m; j++) {
if(need[i][j] > work[j]) {
flag = 0;
break;
}
}
if(flag == 1) {
for(k = 0; k < m; k++) {
work[k] += allocation[i][k];
}
safeSeq[count] = i + 1;
count++;
finish[i] = 1;
safe = 1;
}
}
}
if(safe == 0) {
printf("系统不安全!\n");
return 0;
}
}
printf("系统安全,安全序列为:");
for(i = 0; i < n; i++) {
printf("%d ", safeSeq[i]);
}
printf("\n");
return 0;
}
```
希望对你有帮助!
阅读全文