C语言实现操作系统的时间片轮转调度算法,并输出进程号、周转时间、带权周转时间、处理机空闲时间,平均周转时间和平均带权周转时间
时间: 2023-08-17 22:05:11 浏览: 122
code_detailk3t_操作系统进程调度和时间片轮转_源码
以下是一个采用时间片轮转调度算法的C语言源代码实现,同时输出进程号、周转时间、带权周转时间、处理机空闲时间,平均周转时间和平均带权周转时间:
```c
#include <stdio.h>
#define MAX_PROCESSES 10
typedef struct Process {
int id; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int completion_time; // 完成时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
float weighted_turnaround_time; // 带权周转时间
} Process;
int main() {
int n, i, j, quantum, time = 0, total_waiting_time = 0, total_turnaround_time = 0;
float total_weighted_turnaround_time = 0;
Process processes[MAX_PROCESSES], temp;
// 输入进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
// 输入每个进程的到达时间和执行时间
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for Process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].id = i + 1;
processes[i].remaining_time = processes[i].burst_time;
processes[i].completion_time = 0;
processes[i].waiting_time = 0;
processes[i].turnaround_time = 0;
processes[i].weighted_turnaround_time = 0;
}
// 输入时间片长度
printf("Enter the time quantum: ");
scanf("%d", &quantum);
// 轮转调度
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
done = 0;
if (processes[i].remaining_time > quantum) {
time += quantum;
processes[i].remaining_time -= quantum;
} else {
time += processes[i].remaining_time;
processes[i].waiting_time = time - processes[i].burst_time - processes[i].arrival_time;
processes[i].remaining_time = 0;
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].weighted_turnaround_time = (float)processes[i].turnaround_time / processes[i].burst_time;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
total_weighted_turnaround_time += processes[i].weighted_turnaround_time;
}
}
}
if (done == 1) {
break;
}
}
// 计算平均周转时间和平均带权周转时间
float avg_turnaround_time = (float)total_turnaround_time / n;
float avg_weighted_turnaround_time = (float)total_weighted_turnaround_time / n;
// 输出每个进程的信息
printf("\nProcess\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround Time\tWeighted Turnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].id, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time, processes[i].waiting_time,
processes[i].turnaround_time, processes[i].weighted_turnaround_time);
}
// 输出处理机空闲时间和平均周转时间和平均带权周转时间
printf("\nIdle Time: %d\n", time - processes[n - 1].completion_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
printf("Average Weighted Turnaround Time: %.2f\n", avg_weighted_turnaround_time);
return 0;
}
```
该算法先输入每个进程的到达时间和执行时间,并输入时间片长度。然后进行轮转调度,直到所有进程都完成执行。在每次轮转中,对于每个进程,如果剩余执行时间大于时间片,则减去时间片;否则,计算进程的完成时间、等待时间、周转时间和带权周转时间,并更新总等待时间、总周转时间和总带权周转时间。最后,输出每个进程的信息,以及处理机空闲时间、平均周转时间和平均带权周转时间。
阅读全文