AlgoPlusAlgoPlus
Learn/Operating Systems
Lesson

CPU Scheduling

Only one process runs per core, so when several are ready a scheduling policy decides the order — trading average waiting time, fairness, and responsiveness.

9 min read Watch it move Build it

A CPU core runs one process at a time, so when several are ready, the scheduling policy decides who goes next. Each process needs a burst time of CPU work; the order they're run in changes how long each one waits. The policy is always a trade-off between speed, fairness, and responsiveness.

Three classic policies

  1. 1FCFS (First-Come, First-Served) — run processes in arrival order. Simple, but one long job makes everyone behind it wait — the convoy effect.
  2. 2SJF (Shortest Job First) — always run the ready process with the smallest burst. Provably the lowest average waiting time, but long jobs can be starved.
  3. 3Round-Robin — give each process a fixed quantum (time slice) in turn, then rotate. Preemptive, so the system stays responsive for everyone.

Worked example — waiting time

Three processes arrive together at time 0 with bursts P1 = 24, P2 = 3, P3 = 3. Waiting time is how long a process sits ready before it starts running.

Bursts: P1=24  P2=3  P3=3   (all arrive at t=0)

FCFS order P1, P2, P3:
  | P1 ........ | P2 | P3 |
  0           24   27   30
  waits: P1=0, P2=24, P3=27   -> average = 51/3 = 17

SJF order P2, P3, P1:
  | P2 | P3 | P1 ........ |
  0    3    6           30
  waits: P2=0, P3=3, P1=6     -> average = 9/3 = 3
Why SJF is optimal for average waiting
Running short jobs first means the long job's 24-unit delay is paid by only itself, not by the two short jobs waiting behind it. Front-loading the quick work drops the average wait from 17 down to 3 on the very same job mix.
Preemptive vs non-preemptive
FCFS and basic SJF are non-preemptive — a running job keeps the CPU until it finishes or blocks. Round-Robin is preemptive: a timer interrupt yanks the CPU back when the quantum expires. A tiny quantum gives great responsiveness but wastes time on context switches.
OperationTimeSpace
FCFS · convoy effect on long jobsO(n) by arrivalO(n)
SJF · min average wait; can starve long jobsO(n log n) by burstO(n)
Round-Robin · responsive; quantum trades latency vs overheadO(1) rotateO(n)
Check yourself
Bursts are P1=24, P2=3, P3=3, all arriving at time 0. What is the average waiting time under Shortest Job First?