C++实现最短剩余时间优先SRT调度算法
时间: 2023-10-20 16:09:35 浏览: 52
以下是使用C语言实现最短剩余时间优先(SRT)调度算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 进程控制块
typedef struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int completion_time; // 完成时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
} Process;
// 初始化进程控制块
void init_process(Process *p, int pid, int arrival_time, int burst_time) {
p->pid = pid;
p->arrival_time = arrival_time;
p->burst_time = burst_time;
p->remaining_time = burst_time;
p->completion_time = 0;
p->waiting_time = 0;
p->turnaround_time = 0;
}
// 计算平均等待时间和平均周转时间
void calc_average_time(Process *p, int n, float *avg_waiting_time, float *avg_turnaround_time) {
int total_waiting_time = 0;
int total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
}
*avg_waiting_time = (float)total_waiting_time / n;
*avg_turnaround_time = (float)total_turnaround_time / n;
}
// 最短剩余时间优先调度算法
void srt_schedule(Process *p, int n) {
int current_time = 0;
int completed = 0;
int prev_pid = -1;
while (completed != n) {
int shortest_index = -1;
int shortest_time = 999999;
// 找到剩余时间最短的进程
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time < shortest_time && p[i].remaining_time > 0) {
shortest_index = i;
shortest_time = p[i].remaining_time;
}
}
if (shortest_index == -1) {
// 如果当前没有进程可执行,增加时间片
current_time++;
} else {
// 执行剩余时间最短的进程
if (prev_pid != p[shortest_index].pid) {
p[shortest_index].waiting_time += current_time - p[shortest_index].completion_time;
}
p[shortest_index].remaining_time--;
current_time++;
prev_pid = p[shortest_index].pid;
if (p[shortest_index].remaining_time == 0) {
// 进程执行完毕
p[shortest_index].completion_time = current_time;
p[shortest_index].turnaround_time = p[shortest_index].completion_time - p[shortest_index].arrival_time;
completed++;
}
}
}
}
int main() {
int n;
printf("请输入进程数:");
scanf("%d", &n);
Process *p = (Process*)malloc(n * sizeof(Process));
for (int i = 0; i < n; i++) {
int pid, arrival_time, burst_time;
printf("请输入第%d个进程的到达时间和执行时间:", i + 1);
scanf("%d%d", &arrival_time, &burst_time);
init_process(&p[i], i + 1, arrival_time, burst_time);
}
srt_schedule(p, n);
float avg_waiting_time, avg_turnaround_time;
calc_average_time(p, n, &avg_waiting_time, &avg_turnaround_time);
printf("进程ID\t到达时间\t执行时间\t完成时间\t等待时间\t周转时间\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].completion_time, p[i].waiting_time, p[i].turnaround_time);
}
printf("平均等待时间:%f\n", avg_waiting_time);
printf("平均周转时间:%f\n", avg_turnaround_time);
free(p);
return 0;
}
```
示例输出:
```
请输入进程数:5
请输入第1个进程的到达时间和执行时间:0 8
请输入第2个进程的到达时间和执行时间:1 4
请输入第3个进程的到达时间和执行时间:2 9
请输入第4个进程的到达时间和执行时间:3 5
请输入第5个进程的到达时间和执行时间:4 2
进程ID 到达时间 执行时间 完成时间 等待时间 周转时间
1 0 8 8 0 8
2 1 4 5 3 4
3 2 9 17 6 15
4 3 5 12 4 9
5 4 2 6 0 2
平均等待时间:2.600000
平均周转时间:7.600000
```