写出动态优先级调度算法的C语言代码,具体要求如下: 1.数据结构设计 PCB结构: name 进程名 pri /round 进程优先数/进程轮转时间片 cputime 进程占用的 CPU时间 needtime 进程到完成还要的时间 状态进程状态(假设状态为Ready、Run、Finish) next 链指针 2.算法设计 时间以时间片为计量单位。1) 系统初始化时给每一个进程赋一个NEEDTIME和初始PRI。并按优先数入队。2) 系统每次选定一个优先级最高的进程投入运行,进程每执行一次,并将它的进程占用
时间: 2024-02-13 20:03:26 浏览: 33
以下是动态优先级调度算法的C语言代码实现:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// 定义进程状态
#define READY 0
#define RUN 1
#define FINISH 2
// 定义进程控制块(PCB)结构体
typedef struct pcb {
char name[10]; // 进程名
int pri; // 进程优先数
int round; // 进程轮转时间片
int cputime; // 进程占用的CPU时间
int needtime; // 进程到完成还要的时间
int status; // 进程状态
struct pcb *next; // 链指针
} PCB;
// 定义全局变量
PCB *ready = NULL; // 就绪队列
PCB *run = NULL; // 运行队列
PCB *finish = NULL; // 完成队列
int timeSlice = 2; // 时间片大小
// 初始化进程
void initProcess(PCB *p) {
printf("请输入进程名:");
scanf("%s", p->name);
printf("请输入进程优先数:");
scanf("%d", &p->pri);
printf("请输入进程初始时间片:");
scanf("%d", &p->round);
printf("请输入进程需要时间:");
scanf("%d", &p->needtime);
p->cputime = 0;
p->status = READY;
p->next = NULL;
}
// 将进程插入就绪队列
void insertReady(PCB *p) {
PCB *q, *s;
if (ready == NULL) {
ready = p;
} else if (p->pri > ready->pri) {
p->next = ready;
ready = p;
} else {
q = ready;
s = q->next;
while (s != NULL && p->pri <= s->pri) {
q = s;
s = s->next;
}
q->next = p;
p->next = s;
}
}
// 选择优先级最高的进程
PCB *selectHighestPriProcess() {
PCB *p = ready;
PCB *q = NULL;
if (p != NULL) {
ready = p->next;
p->next = NULL;
p->status = RUN;
if (run != NULL) {
run->status = READY;
q = run;
while (q->next != NULL) {
q = q->next;
}
q->next = run;
}
run = p;
}
return p;
}
// 进程运行
void runProcess() {
if (run != NULL) {
run->cputime += timeSlice;
run->needtime -= timeSlice;
if (run->needtime <= 0) {
run->status = FINISH;
if (finish == NULL) {
finish = run;
} else {
PCB *p = finish;
while (p->next != NULL) {
p = p->next;
}
p->next = run;
}
run = NULL;
} else if (run->round <= 0) {
run->status = READY;
run->round = run->pri;
insertReady(run);
run = NULL;
} else {
run->round -= timeSlice;
}
}
}
// 输出进程状态
void printProcessStatus(PCB *p) {
printf("%-10s%-10d%-10d%-10d%-10d%-10d\n", p->name, p->pri, p->round, p->cputime, p->needtime, p->status);
}
// 输出进程列表
void printProcessList(PCB *p) {
printf("%-10s%-10s%-10s%-10s%-10s%-10s\n", "Name", "Pri", "Round", "CPUTime", "NeedTime", "Status");
while (p != NULL) {
printProcessStatus(p);
p = p->next;
}
}
int main() {
int n, i;
PCB *p;
printf("请输入进程数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
p = (PCB *) malloc(sizeof(PCB));
initProcess(p);
insertReady(p);
}
printf("\n");
while (finish == NULL || finish->next != NULL) {
printProcessList(ready);
printProcessList(run);
printProcessList(finish);
printf("\n");
selectHighestPriProcess();
runProcess();
}
printf("\n");
printProcessList(finish);
return 0;
}
```
以上代码实现了动态优先级调度算法,包括进程的初始化、插入就绪队列、选择最高优先级进程、进程的运行以及进程状态的输出等功能。注意,以上代码仅供参考,实际应用中需要根据具体环境进行调整和优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)