用c语言编写一个银行家算法
时间: 2023-12-07 15:02:59 浏览: 109
银行家算法是一种资源分配算法,用于避免死锁的产生。以下是用C语言实现银行家算法的代码示例:
```c
#include <stdio.h>
#include <stdlib.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 finish[MAX_PROCESS] = {0};
int safe_sequence[MAX_PROCESS];
int num_processes, num_resources;
void input()
{
int i, j;
printf("Enter number of processes: ");
scanf("%d", &num_processes);
printf("Enter number of resources: ");
scanf("%d", &num_resources);
printf("Enter available resources: ");
for (i = 0; i < num_resources; i++) {
scanf("%d", &available[i]);
}
printf("Enter maximum resources needed by each process:\n");
for (i = 0; i < num_processes; i++) {
printf("Process %d: ", i);
for (j = 0; j < num_resources; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter resources allocated to each process:\n");
for (i = 0; i < num_processes; i++) {
printf("Process %d: ", i);
for (j = 0; j < num_resources; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int safety_check()
{
int i, j, k, count = 0, safe = 0;
int work[MAX_RESOURCE];
for (i = 0; i < num_resources; i++) {
work[i] = available[i];
}
while (count < num_processes) {
safe = 0;
for (i = 0; i < num_processes; i++) {
if (finish[i] == 0) {
int flag = 1;
for (j = 0; j < num_resources; j++) {
if (need[i][j] > work[j]) {
flag = 0;
break;
}
}
if (flag) {
for (j = 0; j < num_resources; j++) {
work[j] += allocation[i][j];
}
finish[i] = 1;
safe_sequence[count++] = i;
safe = 1;
}
}
}
if (!safe) {
break;
}
}
if (count == num_processes) {
printf("Safe sequence: ");
for (i = 0; i < num_processes; i++) {
printf("%d ", safe_sequence[i]);
}
printf("\n");
return 1;
} else {
printf("System is in unsafe state.\n");
return 0;
}
}
int request_resources(int pid, int *request)
{
int i, j, safe = 0;
for (i = 0; i < num_resources; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
printf("Error: Requested resources exceed max or available.\n");
return 0;
}
}
for (i = 0; i < num_resources; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (safety_check()) {
printf("System is in safe state.\n");
safe = 1;
} else {
printf("System is in unsafe state, rolling back.\n");
for (i = 0; i < num_resources; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
return safe;
}
int release_resources(int pid, int *release)
{
int i;
for (i = 0; i < num_resources; i++) {
if (release[i] > allocation[pid][i]) {
printf("Error: Released resources exceed allocated.\n");
return 0;
}
}
for (i = 0; i < num_resources; i++) {
available[i] += release[i];
allocation[pid][i] -= release[i];
need[pid][i] += release[i];
}
return 1;
}
int main()
{
int i, j, pid;
int request[MAX_RESOURCE], release[MAX_RESOURCE];
input();
safety_check();
while (1) {
printf("Enter process ID (or -1 to exit): ");
scanf("%d", &pid);
if (pid == -1) {
break;
}
printf("Enter request: ");
for (i = 0; i < num_resources; i++) {
scanf("%d", &request[i]);
}
if (request_resources(pid, request)) {
printf("Resources allocated.\n");
} else {
printf("Resources not allocated.\n");
}
printf("Enter release: ");
for (i = 0; i < num_resources; i++) {
scanf("%d", &release[i]);
}
if (release_resources(pid, release)) {
printf("Resources released.\n");
} else {
printf("Resources not released.\n");
}
printf("Available resources: ");
for (i = 0; i < num_resources; i++) {
printf("%d ", available[i]);
}
printf("\n");
printf("Allocation:\n");
for (i = 0; i < num_processes; i++) {
for (j = 0; j < num_resources; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}
printf("Need:\n");
for (i = 0; i < num_processes; i++) {
for (j = 0; j < num_resources; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
}
return 0;
}
```
阅读全文