设计3—4个并发进程,共享系统的10个同类不可抢占资源。各进程按银行家算法动态进行资源的申请和释放,编写程序模拟这一执行过程
时间: 2024-03-12 20:47:29 浏览: 99
下面是一个基于C语言的模拟程序,实现了3个并发进程,共享10个同类不可抢占资源,采用银行家算法进行资源申请和释放。
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define N 3 // 进程数
#define M 10 // 资源数
// 进程结构体,包含进程ID、所需最大资源数、已分配资源数和需要资源数
struct Process {
int pid; // 进程ID
int max_res[M]; // 所需最大资源数
int allocated_res[M]; // 已分配资源数
int need_res[M]; // 需要资源数
};
// 系统可用资源数
int available_res[M];
// 所有进程的数据结构
struct Process procs[N];
// 初始化资源分配情况
void init() {
int i, j;
srand(time(NULL));
// 初始化系统可用资源数
for (i = 0; i < M; i++) {
available_res[i] = rand() % 5 + 1; // 每个资源数在1-5之间随机分配
}
// 初始化每个进程的最大资源数和需要资源数
for (i = 0; i < N; i++) {
procs[i].pid = i + 1;
for (j = 0; j < M; j++) {
procs[i].max_res[j] = rand() % 5 + 1; // 每个资源数在1-5之间随机分配
procs[i].allocated_res[j] = 0;
procs[i].need_res[j] = procs[i].max_res[j];
}
}
}
// 显示当前资源分配情况
void show() {
int i, j;
printf("Available resource:\n");
for (i = 0; i < M; i++) {
printf("%d ", available_res[i]);
}
printf("\n");
printf("Process\tMax\tAllocated\tNeed\n");
for (i = 0; i < N; i++) {
printf("%d\t", procs[i].pid);
for (j = 0; j < M; j++) {
printf("%d ", procs[i].max_res[j]);
}
printf("\t");
for (j = 0; j < M; j++) {
printf("%d ", procs[i].allocated_res[j]);
}
printf("\t");
for (j = 0; j < M; j++) {
printf("%d ", procs[i].need_res[j]);
}
printf("\n");
}
}
// 检查当前请求是否合法
bool is_safe(int pid, int request[]) {
int i, j;
int work[M]; // 可用资源数
int finish[N]; // 进程是否完成
// 初始化work和finish数组
for (i = 0; i < M; i++) {
work[i] = available_res[i] - request[i];
}
for (i = 0; i < N; i++) {
finish[i] = false;
}
// 模拟进程执行,直到所有进程都完成或无法继续执行
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (!finish[j] && procs[j].need_res[0] <= work[0] && procs[j].need_res[1] <= work[1] && procs[j].need_res[2] <= work[2] && procs[j].need_res[3] <= work[3] && procs[j].need_res[4] <= work[4] && procs[j].need_res[5] <= work[5] && procs[j].need_res[6] <= work[6] && procs[j].need_res[7] <= work[7] && procs[j].need_res[8] <= work[8] && procs[j].need_res[9] <= work[9]) {
// 进程j可以执行,释放所占用的资源
for (int k = 0; k < M; k++) {
work[k] += procs[j].allocated_res[k];
}
finish[j] = true;
}
}
}
// 检查进程是否全部完成,如果完成则说明当前请求是安全的
for (i = 0; i < N; i++) {
if (!finish[i]) {
return false;
}
}
return true;
}
// 进程pid请求资源
void request(int pid, int request[]) {
int i;
if (request[0] > procs[pid - 1].need_res[0] || request[1] > procs[pid - 1].need_res[1] || request[2] > procs[pid - 1].need_res[2] || request[3] > procs[pid - 1].need_res[3] || request[4] > procs[pid - 1].need_res[4] || request[5] > procs[pid - 1].need_res[5] || request[6] > procs[pid - 1].need_res[6] || request[7] > procs[pid - 1].need_res[7] || request[8] > procs[pid - 1].need_res[8] || request[9] > procs[pid - 1].need_res[9]) {
printf("Error: Request exceeds process's need!\n");
return;
}
if (request[0] > available_res[0] || request[1] > available_res[1] || request[2] > available_res[2] || request[3] > available_res[3] || request[4] > available_res[4] || request[5] > available_res[5] || request[6] > available_res[6] || request[7] > available_res[7] || request[8] > available_res[8] || request[9] > available_res[9]) {
printf("Error: Not enough available resource!\n");
return;
}
if (!is_safe(pid, request)) {
printf("Error: Unsafe state, request denied!\n");
return;
}
// 分配资源
for (i = 0; i < M; i++) {
available_res[i] -= request[i];
procs[pid - 1].allocated_res[i] += request[i];
procs[pid - 1].need_res[i] -= request[i];
}
printf("Request granted!\n");
}
// 进程pid释放资源
void release(int pid, int release[]) {
int i;
if (release[0] > procs[pid - 1].allocated_res[0] || release[1] > procs[pid - 1].allocated_res[1] || release[2] > procs[pid - 1].allocated_res[2] || release[3] > procs[pid - 1].allocated_res[3] || release[4] > procs[pid - 1].allocated_res[4] || release[5] > procs[pid - 1].allocated_res[5] || release[6] > procs[pid - 1].allocated_res[6] || release[7] > procs[pid - 1].allocated_res[7] || release[8] > procs[pid - 1].allocated_res[8] || release[9] > procs[pid - 1].allocated_res[9]) {
printf("Error: Release exceeds process's allocation!\n");
return;
}
// 释放资源
for (i = 0; i < M; i++) {
available_res[i] += release[i];
procs[pid - 1].allocated_res[i] -= release[i];
procs[pid - 1].need_res[i] += release[i];
}
printf("Resource released!\n");
}
int main() {
int i, j;
int choice;
int pid;
int request[M];
int release[M];
init();
while (true) {
printf("\n");
show();
printf("Enter 1 to request resources, 2 to release resources, 0 to exit: ");
scanf("%d", &choice);
if (choice == 0) {
break;
}
printf("Enter process ID: ");
scanf("%d", &pid);
if (pid < 1 || pid > N) {
printf("Error: Invalid process ID!\n");
continue;
}
printf("Enter resource request or release:\n");
for (i = 0; i < M; i++) {
if (choice == 1) {
printf("Resource %d request: ", i + 1);
scanf("%d", &request[i]);
} else {
printf("Resource %d release: ", i + 1);
scanf("%d", &release[i]);
}
}
if (choice == 1) {
request(pid, request);
} else {
release(pid, release);
}
}
return 0;
}
```
该程序采用银行家算法,模拟了3个并发进程,共享10个同类不可抢占资源。程序首先随机分配每个进程的最大资源数和系统可用资源数,然后循环读取用户输入的请求或释放资源的操作,并调用相应的函数进行处理。在处理请求或释放资源时,程序会先检查请求是否合法,并通过银行家算法判断当前状态是否安全。如果请求合法且当前状态安全,程序会分配或释放资源,并更新进程的已分配资源数和需要资源数。最后程序会输出当前资源分配情况和每个进程的最大资源数、已分配资源数和需要资源数。
阅读全文