Highest response ratio next (HRRN) is a non-preemptive scheduler that fixes the starvation of SJF. It picks the process with the largest response ratio, (waiting time + burst) / burst. A long job slowly rises in priority as it waits, so it cannot wait forever.
What to build
Run non-preemptive HRRN on a list of processes and print the order they run in and the average waiting time.
Requirements
- When the CPU is free at time
T, look at the processes that have arrived (arrival at mostT) and have not run. Their waiting time isTminus the arrival time. Pick the one with the largest ratio(wait + burst) / burst. - A tie goes to the earlier arrival, and then to the process listed first. The chosen process runs to the end of its burst. If no process has arrived, the CPU idles until the next arrival.
- Print
order:and the processes in the order they run (such asorder: P1 P3 P2), thenaverage waiting Wwith 2 decimal places. A process's waiting time is the time it starts minus its arrival time.
Input
The first line has n (1 ≤ n ≤ 20). Each of the next n lines has the arrival time and the burst time of P1, P2, ... (0 ≤ arrival ≤ 100, 1 ≤ burst ≤ 50), in any order of arrival.
Output
The order: line, then the average waiting line.
Constraints
Compare ratios with integer cross-multiplication, so that no rounding can change a tie.
Example
Input:
4
0 3
2 6
4 4
6 5
Output:
order: P1 P2 P3 P4
average waiting 3.25
Why it works
SJF always prefers the short job, so a long job can wait forever. In HRRN the numerator grows with waiting time, so the ratio of a waiting job keeps rising until it wins. The program is SJF with a different key, and the cross-multiplication keeps the comparison exact.
Examples
4 0 3 2 6 4 4 6 5
order: P1 P2 P3 P4 average waiting 3.25
3 0 10 1 1 2 1
order: P1 P2 P3 average waiting 6.00
Hints
Hint 1
Each time the CPU is free, compute for every arrived, unfinished process: wait = time - arrival, and ratio = (wait + burst) / burst.
Hint 2
Compare two ratios without division: (wait1 + burst1) * burst2 against (wait2 + burst2) * burst1.
Hint 3
A tie goes to the earlier arrival, then to the process listed first. If nobody has arrived, move the clock to the earliest arrival left.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int n, arrival[20], burst[20], done[20] = {0};
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d %d", &arrival[i], &burst[i]);
int time = 0, finished = 0, total_wait = 0;
printf("order:");
while (finished < n) {
int pick = -1;
for (int i = 0; i < n; i++) {
if (done[i] || arrival[i] > time) continue;
if (pick < 0) {
pick = i;
continue;
}
int left = (time - arrival[i] + burst[i]) * burst[pick];
int right = (time - arrival[pick] + burst[pick]) * burst[i];
if (left > right || (left == right && arrival[i] < arrival[pick])) pick = i;
}
if (pick < 0) {
int soonest = -1;
for (int i = 0; i < n; i++)
if (!done[i] && (soonest < 0 || arrival[i] < arrival[soonest])) soonest = i;
time = arrival[soonest];
continue;
}
total_wait += time - arrival[pick];
time += burst[pick];
done[pick] = 1;
finished++;
printf(" P%d", pick + 1);
}
printf("\naverage waiting %.2f\n", (double)total_wait / n);
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step