Shortest remaining time first

Shortest remaining time first (SRTF) is the preemptive version of SJF. Whenever a process arrives, the scheduler compares its burst with the time the running process still needs, and the shorter one gets the CPU.

What to build

Simulate SRTF one time unit at a time, and print each process's completion and waiting time.

Requirements

  • At every time unit, among the processes that have arrived and are not finished, run the one with the smallest remaining time for that unit. Equal remaining times: the earlier arrival, then the process listed first.
  • If no process is ready, the CPU idles for that unit.
  • Print Pi CT WT for each process in order, then average waiting W with 2 decimal places. WT is CT minus arrival minus burst.

Input

The first line has n (1 ≤ n ≤ 20). Each of the next n lines has the arrival time and burst time of P1, P2, ... (0 ≤ arrival ≤ 100, 1 ≤ burst ≤ 50), in any order of arrival.

Output

n lines Pi CT WT, then the average waiting line.

Constraints

The whole simulation takes at most a few thousand time units.

Example

Input:

3
0 24
1 3
2 3

Output:

P1 30 6
P2 4 0
P3 7 2
average waiting 2.67

Why it works

Choosing again at every time unit is what makes SRTF preemptive: a newcomer with a smaller remaining time wins at the very next unit and the old process simply waits with its remaining time unchanged. This gives the smallest average waiting time of any scheduler, at the price of more context switches.

Examples

Example 1 · Input
3
0 24
1 3
2 3
Expected output
P1 30 6
P2 4 0
P3 7 2
average waiting 2.67
Example 2 · Input
4
0 8
1 4
2 9
3 5
Expected output
P1 17 9
P2 5 0
P3 26 15
P4 10 2
average waiting 6.50

Hints

Hint 1

Simulate one time unit at a time. At each unit, pick among the arrived, unfinished processes the one with the smallest remaining time.

Hint 2

Break ties by earlier arrival, then by the process listed first. Run the chosen process for one unit and subtract 1 from its remaining time.

Hint 3

When a process reaches zero, record the clock as its completion time. If nothing is ready, just add 1 to the clock. Waiting time = completion - arrival - burst.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int n, arrival[20], burst[20], left[20], finish[20];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &arrival[i], &burst[i]);
        left[i] = burst[i];
        finish[i] = 0;
    }
    int time = 0, done = 0;
    while (done < n) {
        int pick = -1;
        for (int i = 0; i < n; i++) {
            if (left[i] == 0 || arrival[i] > time) continue;
            if (pick < 0 || left[i] < left[pick] || (left[i] == left[pick] && arrival[i] < arrival[pick])) pick = i;
        }
        if (pick >= 0) {
            left[pick]--;
            if (left[pick] == 0) {
                finish[pick] = time + 1;
                done++;
            }
        }
        time++;
    }
    int total_wait = 0;
    for (int i = 0; i < n; i++) {
        int wait = finish[i] - arrival[i] - burst[i];
        total_wait += wait;
        printf("P%d %d %d\n", i + 1, finish[i], wait);
    }
    printf("average 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

Lesson: Round robin, SRTF and priority →