用c++写时间片轮转调度算法
时间: 2023-06-12 12:07:15 浏览: 55
下面是一个简单的时间片轮转调度算法的 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义进程结构体
typedef struct process {
int pid; // 进程 ID
int burst_time; // 运行时间
int remaining_time; // 剩余时间
int arrival_time; // 到达时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
} process;
// 全局变量
int n; // 进程数
int quantum; // 时间片大小
process *processes; // 进程数组
// 计算等待时间、周转时间和平均等待时间、平均周转时间
void calculate_time() {
int total_waiting_time = 0;
int total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
processes[i].turnaround_time = processes[i].burst_time + processes[i].waiting_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("Average Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}
// 时间片轮转调度算法
void round_robin_scheduling() {
int current_time = 0;
int completed = 0;
while (completed != n) {
int flag = 0;
for (int i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
flag = 1;
if (processes[i].remaining_time > quantum) {
current_time += quantum;
processes[i].remaining_time -= quantum;
} else {
current_time += processes[i].remaining_time;
processes[i].waiting_time = current_time - processes[i].burst_time - processes[i].arrival_time;
processes[i].remaining_time = 0;
completed++;
}
}
}
if (flag == 0) {
break;
}
}
calculate_time();
}
int main() {
// 输入进程数和时间片大小
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the quantum: ");
scanf("%d", &quantum);
// 动态分配进程数组内存
processes = (process *)malloc(n * sizeof(process));
// 输入每个进程的信息
for (int i = 0; i < n; i++) {
printf("Enter Process %d Information:\n", i + 1);
printf("Burst Time: ");
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
printf("Arrival Time: ");
scanf("%d", &processes[i].arrival_time);
processes[i].pid = i + 1;
}
// 执行时间片轮转调度算法
round_robin_scheduling();
// 释放进程数组内存
free(processes);
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)