由于提供的题目比较复杂,我先分步给出一些需要实现的功能,然后再提供一个大致的框架。
- 定义进程控制块(PCB),包括进程名、需要的资源总数、已分配的资源数、进程状态等信息。
- 定义资源总量、可用资源量和每个进程需要的最大资源数,存储在数组或结构体中。
- 构建一个初始时刻所有进程都是就绪状态的进程队列,每个时间片轮转调度算法从队列头出队一个进程运行。
- 运行一个时间片后,判断该进程是否完成。若完成,将已分配的资源释放,并将该进程从队列中删除,并唤醒等待该进程资源的阻塞进程;若未完成,则将该进程插入就绪队列末尾。
- 如果有阻塞进程,在每个时间片的结束时刻,按照银行家算法分配资源。如果无法分配,则将该进程插入阻塞队列尾部。
- 如果有已经阻塞的进程需要分配资源,则在每个时间片的开始时刻,重新按照银行家算法判断是否可分配。如果可以分配,则将该进程从阻塞队列中删除,并加入就绪队列中。
- 当所有进程都完成后,输出每个进程的状态和 PCB 信息。
以下是一个基本框架,仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESS_NUM 5
#define MAX_RESOURCE_NUM 3
#define TIME_SLICE 5 // 时间片长度(单位:秒)
#define BASE_DEMAND 2 // 进程最小资源需求
#define MAX_DEMAND 8 // 进程最大资源需求
// 进程状态
typedef enum {
WAIT,
RUN,
BLOCK,
FINISH
} ProcessState;
// 进程控制块
typedef struct {
char name[32];
int demand[MAX_RESOURCE_NUM]; // 需要的最大资源数
int allocated[MAX_RESOURCE_NUM]; // 已经分配的资源数
ProcessState state;
} PCB;
// 资源总数、可用资源量、每个进程需要的最大资源数
int resourceNum[MAX_RESOURCE_NUM] = {10, 15, 8};
int availableResource[MAX_RESOURCE_NUM] = {10, 15, 8};
int maxDemand[MAX_PROCESS_NUM][MAX_RESOURCE_NUM] = {
{8, 3, 4},
{5, 7, 3},
{3, 2, 5},
{6, 1, 3},
{2, 5, 4}
};
// 进程队列
typedef struct node {
PCB process;
struct node *next;
} PCBNode;
PCBNode *readyQueue = NULL, *blockQueue = NULL;
// 生成资源需求量(随机数)
void generateDemand(int *demand) {
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
demand[i] = BASE_DEMAND + rand() % (MAX_DEMAND - BASE_DEMAND + 1);
}
}
// 初始化进程控制块
void initPCB(PCB *pcb, char *name) {
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
generateDemand(pcb->demand);
pcb->allocated[i] = 0;
}
strcpy(pcb->name, name);
pcb->state = WAIT;
}
// 检查资源分配是否安全
int isSafe() {
int work[MAX_RESOURCE_NUM];
int finish[MAX_PROCESS_NUM] = {0};
int finishCount = 0;
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
work[i] = availableResource[i];
}
while (1) {
int currentProcess = -1;
for (int i = 0; i < MAX_PROCESS_NUM; i++) {
if (finish[i] == 0) {
int j;
for (j = 0; j < MAX_RESOURCE_NUM; j++) {
if (maxDemand[i][j] - readyQueue->process.allocated[j] > work[j])
break;
}
if (j == MAX_RESOURCE_NUM) {
currentProcess = i;
break;
}
}
}
if (currentProcess == -1)
break;
for (int j = 0; j < MAX_RESOURCE_NUM; j++)
work[j] += readyQueue->process.allocated[j];
finish[currentProcess] = 1;
finishCount++;
}
return finishCount == MAX_PROCESS_NUM;
}
// 分配资源
int allocateResource(PCB *pcb) {
if ( isSafe() ) {
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
availableResource[i] -= pcb->demand[i];
pcb->allocated[i] += pcb->demand[i];
}
return 1;
} else {
return 0;
}
}
// 释放资源
void releaseResource(PCB *pcb) {
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
availableResource[i] += pcb->allocated[i];
pcb->allocated[i] = 0;
}
}
// 创建新进程
int createProcess(PCB *newPCB) {
static int processCount = 0;
if (processCount >= MAX_PROCESS_NUM)
return 0;
initPCB(newPCB, "ProcessN");
newPCB->name[7] += processCount++;
return 1;
}
// 进程入队
void enqueue(PCBNode **queue, PCB process) {
PCBNode *newNode = (PCBNode *)malloc(sizeof(PCBNode));
newNode->process = process;
newNode->next = NULL;
if (*queue == NULL) {
*queue = newNode;
} else {
PCBNode *tmp = *queue;
while (tmp->next)
tmp = tmp->next;
tmp->next = newNode;
}
}
// 进程出队(队列头节点)
void dequeue(PCBNode **queue, PCB *process) {
if (*queue == NULL)
return;
PCBNode *tmp = *queue;
*process = tmp->process;
*queue = tmp->next;
free(tmp);
}
// 进程状态变更
void changeProcessState(PCB *pcb, ProcessState state) {
pcb->state = state;
}
// 输出进程信息
void printPCB() {
printf("-----------------------------\n");
printf("ready queue:\n");
PCBNode *tmp = readyQueue;
while (tmp) {
printf("%s ", tmp->process.name);
for (int i = 0; i < MAX_RESOURCE_NUM; i++)
printf("%d ", tmp->process.demand[i]); // 需要的最大资源数
printf("\n");
tmp = tmp->next;
}
printf("block queue:\n");
tmp = blockQueue;
while (tmp) {
printf("%s ", tmp->process.name);
for (int i = 0; i < MAX_RESOURCE_NUM; i++)
printf("%d ", tmp->process.demand[i]); // 需要的最大资源数
printf("\n");
tmp = tmp->next;
}
for (int i = 0; i < MAX_PROCESS_NUM; i++) {
printf("%s ", maxDemand[i]);
for (int j = 0; j < MAX_RESOURCE_NUM; j++) {
printf("%d ", maxDemand[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
PCB currentProcess;
int t, i;
srand((unsigned)time(NULL));
initPCB(¤tProcess, "Init");
enqueue(&readyQueue, currentProcess);
for (t = 0; t < 60; t += TIME_SLICE) {
if (readyQueue == NULL) {
printf("All processes finished.\n");
break;
}
printf("time:%d\n", t);
// 执行当前的进程
dequeue(&readyQueue, ¤tProcess);
changeProcessState(¤tProcess, RUN);
printf("%s running...\n", currentProcess.name);
sleep(TIME_SLICE);
generateDemand(currentProcess.demand);
// 判断当前进程是否完成
if (currentProcess.demand[0] <= 0 && currentProcess.demand[1] <= 0 && currentProcess.demand[2] <= 0) {
changeProcessState(¤tProcess, FINISH);
releaseResource(¤tProcess);
printf("%s finished.\n", currentProcess.name);
// 将等待当前进程资源的进程唤醒
PCBNode *tmp = blockQueue;
while (tmp) {
for (int i = 0; i < MAX_RESOURCE_NUM; i++) {
if (tmp->process.demand[i] > availableResource[i]) {
break;
}
}
PCBNode *next = tmp->next;
dequeue(&blockQueue, ¤tProcess);
enqueue(&readyQueue, currentProcess);
printf("%s unblock.\n", currentProcess.name);
tmp = next;
}
// 随机创建一个新进程
PCB newPCB;
if (createProcess(&newPCB)) {
if (allocateResource(&newPCB)) {
printf("%s created.\n", newPCB.name);
changeProcessState(&newPCB, WAIT);
enqueue(&readyQueue, newPCB);
} else {
printf("%s block.\n", newPCB.name);
changeProcessState(&newPCB, BLOCK);
enqueue(&blockQueue, newPCB);
}
}
} else {
// 将当前进程重新加入队列
changeProcessState(¤tProcess, WAIT);
enqueue(&readyQueue, currentProcess);
}
// 检查是否有进程需要分配资源
if (blockQueue != NULL) {
PCBNode *tmp = blockQueue;
while (tmp) {
for (i = 0; i < MAX_RESOURCE_NUM; i++) {
if (tmp->process.demand[i] > availableResource[i]) {
break;
}
}
if (i == MAX_RESOURCE_NUM) {
// 资源分配成功,唤醒等待当前进程的进程
PCBNode *next = tmp->next;
dequeue(&blockQueue, ¤tProcess);
enqueue(&readyQueue, currentProcess);
printf("%s unblock.\n", currentProcess.name);
tmp = next;
} else {
tmp = tmp->next;
}
}
}
// 打印进程信息
printPCB();
}
return 0;
}
本程序还存在许多错误和不完整的地方,需要根据实际情况进行调整和完善。