使用简单轮转法和优先数法写一个进程调度程序
时间: 2024-04-29 13:23:16 浏览: 17
以下是一个简单的进程调度程序,使用简单轮转法和优先数法:
```
#include <stdio.h>
#define MAX_PROCESSES 10 // 最大进程数
#define QUANTUM 2 // 时间片长度
int current_time = 0; // 当前时间
int num_processes = 0; // 进程数
int queue[MAX_PROCESSES]; // 进程队列
int front = 0, rear = -1; // 队列指针
// 进程结构体
struct process {
int pid; // 进程ID
int arrival; // 到达时间
int burst; // 运行时间
int priority; // 优先级
int remaining; // 剩余时间
};
struct process processes[MAX_PROCESSES]; // 进程数组
// 添加进程到队列
void enqueue(int pid) {
rear++;
queue[rear] = pid;
}
// 从队列中取出一个进程
int dequeue() {
int pid = queue[front];
front++;
return pid;
}
// 检查队列是否为空
int is_empty() {
return rear < front;
}
// 初始化进程数组
void init_processes() {
processes[0] = (struct process) {1, 0, 8, 3, 8};
processes[1] = (struct process) {2, 1, 4, 1, 4};
processes[2] = (struct process) {3, 2, 9, 2, 9};
num_processes = 3;
}
// 打印进程信息
void print_processes() {
printf("进程ID\t到达时间\t运行时间\t优先级\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival, processes[i].burst, processes[i].priority);
}
}
// 简单轮转法
void round_robin() {
int time_quantum = QUANTUM;
int running_process = -1;
while (!is_empty() || running_process != -1) {
// 检查是否有进程到达
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival == current_time) {
enqueue(processes[i].pid);
}
}
// 取出一个进程运行
if (running_process == -1) {
if (!is_empty()) {
running_process = dequeue();
time_quantum = QUANTUM;
}
} else {
time_quantum--;
processes[running_process].remaining--;
// 进程运行完毕
if (processes[running_process].remaining == 0) {
printf("进程 %d 在时间 %d 运行完毕\n", processes[running_process].pid, current_time);
running_process = -1;
}
// 时间片用完,将进程加入队尾
else if (time_quantum == 0) {
enqueue(running_process);
running_process = -1;
}
}
current_time++;
}
}
// 优先数法
void priority_scheduling() {
int running_process = -1;
while (!is_empty() || running_process != -1) {
// 检查是否有进程到达
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival == current_time) {
enqueue(processes[i].pid);
}
}
// 找出优先级最高的进程
if (running_process == -1) {
if (!is_empty()) {
int highest_priority = -1;
for (int i = front; i <= rear; i++) {
int pid = queue[i];
if (processes[pid - 1].priority > highest_priority) {
highest_priority = processes[pid - 1].priority;
running_process = pid - 1;
}
}
}
} else {
processes[running_process].remaining--;
// 进程运行完毕
if (processes[running_process].remaining == 0) {
printf("进程 %d 在时间 %d 运行完毕\n", processes[running_process].pid, current_time);
running_process = -1;
}
}
current_time++;
}
}
int main() {
init_processes();
print_processes();
printf("简单轮转法:\n");
round_robin();
printf("优先数法:\n");
priority_scheduling();
return 0;
}
```
该程序首先定义了一个进程结构体,包含进程ID、到达时间、运行时间、优先级和剩余时间等信息。然后定义了一个进程数组,存储所有的进程。
程序中实现了两种进程调度算法:简单轮转法和优先数法。简单轮转法将所有进程加入队列,每次取出队首的进程运行,如果时间片用完了,则将进程加入队尾;如果进程运行完毕,则将其从队列中移除。优先数法每次从队列中选择优先级最高的进程运行,如果进程运行完毕,则将其从队列中移除。
在主函数中,先初始化了进程数组并打印了所有进程的信息。然后分别使用简单轮转法和优先数法进行进程调度,并打印出每个进程的运行时间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)