银行家算法模拟程序C语言
时间: 2023-12-06 09:04:21 浏览: 58
以下是一个简单的银行家算法模拟程序的C语言实现:
```c
#include <stdio.h>
#include <stdbool.h>
#define P 5 // 进程数
#define R 3 // 资源数
int available[R]; // 可用资源数
int max[P][R]; // 最大需求矩阵
int allocation[P][R]; // 已分配矩阵
int need[P][R]; // 需求矩阵
bool finish[P]; // 进程是否完成
void init() {
// 初始化可用资源数
available[0] = 3;
available[1] = 3;
available[2] = 2;
// 初始化最大需求矩阵
max[0][0] = 7;
max[0][1] = 5;
max[0][2] = 3;
max[1][0] = 3;
max[1][1] = 2;
max[1][2] = 2;
max[2][0] = 9;
max[2][1] = 0;
max[2][2] = 2;
max[3][0] = 2;
max[3][1] = 2;
max[3][2] = 2;
max[4][0] = 4;
max[4][1] = 3;
max[4][2] = 3;
// 初始化已分配矩阵
allocation[0][0] = 0;
allocation[0][1] = 1;
allocation[0][2] = 0;
allocation[1][0] = 2;
allocation[1][1] = 0;
allocation[1][2] = 0;
allocation[2][0] = 3;
allocation[2][1] = 0;
allocation[2][2] = 2;
allocation[3][0] = 2;
allocation[3][1] = 1;
allocation[3][2] = 1;
allocation[4][0] = 0;
allocation[4][1] = 0;
allocation[4][2] = 2;
// 初始化需求矩阵
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
// 初始化进程是否完成
for (int i = 0; i < P; i++) {
finish[i] = false;
}
}
bool is_safe() {
int work[R];
for (int i = 0; i < R; i++) {
work[i] = available[i];
}
bool finish_all = false;
while (!finish_all) {
bool can_alloc = false;
for (int i = 0; i < P; i++) {
if (!finish[i]) {
bool can_finish = true;
for (int j = 0; j < R; j++) {
if (need[i][j] > work[j]) {
can_finish = false;
break;
}
}
if (can_finish) {
can_alloc = true;
finish[i] = true;
for (int j = 0; j < R; j++) {
work[j] += allocation[i][j];
}
}
}
}
if (!can_alloc) {
finish_all = true;
for (int i = 0; i < P; i++) {
if (!finish[i]) {
return false;
}
}
}
}
return true;
}
void request(int pid, int request[R]) {
bool can_alloc = true;
for (int i = 0; i < R; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
can_alloc = false;
break;
}
}
if (can_alloc) {
for (int i = 0; i < R; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (is_safe()) {
printf("分配成功,安全序列为:");
for (int i = 0; i < P; i++) {
if (finish[i]) {
printf("%d ", i);
}
}
printf("\n");
} else {
printf("分配失败,不安全状态\n");
for (int i = 0; i < R; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
} else {
printf("分配失败,请求资源超过需求或可用资源\n");
}
}
void show() {
printf("当前时刻各进程的资源分配情况:\n");
printf("进程\t最大需求\t已分配\t\t需求\t\t可用资源\n");
for (int i = 0; i < P; i++) {
printf("%d\t", i);
for (int j = 0; j < R; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
for (int j = 0; j < R; j++) {
printf("%d ", allocation[i][j]);
}
printf("\t\t");
for (int j = 0; j < R; j++) {
printf("%d ", need[i][j]);
}
printf("\t\t");
if (i == 0) {
printf("%d %d %d\n", available[0], available[1], available[2]);
} else {
printf("\n");
}
}
}
int main() {
init();
show();
int pid;
int request[R];
while (true) {
printf("请输入进程编号和请求资源数(格式:pid r1 r2 r3):");
scanf("%d %d %d %d", &pid, &request[0], &request[1], &request[2]);
request(pid, request);
show();
}
return 0;
}
```