进程调度代码及注释
时间: 2023-12-14 16:36:28 浏览: 44
操作系统进程调度C++代码实现
以下是一个简单的进程调度代码及注释,使用了先来先服务(FCFS)调度算法:
```c
#include <stdio.h>
// 定义进程结构体
struct process {
int pid; // 进程ID
int burst_time; // 进程需要的CPU时间
int waiting_time; // 进程等待时间
int turnaround_time; // 进程周转时间
};
int main() {
int n; // 进程数
float avg_waiting_time = 0, avg_turnaround_time = 0; // 平均等待时间和平均周转时间
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process processes[n]; // 定义进程数组
// 输入每个进程的信息
for(int i = 0; i < n; i++) {
printf("Enter burst time for process %d: ", i+1);
scanf("%d", &processes[i].burst_time);
processes[i].pid = i+1;
}
// 计算每个进程的等待时间和周转时间
processes[0].waiting_time = 0;
processes[0].turnaround_time = processes[0].burst_time;
for(int i = 1; i < n; i++) {
processes[i].waiting_time = processes[i-1].waiting_time + processes[i-1].burst_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
}
// 计算平均等待时间和平均周转时间
for(int i = 0; i < n; i++) {
avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
// 输出结果
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
return 0;
}
```
注释见代码:
阅读全文