用一个C语言模拟代码实现PSA(优先级)和RR(时间片轮转)操作系统
时间: 2023-10-29 17:04:20 浏览: 122
编写程序实现基于优先级的时间片轮转调度算法
4星 · 用户满意度95%
PSA优先级调度算法的C语言实现:
```c
#include <stdio.h>
#define MAX_PROCESS 10
struct Process {
int id;
int priority;
int burst_time;
int arrival_time;
int waiting_time;
int turnaround_time;
};
int main() {
int n, i, j, total_waiting_time = 0, total_turnaround_time = 0;
float avg_waiting_time, avg_turnaround_time;
struct Process p[MAX_PROCESS], temp;
// 输入进程信息
printf("请输入进程数量(最多%d个):", MAX_PROCESS);
scanf("%d", &n);
printf("请依次输入每个进程的优先级、CPU执行时间和到达时间:\n");
for (i = 0; i < n; i++) {
p[i].id = i + 1;
scanf("%d %d %d", &p[i].priority, &p[i].burst_time, &p[i].arrival_time);
}
// 按照优先级对进程进行排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (p[j].priority < p[j + 1].priority) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
// 计算等待时间和周转时间
for (i = 0; i < n; i++) {
p[i].waiting_time = i == 0 ? 0 : p[i - 1].waiting_time + p[i - 1].burst_time;
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
}
// 输出结果
printf("进程ID\t优先级\tCPU执行时间\t到达时间\t等待时间\t周转时间\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].id, p[i].priority, p[i].burst_time, p[i].arrival_time, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time = (float) total_waiting_time / n;
avg_turnaround_time = (float) total_turnaround_time / n;
printf("平均等待时间:%.2f\n平均周转时间:%.2f\n", avg_waiting_time, avg_turnaround_time);
return 0;
}
```
RR时间片轮转调度算法的C语言实现:
```c
#include <stdio.h>
#define MAX_PROCESS 10
struct Process {
int id;
int burst_time;
int arrival_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};
int main() {
int n, i, j, time_quantum, total_waiting_time = 0, total_turnaround_time = 0;
float avg_waiting_time, avg_turnaround_time;
struct Process p[MAX_PROCESS], temp;
// 输入进程信息
printf("请输入进程数量(最多%d个):", MAX_PROCESS);
scanf("%d", &n);
printf("请依次输入每个进程的CPU执行时间和到达时间:\n");
for (i = 0; i < n; i++) {
p[i].id = i + 1;
scanf("%d %d", &p[i].burst_time, &p[i].arrival_time);
p[i].remaining_time = p[i].burst_time;
}
printf("请输入时间片大小:");
scanf("%d", &time_quantum);
// 时间片轮转调度
for (i = 0; ; i++) {
int all_finished = 1; // 所有进程是否都已经完成
for (j = 0; j < n; j++) {
if (p[j].remaining_time > 0) {
all_finished = 0;
if (p[j].remaining_time > time_quantum) {
// 执行时间超过时间片大小
p[j].remaining_time -= time_quantum;
i += time_quantum - 1; // 时间片耗尽,更新时间
} else {
// 执行时间不足时间片大小,执行完毕
p[j].waiting_time = i - p[j].arrival_time - p[j].burst_time + p[j].remaining_time;
p[j].remaining_time = 0;
}
}
}
if (all_finished) {
break;
}
}
// 计算周转时间
for (i = 0; i < n; i++) {
p[i].turnaround_time = p[i].burst_time + p[i].waiting_time;
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
}
// 输出结果
printf("进程ID\tCPU执行时间\t到达时间\t等待时间\t周转时间\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].id, p[i].burst_time, p[i].arrival_time, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time = (float) total_waiting_time / n;
avg_turnaround_time = (float) total_turnaround_time / n;
printf("平均等待时间:%.2f\n平均周转时间:%.2f\n", avg_waiting_time, avg_turnaround_time);
return 0;
}
```
阅读全文