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 10:19:54 浏览: 30
1. 进程调度算法描述:
动态优先级调度算法:
动态优先级调度算法根据进程的实时情况动态改变进程的优先级,一般是根据进程等待时间、执行时间、优先级等因素来动态调整优先级。当一个进程等待的时间越长,其优先级也就越高,以便能够尽快执行。
时间片轮转调度算法:
时间片轮转调度算法是指每个进程被分配一个时间片,当进程用完时间片后,就会被放到队列的末尾,再分配给下一个进程使用时间片。这种算法可以保证所有进程都能有机会执行,避免了某一个进程一直霸占CPU的情况。
2. C语言设计一个对n个并发进程进行调度的程序:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESS_NUM 10
typedef enum {
READY, RUNNING, FINISHED
} ProcessState;
typedef struct {
int id;
int priority;
int chip;
int alltime;
int cputime;
ProcessState state;
struct PCB *next;
} PCB;
PCB *ready_queue = NULL;
PCB *running_process = NULL;
void add_process(int id, int priority, int chip, int alltime) {
PCB *p = (PCB *) malloc(sizeof(PCB));
p->id = id;
p->priority = priority;
p->chip = chip;
p->alltime = alltime;
p->cputime = 0;
p->state = READY;
p->next = NULL;
PCB *q = ready_queue;
if (q == NULL) {
ready_queue = p;
} else {
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void update_priority() {
PCB *p = ready_queue;
while (p != NULL) {
p->priority += 1;
p = p->next;
}
}
void schedule() {
if (running_process != NULL) {
running_process->chip -= 1;
running_process->cputime += 1;
running_process->alltime -= 1;
if (running_process->alltime == 0) {
running_process->state = FINISHED;
running_process = NULL;
} else if (running_process->chip == 0) {
running_process->state = READY;
running_process->chip = 2;
running_process->priority -= 1;
update_priority();
PCB *p = ready_queue;
ready_queue = ready_queue->next;
p->next = NULL;
PCB *q = ready_queue;
if (q == NULL) {
ready_queue = p;
} else {
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
running_process = NULL;
}
}
if (running_process == NULL && ready_queue != NULL) {
PCB *p = ready_queue;
PCB *q = NULL;
int max_priority = -1;
while (p != NULL) {
if (p->priority > max_priority) {
max_priority = p->priority;
q = p;
}
p = p->next;
}
if (q != NULL) {
ready_queue = q->next;
q->next = NULL;
q->state = RUNNING;
running_process = q;
}
}
}
void print_process_info() {
printf("Running Process: ");
if (running_process == NULL) {
printf("None\n");
} else {
printf("id=%d, priority=%d, chip=%d, alltime=%d, cputime=%d\n", running_process->id, running_process->priority, running_process->chip, running_process->alltime, running_process->cputime);
}
printf("Ready Queue: ");
PCB *p = ready_queue;
while (p != NULL) {
printf("id=%d, priority=%d, chip=%d, alltime=%d, cputime=%d ", p->id, p->priority, p->chip, p->alltime, p->cputime);
p = p->next;
}
printf("\n");
}
int main() {
int process_num;
printf("Please input the number of processes: ");
scanf("%d", &process_num);
for (int i = 0; i < process_num; i++) {
int id, priority, chip, alltime;
printf("Please input the process id, priority, chip and alltime: ");
scanf("%d %d %d %d", &id, &priority, &chip, &alltime);
add_process(id, priority, chip, alltime);
}
while (1) {
schedule();
print_process_info();
getchar();
}
return 0;
}
```
3. 2种不同的调度算法:
这里我们实现了动态优先级调度算法和时间片轮转调度算法,可以在代码中通过修改`schedule()`函数中的调度算法来选择不同的算法。
4. 显示或打印各种进程状态和参数变化情况:
在程序中,我们通过`print_process_info()`函数来打印各个进程的状态和参数信息。在每个时间片中,我们都会调用`print_process_info()`函数来显示最新的进程信息。