FCFS scheduling

The processes run first come, first served: each one runs to the end of its burst, in the order they arrived. If the CPU is free before the next process arrives, it waits (idles) until it does.

Input

The first line has n (1 ≤ n ≤ 20). Each of the next n lines has a process's arrival time and burst time (0 ≤ arrival ≤ 100, 1 ≤ burst ≤ 50). The processes are listed in order of arrival; P1 is the first.

Output

For each process, in order: Pi CT TAT WT (completion, turnaround and waiting time). Then average waiting W and average turnaround T, each with 2 decimal places.

Examples

Example 1 · Input
4
0 5
1 3
2 8
3 2
Expected output
P1 5 5 0
P2 8 7 4
P3 16 14 6
P4 18 15 13
average waiting 5.75
average turnaround 10.25
Example 2 · Input
3
0 2
6 3
6 1
Expected output
P1 2 2 0
P2 9 3 0
P3 10 4 3
average waiting 1.00
average turnaround 3.00

Hints

Hint 1

Keep a clock. A process starts at the later of the clock and its arrival time.

Hint 2

CT is when it finishes; TAT = CT − AT; WT = TAT − BT.

Hint 3

Print the averages with %.2f after dividing as doubles.

Solution

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

int main(void) {
    int n, arrival[20], burst[20];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d %d", &arrival[i], &burst[i]);
    int time = 0, total_wait = 0, total_tat = 0;
    for (int i = 0; i < n; i++) {
        if (time < arrival[i]) time = arrival[i];
        time += burst[i];
        int tat = time - arrival[i];
        int wait = tat - burst[i];
        total_tat += tat;
        total_wait += wait;
        printf("P%d %d %d %d\n", i + 1, time, tat, wait);
    }
    printf("average waiting %.2f\n", (double)total_wait / n);
    printf("average turnaround %.2f\n", (double)total_tat / n);
    return 0;
}

Lesson: CPU scheduling: FCFS and SJF →