用c语言写一段银行家算法的代码
时间: 2023-06-03 13:07:58 浏览: 96
以下是使用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 is_safe(int n_process, int n_resource, int work[MAX_RESOURCE], int finish[MAX_PROCESS]) {
int i, j;
int flag = 0;
int count = 0;
int safe_sequence[n_process];
for(i = 0; i < n_process; i++) {
finish[i] = 0;
}
while(count < n_process) {
flag = 0;
for(i = 0; i < n_process; i++) {
if(!finish[i]) {
int can_execute = 1;
for(j = 0; j < n_resource; j++) {
if(need[i][j] > work[j]) {
can_execute = 0;
break;
}
}
if(can_execute) {
safe_sequence[count] = i;
count++;
finish[i] = 1;
flag = 1;
for(j = 0; j < n_resource; j++) {
work[j] += allocation[i][j];
}
}
}
}
if(!flag) {
break;
}
}
if(count == n_process) {
printf("Safe sequence: ");
for(i = 0; i < n_process; i++) {
printf("%d ", safe_sequence[i]);
}
printf("\n");
return 1;
} else {
printf("Unsafe state detected!\n");
return 0;
}
}
void bank_algorithm(int n_process, int n_resource) {
int i, j;
int request_process;
int request[MAX_RESOURCE];
int work[MAX_RESOURCE];
int finish[MAX_PROCESS] = {0};
printf("\nEnter request process number:");
scanf("%d", &request_process);
printf("\nEnter the request:\n");
for(i = 0; i < n_resource; i++) {
scanf("%d", &request[i]);
}
int can_grant = 1;
for(i = 0; i < n_resource; i++) {
if(request[i] > need[request_process][i] || request[i] > available[i]) {
can_grant = 0;
break;
}
}
if(can_grant) {
for(i = 0; i < n_resource; i++) {
available[i] -= request[i];
allocation[request_process][i] += request[i];
need[request_process][i] -= request[i];
}
if(is_safe(n_process, n_resource, available, finish)) {
printf("Request granted!\n");
} else {
for(i = 0; i < n_resource; i++) {
available[i] += request[i];
allocation[request_process][i] -= request[i];
need[request_process][i] += request[i];
}
printf("Request cannot be granted!\n");
}
} else {
printf("Request cannot be granted!\n");
}
}
int main() {
int n_process, n_resource;
int i, j;
printf("Enter number of processes:");
scanf("%d", &n_process);
printf("Enter number of resources:");
scanf("%d", &n_resource);
printf("Enter available resources:\n");
for(i = 0; i < n_resource; i++) {
scanf("%d", &available[i]);
}
printf("Enter maximum resources for each process:\n");
for(i = 0; i < n_process; i++) {
printf("Enter maximum resources for process %d:\n", i);
for(j = 0; j < n_resource; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter allocated resources for each process:\n");
for(i = 0; i < n_process; i++) {
printf("Enter allocated resources for process %d:\n", i);
for(j = 0; j < n_resource; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] = max[i][j] - allocation[i][j];
}
}
is_safe(n_process, n_resource, available, NULL);
bank_algorithm(n_process, n_resource);
return 0;
}
```
以上代码实现了银行家算法,可以输入可用资源数量、每个进程需求资源数量以及已分配资源数量来判断是否处于安全状态,并且可以按照某个进程的需求资源数量发起请求,并判断是否能够满足资源需求并保持安全状态。
阅读全文