azure011328

导航

 

#include <stdio.h>
#include <stdlib.h>

// 进程控制块结构
struct PCB {
char name[10]; // 进程标识符
int prio; // 进程优先数
int round; // 轮转时间片数
int cputime; // 进程累计占用CPU的时间片数
int needtime; // 进程到完成还需要的时间片数
char state; // 进程状态(R:运行, W:就绪, F:完成)
struct PCB *next; // 链指针
};

// 全局变量
struct PCB *run = NULL; // 当前运行进程指针
struct PCB *ready = NULL; // 就绪队列头指针
struct PCB *tail = NULL; // 就绪队列尾指针
struct PCB *finish = NULL; // 完成队列头指针

// 函数声明
void create();
void prisch();
void roundsch();
void priority_run();
void round_run();
void print();
void insert1(struct PCB *p);
void insert2(struct PCB *p);

// 创建进程
void create() {
struct PCB *p;
int num, i;

printf("请输入要创建的进程数量: ");
scanf("%d", &num);

for(i = 0; i < num; i++) {
p = (struct PCB*)malloc(sizeof(struct PCB));
printf("请输入进程%d的名称和需要的时间片数: ", i+1);
scanf("%s %d", p->name, &p->needtime);

p->cputime = 0;
p->round = 2; // 轮转时间片固定为2
p->prio = 50 - p->needtime; // 初始优先级
p->state = 'W';
p->next = NULL;

// 插入就绪队列
if(ready == NULL) {
ready = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
}

// 优先级调度选择
void prisch() {
struct PCB *p, *max_prio_p, *prev;
int max_prio;

if(ready == NULL) return;

// 找出优先级最高的进程
p = ready;
prev = NULL;
max_prio = p->prio;
max_prio_p = p;

while(p != NULL) {
if(p->prio > max_prio) {
max_prio = p->prio;
max_prio_p = p;
}
p = p->next;
}

// 从就绪队列中移除该进程
if(max_prio_p == ready) {
ready = ready->next;
} else {
p = ready;
while(p->next != max_prio_p) {
p = p->next;
}
p->next = max_prio_p->next;
}

// 设置为运行状态
max_prio_p->state = 'R';
run = max_prio_p;
}

// 轮转调度选择
void roundsch() {
if(ready == NULL) return;

// 取出就绪队列第一个进程
run = ready;
ready = ready->next;
run->next = NULL;
run->state = 'R';
}

// 优先级调度执行
void priority_run() {
while(run != NULL || ready != NULL) {
if(run == NULL) {
prisch(); // 调度
if(run == NULL && ready != NULL) {
// 确保如果就绪队列不空,一定要调度一个进程
prisch();
}
}

if(run != NULL) {
// 执行一个时间片
run->cputime++;
run->needtime--;
run->prio--;

if(run->needtime == 0) {
// 进程完成
run->state = 'F';
run->next = finish;
finish = run;
run = NULL;

// 完成一个进程后立即尝试调度下一个
if(ready != NULL) {
prisch();
}
} else {
// 重新插入就绪队列
run->state = 'W';
insert1(run);
run = NULL;

// 立即尝试调度下一个
if(ready != NULL) {
prisch();
}
}
}

print(); // 打印当前状态
}
}

// 轮转调度执行
void round_run() {
while(run != NULL || ready != NULL) {
if(run == NULL) {
roundsch(); // 调度
}

if(run != NULL) {
// 执行2个时间片(一个轮转单位)
run->cputime += 2;
run->needtime -= 2;

if(run->needtime <= 0) {
// 进程完成
run->state = 'F';
run->next = finish;
finish = run;
run = NULL;
} else {
// 重新插入就绪队列末尾
run->state = 'W';
insert2(run);
run = NULL;
}
}

print(); // 打印当前状态
}
}

// 打印状态
void print() {
struct PCB *p;

printf("\n当前运行进程:\n");
if(run != NULL) {
printf("name\tcputime\tneedtime\tpriority\tstate\n");
printf("%s\t%d\t%d\t\t%d\t\t%c\n",
run->name, run->cputime, run->needtime, run->prio, run->state);
} else {
printf("无\n");
}

printf("\n就绪队列:\n");
p = ready;
if(p == NULL) {
printf("无\n");
} else {
printf("name\tcputime\tneedtime\tpriority\tstate\n");
while(p != NULL) {
printf("%s\t%d\t%d\t\t%d\t\t%c\n",
p->name, p->cputime, p->needtime, p->prio, p->state);
p = p->next;
}
}

printf("\n完成队列:\n");
p = finish;
if(p == NULL) {
printf("无\n");
} else {
printf("name\tcputime\tneedtime\tpriority\tstate\n");
while(p != NULL) {
printf("%s\t%d\t%d\t\t%d\t\t%c\n",
p->name, p->cputime, p->needtime, p->prio, p->state);
p = p->next;
}
}
printf("------------------------------------------------\n");
}

// 按优先级插入就绪队列
void insert1(struct PCB *p) {
struct PCB *q, *prev;

p->state = 'W';

if(ready == NULL) {
ready = p;
tail = p;
return;
}

// 按优先级插入到合适位置
q = ready;
prev = NULL;

while(q != NULL && q->prio >= p->prio) {
prev = q;
q = q->next;
}

if(prev == NULL) {
p->next = ready;
ready = p;
} else {
p->next = prev->next;
prev->next = p;
}

if(p->next == NULL) {
tail = p;
}
}

// 按FIFO插入就绪队列(队尾)
void insert2(struct PCB *p) {
p->state = 'W';
p->next = NULL;

if(ready == NULL) {
ready = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}

int main() {
int algorithm;

printf("请选择调度算法(1-优先级, 2-轮转): ");
scanf("%d", &algorithm);

create(); // 创建进程

if(algorithm == 1) {
printf("\n开始优先级调度算法:\n");
priority_run();
} else {
printf("\n开始时间片轮转调度算法:\n");
round_run();
}

return 0;
}

posted on 2025-06-11 08:41  淮竹i  阅读(5)  评论(0)    收藏  举报