银行家算法(安全性检测及资源申请分配)c语言
时间: 2023-11-17 09:06:06 浏览: 139
银行家算法、安全性算法
下面是一个简单的银行家算法的C语言实现,仅供参考:
```c
#include <stdio.h>
#include <stdbool.h>
// 定义进程数和资源数的最大值
#define MAX_PROCESS 100
#define MAX_RESOURCES 100
// 进程结构体,用于存储每个进程的信息
typedef struct {
int id; // 进程ID
int max[MAX_RESOURCES]; // 最大资源需求量
int allocated[MAX_RESOURCES]; // 已分配的资源量
int need[MAX_RESOURCES]; // 资源需求量
bool finished; // 进程是否完成
} Process;
// 全局变量,存储系统中的进程和资源
Process processes[MAX_PROCESS];
int available[MAX_RESOURCES];
int n_processes, n_resources;
// 比较两个资源向量是否相等
bool is_equal(int a[], int b[]) {
for (int i = 0; i < n_resources; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
// 比较资源向量a是否小于等于资源向量b
bool is_less_than_or_equal(int a[], int b[]) {
for (int i = 0; i < n_resources; i++) {
if (a[i] > b[i]) {
return false;
}
}
return true;
}
// 处理资源请求
bool request_resources(int pid, int request[]) {
// 检查请求是否合法
if (!is_less_than_or_equal(request, processes[pid].need)) {
return false;
}
if (!is_less_than_or_equal(request, available)) {
return false;
}
// 假设分配资源后不会出现死锁
for (int i = 0; i < n_resources; i++) {
available[i] -= request[i];
processes[pid].allocated[i] += request[i];
processes[pid].need[i] -= request[i];
}
// 检查是否出现死锁
bool is_safe = false;
int work[n_resources];
bool finish[n_processes];
for (int i = 0; i < n_processes; i++) {
finish[i] = processes[i].finished;
}
while (!is_safe) {
// 搜索未完成的进程
int i;
for (i = 0; i < n_processes; i++) {
if (!finish[i] && is_less_than_or_equal(processes[i].need, available)) {
break;
}
}
if (i == n_processes) {
break;
}
// 分配资源
for (int j = 0; j < n_resources; j++) {
work[j] = available[j];
}
for (int j = 0; j < n_resources; j++) {
available[j] += processes[i].allocated[j];
processes[i].allocated[j] = 0;
processes[i].need[j] = 0;
processes[i].finished = true;
}
// 检查是否出现死锁
bool is_finish = true;
for (int j = 0; j < n_processes; j++) {
if (!finish[j] && is_less_than_or_equal(processes[j].need, available)) {
is_finish = false;
break;
}
}
if (is_finish) {
is_safe = true;
}
else {
for (int j = 0; j < n_resources; j++) {
available[j] = work[j];
}
for (int j = 0; j < n_resources; j++) {
processes[i].allocated[j] += request[j];
processes[i].need[j] -= request[j];
}
processes[i].finished = false;
}
}
// 如果出现死锁,撤销资源分配
if (!is_safe) {
for (int i = 0; i < n_resources; i++) {
available[i] += request[i];
processes[pid].allocated[i] -= request[i];
processes[pid].need[i] += request[i];
}
return false;
}
return true;
}
int main() {
// 输入进程数和资源数
printf("Enter number of processes: ");
scanf("%d", &n_processes);
printf("Enter number of resources: ");
scanf("%d", &n_resources);
// 输入每个进程的最大资源需求量和已分配的资源量
for (int i = 0; i < n_processes; i++) {
printf("Enter max resource needs for process %d: ", i);
for (int j = 0; j < n_resources; j++) {
scanf("%d", &processes[i].max[j]);
processes[i].need[j] = processes[i].max[j];
}
printf("Enter allocated resources for process %d: ", i);
for (int j = 0; j < n_resources; j++) {
scanf("%d", &processes[i].allocated[j]);
processes[i].need[j] -= processes[i].allocated[j];
}
processes[i].id = i;
processes[i].finished = false;
}
// 输入系统中每种资源的总量
printf("Enter available resources: ");
for (int i = 0; i < n_resources; i++) {
scanf("%d", &available[i]);
}
// 处理资源请求
int pid, request[MAX_RESOURCES];
printf("Enter process ID and resource request (e.g. 1 2 3): ");
scanf("%d", &pid);
for (int i = 0; i < n_resources; i++) {
scanf("%d", &request[i]);
}
if (request_resources(pid, request)) {
printf("Request granted.\n");
}
else {
printf("Request denied.\n");
}
return 0;
}
```
阅读全文