帮我写一个带中文注释的C语言程序:模拟实现操作系统进程调度的四种算法
时间: 2024-03-08 16:46:44 浏览: 97
操作系统中关于进程调度的模拟算法
好的,以下是一个简单的带中文注释的C语言程序,模拟实现操作系统进程调度的四种算法(FCFS、SJF、优先级调度和时间片轮转):
```c
#include <stdio.h>
#include <stdlib.h>
// 进程结构体
typedef struct process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
int completion_time; // 完成时间
} Process;
// 比较函数,用于排序
int compare(const void *a, const void *b) {
return (*(Process *)a).arrival_time - (*(Process *)b).arrival_time;
}
// 先来先服务算法
void fcfs(Process processes[], int n) {
int total_waiting_time = 0, total_turnaround_time = 0;
int current_time = 0;
for (int i = 0; i < n; i++) {
// 计算等待时间和周转时间
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
// 计算完成时间和当前时间
processes[i].completion_time = current_time + processes[i].burst_time;
current_time = processes[i].completion_time;
// 更新总等待时间和总周转时间
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
// 计算平均等待时间和平均周转时间
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
// 输出结果
printf("FCFS:\n");
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
}
// 最短作业优先算法
void sjf(Process processes[], int n) {
int total_waiting_time = 0, total_turnaround_time = 0;
int current_time = 0;
// 按照执行时间排序
qsort(processes, n, sizeof(Process), compare);
for (int i = 0; i < n; i++) {
// 计算等待时间和周转时间
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
// 计算完成时间和当前时间
processes[i].completion_time = current_time + processes[i].burst_time;
current_time = processes[i].completion_time;
// 更新总等待时间和总周转时间
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
// 计算平均等待时间和平均周转时间
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
// 输出结果
printf("SJF:\n");
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
}
// 优先级调度算法
void priority_scheduling(Process processes[], int n) {
int total_waiting_time = 0, total_turnaround_time = 0;
int current_time = 0;
// 按照优先级排序
qsort(processes, n, sizeof(Process), compare);
for (int i = 0; i < n; i++) {
// 计算等待时间和周转时间
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
// 计算完成时间和当前时间
processes[i].completion_time = current_time + processes[i].burst_time;
current_time = processes[i].completion_time;
// 更新总等待时间和总周转时间
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
// 计算平均等待时间和平均周转时间
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
// 输出结果
printf("优先级调度:\n");
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
}
// 时间片轮转算法
void round_robin(Process processes[], int n, int quantum) {
int total_waiting_time = 0, total_turnaround_time = 0;
int current_time = 0;
// 记录剩余执行时间
int *remaining_time = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
remaining_time[i] = processes[i].burst_time;
}
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0) {
done = 0;
// 执行一段时间片
if (remaining_time[i] > quantum) {
current_time += quantum;
remaining_time[i] -= quantum;
} else {
current_time += remaining_time[i];
processes[i].completion_time = current_time;
remaining_time[i] = 0;
}
}
}
// 如果所有进程都执行完毕,则退出循环
if (done == 1) {
break;
}
}
for (int i = 0; i < n; i++) {
// 计算等待时间和周转时间
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
// 更新总等待时间和总周转时间
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
// 计算平均等待时间和平均周转时间
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
// 输出结果
printf("时间片轮转:\n");
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
}
int main() {
int n, quantum;
// 读入进程数和时间片长度
printf("请输入进程数: ");
scanf("%d", &n);
printf("请输入时间片长度: ");
scanf("%d", &quantum);
// 创建进程数组
Process *processes = (Process *)malloc(sizeof(Process) * n);
// 读入进程信息
for (int i = 0; i < n; i++) {
printf("请输入进程%d的信息(到达时间 执行时间 优先级): ", i + 1);
scanf("%d %d %d", &processes[i].arrival_time, &processes[i].burst_time, &processes[i].priority);
processes[i].pid = i + 1;
}
// 调用四种算法
fcfs(processes, n);
sjf(processes, n);
priority_scheduling(processes, n);
round_robin(processes, n, quantum);
return 0;
}
```
希望能够帮到你!
阅读全文