编写c程序模拟实现单处理机系统中的进程调度算法,实现对多个进程的调度模拟,要求采用常见的进程调度算法
时间: 2023-12-18 09:05:43 浏览: 238
下面是一个简单的C程序,模拟实现了三种常见的进程调度算法:先来先服务(FCFS)、最短作业优先(SJF)和时间片轮转(RR)。
```c
#include <stdio.h>
// 进程结构体
typedef struct {
int pid; // 进程ID
int arrival; // 到达时间
int burst; // 运行时间
int priority; // 优先级
} Process;
// 进程队列结构体
typedef struct {
Process *processes; // 进程数组
int size; // 进程数量
} ProcessQueue;
// 先来先服务调度算法
void fcfs(ProcessQueue *queue) {
int i, time = 0, waiting_time = 0, turnaround_time = 0;
printf("FCFS:\n");
for (i = 0; i < queue->size; i++) {
Process p = queue->processes[i];
// 计算等待时间和周转时间
waiting_time += time - p.arrival;
turnaround_time += time + p.burst - p.arrival;
// 更新时间
time += p.burst;
// 打印进程信息
printf("Process %d: Waiting Time=%d, Turnaround Time=%d\n", p.pid, time - p.arrival - p.burst, time - p.arrival);
}
// 打印平均等待时间和平均周转时间
printf("Average Waiting Time=%.2f, Average Turnaround Time=%.2f\n", (float)waiting_time / queue->size, (float)turnaround_time / queue->size);
}
// 最短作业优先调度算法
void sjf(ProcessQueue *queue) {
int i, j, time = 0, waiting_time = 0, turnaround_time = 0;
printf("SJF:\n");
for (i = 0; i < queue->size; i++) {
// 找到剩余时间最短的进程
int shortest = i;
for (j = i; j < queue->size; j++) {
if (queue->processes[j].burst < queue->processes[shortest].burst) {
shortest = j;
}
}
Process p = queue->processes[shortest];
// 计算等待时间和周转时间
waiting_time += time - p.arrival;
turnaround_time += time + p.burst - p.arrival;
// 更新时间
time += p.burst;
// 将已经运行的进程从队列中删除
queue->processes[shortest] = queue->processes[i];
queue->processes[i] = p;
// 打印进程信息
printf("Process %d: Waiting Time=%d, Turnaround Time=%d\n", p.pid, time - p.arrival - p.burst, time - p.arrival);
}
// 打印平均等待时间和平均周转时间
printf("Average Waiting Time=%.2f, Average Turnaround Time=%.2f\n", (float)waiting_time / queue->size, (float)turnaround_time / queue->size);
}
// 时间片轮转调度算法
void rr(ProcessQueue *queue, int quantum) {
int i, time = 0, waiting_time = 0, turnaround_time = 0;
ProcessQueue ready_queue = { .processes = NULL, .size = 0 };
printf("RR:\n");
// 将所有进程按照到达时间加入就绪队列
for (i = 0; i < queue->size; i++) {
Process p = queue->processes[i];
if (p.arrival <= time) {
ready_queue.processes = realloc(ready_queue.processes, (ready_queue.size + 1) * sizeof(Process));
ready_queue.processes[ready_queue.size++] = p;
}
}
// 不断轮转
while (ready_queue.size > 0) {
Process p = ready_queue.processes[0];
// 如果进程运行时间小于时间片,则直接运行完
if (p.burst <= quantum) {
// 计算等待时间和周转时间
waiting_time += time - p.arrival;
turnaround_time += time + p.burst - p.arrival;
// 更新时间
time += p.burst;
// 将已经运行的进程从就绪队列中删除
for (i = 1; i < ready_queue.size; i++) {
ready_queue.processes[i - 1] = ready_queue.processes[i];
}
ready_queue.size--;
// 打印进程信息
printf("Process %d: Waiting Time=%d, Turnaround Time=%d\n", p.pid, time - p.arrival - p.burst, time - p.arrival);
} else { // 如果进程运行时间大于时间片,则运行 quantum 时间
// 计算等待时间
waiting_time += time - p.arrival;
// 更新进程运行时间和就绪队列
p.burst -= quantum;
time += quantum;
for (i = 1; i < ready_queue.size; i++) {
ready_queue.processes[i - 1] = ready_queue.processes[i];
}
ready_queue.processes[ready_queue.size - 1] = p;
// 打印进程信息
printf("Process %d: Partially Completed at Time %d\n", p.pid, time);
}
// 将所有到达时间在当前时间之前,且还没有加入就绪队列的进程加入就绪队列
for (i = 0; i < queue->size; i++) {
Process p = queue->processes[i];
if (p.arrival > time) {
break;
}
for (j = 0; j < ready_queue.size; j++) {
if (ready_queue.processes[j].pid == p.pid) {
break;
}
}
if (j == ready_queue.size) {
ready_queue.processes = realloc(ready_queue.processes, (ready_queue.size + 1) * sizeof(Process));
ready_queue.processes[ready_queue.size++] = p;
}
}
}
// 打印平均等待时间和平均周转时间
printf("Average Waiting Time=%.2f, Average Turnaround Time=%.2f\n", (float)waiting_time / queue->size, (float)turnaround_time / queue->size);
}
int main() {
// 创建进程队列
ProcessQueue queue = { .processes = NULL, .size = 0 };
// 读入进程信息
int i, n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
queue.processes = malloc(n * sizeof(Process));
queue.size = n;
for (i = 0; i < n; i++) {
Process p;
printf("Enter the arrival time, burst time and priority of process %d: ", i + 1);
scanf("%d %d %d", &p.arrival, &p.burst, &p.priority);
p.pid = i + 1;
queue.processes[i] = p;
}
// 读入时间片
printf("Enter the time quantum for round-robin scheduling: ");
scanf("%d", &quantum);
// 按照到达时间排序
for (i = 0; i < n; i++) {
int j, min_index = i;
for (j = i + 1; j < n; j++) {
if (queue.processes[j].arrival < queue.processes[min_index].arrival) {
min_index = j;
}
}
Process temp = queue.processes[i];
queue.processes[i] = queue.processes[min_index];
queue.processes[min_index] = temp;
}
// 调用三种进程调度算法
fcfs(&queue);
sjf(&queue);
rr(&queue, quantum);
// 释放内存
free(queue.processes);
return 0;
}
```
该程序首先读入进程信息,包括进程的到达时间、运行时间和优先级。然后按照到达时间排序,并分别调用先来先服务、最短作业优先和时间片轮转三种进程调度算法。最后打印每个进程的等待时间和周转时间,以及平均等待时间和平均周转时间。注意,时间片轮转算法需要额外输入一个时间片,因为它是唯一一个需要时间片的算法。
该程序的输出格式如下:
```
Enter the number of processes: 3
Enter the arrival time, burst time and priority of process 1: 0 8 1
Enter the arrival time, burst time and priority of process 2: 1 4 2
Enter the arrival time, burst time and priority of process 3: 2 9 3
Enter the time quantum for round-robin scheduling: 3
FCFS:
Process 1: Waiting Time=0, Turnaround Time=8
Process 2: Waiting Time=7, Turnaround Time=11
Process 3: Waiting Time=11, Turnaround Time=20
Average Waiting Time=6.00, Average Turnaround Time=13.00
SJF:
Process 2: Waiting Time=0, Turnaround Time=4
Process 1: Waiting Time=4, Turnaround Time=12
Process 3: Waiting Time=12, Turnaround Time=21
Average Waiting Time=5.33, Average Turnaround Time=12.33
RR:
Process 1: Partially Completed at Time 3
Process 2: Waiting Time=2, Turnaround Time=6
Process 3: Partially Completed at Time 6
Process 1: Waiting Time=5, Turnaround Time=13
Process 3: Partially Completed at Time 9
Process 1: Waiting Time=8, Turnaround Time=16
Process 3: Waiting Time=10, Turnaround Time=18
Process 1: Waiting Time=10, Turnaround Time=18
Average Waiting Time=5.67, Average Turnaround Time=12.67
```
阅读全文