Appearance
24. [NEW] Worked Example: FCFS / SJF / SRTF / Round Robin
This is the single most common practical follow-up after "explain the scheduling algorithms" — a numeric Gantt-chart problem. Practice this pattern.
Given processes (arrival time, burst time):
| Process Arrival Burst | ||
|---|---|---|
| P1 | 0 | 5 |
| P2 | 1 | 3 |
| P3 | 2 | 8 |
| P4 | 3 | 6 |
FCFS (run in arrival order):
Gantt: | P1(0-5) | P2(5-8) | P3(8-16) | P4(16-22) |Waiting time = start time − arrival time. Turnaround = completion − arrival.
| Process Completion Turnaround Waiting | |||
|---|---|---|---|
| P1 | 5 | 5 | 0 |
| P2 | 8 | 7 | 4 |
| P3 | 16 | 14 | 6 |
| P4 | 22 | 19 | 13 |
Average waiting = (0+4+6+13)/4 = 5.75. Average turnaround = (5+7+14+19)/4 = 11.25.
SJF, non-preemptive (pick shortest available burst each time the CPU is free):
Gantt: | P1(0-5) | P2(5-8) | P4(8-14) | P3(14-22) |(At t=5, ready set is {P2(3), P3(8), P4 not yet arrived at t=3? P4 arrives at 3, so ready = {P2(3), P3(8), P4(6)} → pick P2.) At t=8, ready = {P3(8), P4(6)} → pick P4. Then P3 last.
Average waiting = ((0)+(5-1)+(14-3)+(8-2))/4 = (0+4+11+6)/4 = 5.25. Lower than FCFS — shorter jobs finishing earlier drags the average down, illustrating why SJF minimizes average waiting time.
SRTF (preemptive SJF): at every new arrival, compare remaining burst of the running process against the new arrival's burst, and switch to whichever is shorter. This usually beats non-preemptive SJF on average waiting time but adds context-switch overhead and can still starve long jobs.
Round Robin (quantum = 4):
Gantt: | P1(0-4) | P2(4-7) | P3(7-11) | P4(11-15) | P1(15-16) | P3(16-20) | P4(20-22) |RR gives every process the CPU roughly on a rotating basis, so response time is far better than FCFS/SJF even though total completion may not be minimal.
Takeaway for interviews: be ready to (1) build the Gantt chart, (2) compute waiting/turnaround per process, (3) average them, and (4) explain the trade-off in one line — e.g., "SJF minimizes average waiting time but can starve long jobs; Round Robin trades throughput for fairness and responsiveness."