c语言银行家算法运行实例
时间: 2023-10-04 15:14:17 浏览: 144
C语言 银行家算法的实现
以下是一个简单的银行家算法的实现示例,使用 C 语言编写:
```c
#include <stdio.h>
#include <stdbool.h>
#define NUM_RESOURCES 3
#define NUM_PROCESSES 5
int available[NUM_RESOURCES] = {3, 3, 2};
int max_need[NUM_PROCESSES][NUM_RESOURCES] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3},
};
int allocated[NUM_PROCESSES][NUM_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2},
};
// 计算已分配资源和需要资源
void calculate_need(int need[NUM_PROCESSES][NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) {
for (int i = 0; i < NUM_PROCESSES; i++) {
for (int j = 0; j < NUM_RESOURCES; j++) {
need[i][j] = max_need[i][j] - allocated[i][j];
}
}
}
// 判断一个进程是否需要的资源是否小于等于可用资源
bool check_process(int process, int need[NUM_PROCESSES][NUM_RESOURCES], int available[NUM_RESOURCES]) {
for (int i = 0; i < NUM_RESOURCES; i++) {
if (need[process][i] > available[i]) {
return false;
}
}
return true;
}
// 判断当前状态是否安全
bool check_safe(int available[NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) {
int need[NUM_PROCESSES][NUM_RESOURCES];
calculate_need(need, allocated, max_need);
bool finish[NUM_PROCESSES] = {false};
int work[NUM_RESOURCES];
for (int i = 0; i < NUM_RESOURCES; i++) {
work[i] = available[i];
}
int safe_seq[NUM_PROCESSES];
int count = 0;
while (count < NUM_PROCESSES) {
bool found = false;
for (int i = 0; i < NUM_PROCESSES; i++) {
if (!finish[i] && check_process(i, need, work)) {
finish[i] = true;
for (int j = 0; j < NUM_RESOURCES; j++) {
work[j] += allocated[i][j];
}
safe_seq[count] = i;
count++;
found = true;
}
}
if (!found) {
return false;
}
}
return true;
}
// 请求资源
bool request_resources(int process, int request[NUM_RESOURCES], int available[NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) {
int need[NUM_PROCESSES][NUM_RESOURCES];
calculate_need(need, allocated, max_need);
for (int i = 0; i < NUM_RESOURCES; i++) {
if (request[i] > need[process][i]) {
return false;
}
if (request[i] > available[i]) {
return false;
}
}
for (int i = 0; i < NUM_RESOURCES; i++) {
available[i] -= request[i];
allocated[process][i] += request[i];
need[process][i] -= request[i];
}
if (!check_safe(available, allocated, max_need)) {
for (int i = 0; i < NUM_RESOURCES; i++) {
available[i] += request[i];
allocated[process][i] -= request[i];
need[process][i] += request[i];
}
return false;
}
return true;
}
// 打印状态
void print_status() {
printf("可用资源:");
for (int i = 0; i < NUM_RESOURCES; i++) {
printf(" %d", available[i]);
}
printf("\n最大需求:\n");
for (int i = 0; i < NUM_PROCESSES; i++) {
printf("进程 %d:", i);
for (int j = 0; j < NUM_RESOURCES; j++) {
printf(" %d", max_need[i][j]);
}
printf("\n");
}
printf("已分配资源:\n");
for (int i = 0; i < NUM_PROCESSES; i++) {
printf("进程 %d:", i);
for (int j = 0; j < NUM_RESOURCES; j++) {
printf(" %d", allocated[i][j]);
}
printf("\n");
}
}
// 测试代码
int main() {
print_status();
if (check_safe(available, allocated, max_need)) {
printf("当前状态安全\n");
} else {
printf("当前状态不安全\n");
}
int request[NUM_RESOURCES] = {1, 0, 2};
if (request_resources(1, request, available, allocated, max_need)) {
printf("请求资源成功\n");
print_status();
} else {
printf("请求资源失败\n");
}
return 0;
}
```
运行上述代码,将输出以下结果:
```
可用资源: 3 3 2
最大需求:
进程 0: 7 5 3
进程 1: 3 2 2
进程 2: 9 0 2
进程 3: 2 2 2
进程 4: 4 3 3
已分配资源:
进程 0: 0 1 0
进程 1: 2 0 0
进程 2: 3 0 2
进程 3: 2 1 1
进程 4: 0 0 2
当前状态安全
请求资源成功
可用资源: 2 3 0
最大需求:
进程 0: 7 5 3
进程 1: 3 2 2
进程 2: 9 0 2
进程 3: 2 2 2
进程 4: 4 3 3
已分配资源:
进程 0: 0 1 0
进程 1: 3 0 2
进程 2: 3 0 2
进程 3: 2 1 1
进程 4: 0 0 2
```
可以看到,初始状态下当前状态是安全的,请求资源成功后仍然是安全状态。
阅读全文