cpu scheduler · FCFS / SJF / SRTF / RR
The same workload, four scheduling policies, very different averages. Drag arrivals and bursts, watch the Gantt chart redraw, and try to find the policy that wins for your workload.
avg wait below 4. Then make it worse by re-shaping the workload. Convoy effect is real.| id | arrival | burst | wait | turnaround | response | |
|---|---|---|---|---|---|---|
| 0 | 8 | 0 | ||||
| 7 | 11 | 7 | ||||
| 10 | 19 | 10 | ||||
| 18 | 23 | 18 |
Source
// scheduler · CSV-driven simulator (Project 2 in the OS Stack repo)
// Each scheduler picks the next process at every dispatch decision.
//
// FCFS -> first-come-first-served: finish current before next
// SJF -> shortest-job-first: non-preemptive — fairness can suffer
// SRTF -> shortest-remaining-time-first: preemptive variant of SJF
// RR -> round-robin: time-slice of Q ticks, then rotate
typedef struct proc { int pid; int arrival; int burst; int priority; } proc;
proc* schedule_fcfs(proc *ready, int n);
proc* schedule_sjf (proc *ready, int n);
proc* schedule_srtf(proc *ready, int n, int now);
proc* schedule_rr (proc *ready, int n, int *active_q);What's really happening
Every scheduler picks the next process at every dispatch decision — a context switch trigger. FCFS only re-dispatches when the current process finishes. SJF and SRTF look at remaining work. RR adds a clock-driven preemption every Q ticks. The differences in avg wait trace directly to how aggressively the policy interrupts long jobs.
Convoy effect: when a long job arrives first, FCFS makes every later short job wait for it. SJF/SRTF fix this by reordering. Try the convoy preset on FCFS vs SRTF and watch the average wait fall by 4–6×.
RR's knob: small Q means more context switches (overhead this simulator ignores) but better response time. Large Q approaches FCFS behavior. Real schedulers like CFS use a virtual-runtime heuristic to dodge the convoy without paying the SJF oracle cost of knowing each job's burst length up front.