1.给出进程调度的算法描述(如基于动态优先级和时间片轮转调度算法的描述)。 2.用C语言设计一个对n个并发进程进行调度的程序,每个进程由一个进程控制块(PCB)结构表示,该进程控制块应包括下述信息:进程标识ID、进程优先数PRIORITY(并规定优先数与优先权成正比)、时间片数CHIP、进程已经占用CPU的时间CPUTIME,进程还需要运行的时间ALLTIME(当进程运行完毕时,其值为0)、进程的状态STATE(为简化起见。设每个进程处于运行E(excecuting)、就绪R(ready)和完成F(finish)三种状态之一,并假设起始状态都是就绪状态R。),以及进程队列指针NEXT(用来将PCB排成队列)等,可按照调度算法的不同而增删。 3.调度程序应当包含2种不同的调度算法,运行时可以任选一种,以利于各种方法的分析和比较。 4.程序应能显示或打印各种进程状态和参数变化情况,便于观察。即要显示每个时间片内各进程的情况,并且指出运行进程及就绪和阻塞队列中的内容。
时间: 2024-01-22 09:20:13 浏览: 65
1. 进程调度算法的描述:
基于动态优先级调度算法:
每个进程都有一个动态优先级,每次执行完毕后,根据进程的情况重新计算优先级。系统会选择优先级最高的进程来运行。如果有多个进程优先级相同,则采用时间片轮转调度。
时间片轮转调度算法:
每个进程被分配一个时间片,当时间片用完后,如果进程还未执行完毕,则将其放入就绪队列尾部,等待下一次时间片到来。如果所有进程时间片用完后,所有进程还未执行完毕,则重新开始下一轮时间片轮转调度。
2. 用C语言设计一个对n个并发进程进行调度的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10 // 最大进程数
typedef enum {
READY,
RUNNING,
FINISHED
} State;
typedef struct {
int pid; // 进程ID
int priority; // 进程优先数
int chip; // 时间片数
int cputime; // 进程已经占用CPU的时间
int alltime; // 进程还需要运行的时间
State state; // 进程状态
struct PCB *next; // 进程队列指针
} PCB;
PCB *ready_queue = NULL;
PCB *running_process = NULL;
PCB *finished_queue = NULL;
// 将进程插入就绪队列
void insert_ready(PCB *p) {
PCB *ptr = ready_queue;
if (ready_queue == NULL) { // 如果队列为空,直接插入
ready_queue = p;
p->next = NULL;
return;
}
if (p->priority > ptr->priority) { // 如果优先级最高,插入队首
p->next = ptr;
ready_queue = p;
return;
}
while (ptr->next != NULL && p->priority <= ptr->next->priority) { // 查找插入位置
ptr = ptr->next;
}
p->next = ptr->next;
ptr->next = p;
}
// 将进程插入完成队列
void insert_finished(PCB *p) {
PCB *ptr = finished_queue;
if (finished_queue == NULL) { // 如果队列为空,直接插入
finished_queue = p;
p->next = NULL;
return;
}
while (ptr->next != NULL) { // 查找插入位置
ptr = ptr->next;
}
ptr->next = p;
p->next = NULL;
}
// 创建进程
PCB *create_process(int pid, int priority, int chip, int alltime) {
PCB *p = (PCB *)malloc(sizeof(PCB));
p->pid = pid;
p->priority = priority;
p->chip = chip;
p->cputime = 0;
p->alltime = alltime;
p->state = READY;
p->next = NULL;
return p;
}
// 进程调度函数
void schedule() {
PCB *p;
if (running_process != NULL && running_process->chip > 0) { // 如果进程时间片未用完,继续运行
running_process->chip--;
running_process->cputime++;
running_process->alltime--;
if (running_process->alltime == 0) { // 如果进程执行完毕,将其插入完成队列
running_process->state = FINISHED;
insert_finished(running_process);
running_process = NULL;
} else if (running_process->chip == 0) { // 如果进程时间片用完,将其插入就绪队列
running_process->state = READY;
insert_ready(running_process);
running_process = NULL;
}
}
if (running_process == NULL && ready_queue != NULL) { // 如果当前没有进程在运行,从就绪队列中选择优先级最高的进程运行
p = ready_queue;
ready_queue = ready_queue->next;
p->state = RUNNING;
p->chip = 2;
running_process = p;
}
}
// 显示进程状态
void show_processes() {
PCB *p;
printf("Running process:\n");
if (running_process == NULL) {
printf("None\n");
} else {
printf("PID: %d, PRIORITY: %d, CPUTIME: %d, ALLTIME: %d\n", running_process->pid, running_process->priority, running_process->cputime, running_process->alltime);
}
printf("Ready queue:\n");
p = ready_queue;
while (p != NULL) {
printf("PID: %d, PRIORITY: %d, ALLTIME: %d\n", p->pid, p->priority, p->alltime);
p = p->next;
}
printf("Finished queue:\n");
p = finished_queue;
while (p != NULL) {
printf("PID: %d, PRIORITY: %d, CPUTIME: %d\n", p->pid, p->priority, p->cputime);
p = p->next;
}
}
int main() {
int n, i, pid, priority, chip, alltime;
PCB *p;
printf("Enter number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter PID, priority, chip and alltime for process %d: ", i + 1);
scanf("%d%d%d%d", &pid, &priority, &chip, &alltime);
p = create_process(pid, priority, chip, alltime);
insert_ready(p);
}
printf("\n");
while (finished_queue == NULL) { // 所有进程未执行完毕时循环
show_processes();
schedule();
printf("\n");
}
return 0;
}
```
3. 调度程序应当包含2种不同的调度算法:
基于动态优先级调度算法和时间片轮转调度算法。
4. 程序应能显示或打印各种进程状态和参数变化情况,便于观察。即要显示每个时间片内各进程的情况,并且指出运行进程及就绪和阻塞队列中的内容。
程序中的 `show_processes` 函数可以显示每个时间片内各进程的情况,包括运行进程及就绪和完成队列中的内容。
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)