高响应比优先调度算法c语言
时间: 2023-08-13 14:41:34 浏览: 81
以下是一种基于高响应比优先调度算法的 C 语言实现:
```c
#include <stdio.h>
#define MAX_PROCESS 10
struct Process {
int pid; // 进程 ID
int arrival; // 到达时间
int burst; // 运行时间
int priority; // 优先级
int response; // 响应时间
int waiting; // 等待时间
};
int main() {
struct Process processes[MAX_PROCESS];
int n, i, j;
float avg_response = 0, avg_waiting = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter arrival time, burst time and priority of each process:\n");
for (i = 0; i < n; i++) {
processes[i].pid = i + 1;
scanf("%d%d%d", &processes[i].arrival, &processes[i].burst, &processes[i].priority);
processes[i].response = processes[i].waiting = 0;
}
int time = 0, completed = 0;
while (completed < n) {
int min_priority = 9999, selected = -1;
for (i = 0; i < n; i++) {
if (processes[i].arrival <= time && processes[i].burst > 0) {
float ratio = (float)(time - processes[i].arrival + processes[i].burst) / processes[i].burst;
if (processes[i].priority / ratio < min_priority) {
min_priority = processes[i].priority / ratio;
selected = i;
}
}
}
if (selected == -1) {
time++;
continue;
}
processes[selected].response = time - processes[selected].arrival;
processes[selected].burst--;
time++;
if (processes[selected].burst == 0) {
completed++;
processes[selected].waiting = processes[selected].response - processes[selected].arrival;
avg_response += processes[selected].response;
avg_waiting += processes[selected].waiting;
}
}
avg_response /= n;
avg_waiting /= n;
printf("\nPID\tAT\tBT\tPRI\tRT\tWT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].arrival, processes[i].burst + processes[i].waiting, processes[i].priority, processes[i].response, processes[i].waiting);
}
printf("\nAverage response time = %.2f\n", avg_response);
printf("Average waiting time = %.2f\n", avg_waiting);
return 0;
}
```
该程序首先读入进程的到达时间、运行时间和优先级,然后使用高响应比优先调度算法对进程进行调度,最后输出每个进程的响应时间和等待时间以及平均响应时间和平均等待时间。
阅读全文