c语言编写并调试一个模拟的进程调度程序,采用 “短进程优先”调度算法对多个进程进行调度。计算平均周转时间和平均带权周转时间。
时间: 2024-01-22 20:17:50 浏览: 59
采用短进程优先算法模拟实现进程调度.pdf
好的,我会给你提供一个简单的短进程优先调度算法的C语言代码。
```c
#include<stdio.h>
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int completion_time; // 完成时间
int turn_around_time; // 周转时间
float waiting_time; // 等待时间
float priority; // 优先级
};
int main() {
int n, i, j;
float avg_waiting_time = 0, avg_turn_around_time = 0;
struct Process p[10], temp;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the arrival time, burst time and priority of each process:\n");
for (i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("P[%d]: ", p[i].pid);
scanf("%d %d %f", &p[i].arrival_time, &p[i].burst_time, &p[i].priority);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].arrival_time > p[j].arrival_time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].priority < p[j].priority && p[i].arrival_time <= p[j].arrival_time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
p[0].completion_time = p[0].arrival_time + p[0].burst_time;
p[0].turn_around_time = p[0].completion_time - p[0].arrival_time;
p[0].waiting_time = p[0].turn_around_time - p[0].burst_time;
avg_waiting_time = p[0].waiting_time;
avg_turn_around_time = p[0].turn_around_time;
for (i = 1; i < n; i++) {
p[i].completion_time = p[i - 1].completion_time + p[i].burst_time;
p[i].turn_around_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turn_around_time - p[i].burst_time;
avg_waiting_time += p[i].waiting_time;
avg_turn_around_time += p[i].turn_around_time;
}
printf("\nPId\tAT\tBT\tPT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P[%d]\t%d\t%d\t%.2f\t%d\t%d\t%.2f\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].priority, p[i].completion_time, p[i].turn_around_time, p[i].waiting_time);
}
printf("Average waiting time: %.2f\n", avg_waiting_time / n);
printf("Average turn around time: %.2f\n", avg_turn_around_time / n);
return 0;
}
```
这个程序会输入进程的到达时间、执行时间和优先级,然后以短进程优先的方式进行调度,并计算出平均周转时间和平均带权周转时间。
注意,此程序仅供参考,实际运行中可能需要根据具体情况进行调整。
阅读全文