Systems · 15 min

CPU scheduling: who runs next?

One CPU, many processes: first come first served, shortest job first, round robin and priorities, and the waiting each one causes.

01Why it matters

Hundreds of programs, a handful of cores

Your laptop is running hundreds of threads right now: the browser, a music player, background updates, this page. It has perhaps eight cores. Each core can run only one thread at a time, so something has to decide, many times a second, who gets it next. That something is the scheduler.

The choice matters. Make a short job wait behind a long one and it feels sluggish. Hand out turns too eagerly and the core spends its time switching instead of working. Every policy here is a different answer to one question: who should run now?

02Key parts

The pieces you will see

process and burst
A job that needs the CPU. Its burst is how many units of CPU time it needs before it is done (or stops to wait for something else).
ready queue
The processes that have arrived and are waiting for the CPU. The scheduler picks from here.
preemption
Taking the CPU away from a running process before it finishes, because the rule says someone else should run.
quantum
Round robin’s turn length: a process runs for at most this long, then goes to the back of the queue.
waiting, turnaround, response
Turnaround is finish − arrival. Waiting is the part of that spent in the queue: turnaround − burst. Response is how long until it first runs.

03See it work

Watch a convoy form

First come, first served is the obvious rule: run jobs in the order they arrive, each to the end. Watch what happens when the first job is a long one. On the Gantt chart, each row is a process: its pale bars are time spent waiting, its solid bars are time on the CPU.

P1 needs 8 units and arrives first. P2, P3 and P4 need 6 units between them, yet each waits about 8. That queue behind one slow job is the convoy effect.

Now the same four jobs under shortest remaining time first (SRTF): whenever a job arrives with less work left than the running one, it takes over the CPU.

P2 arrives at t = 1 needing 2, while P1 still needs 7: P1 is preempted. The average wait drops from 5.75 under FCFS to 2.

04Predict

Your turn: be the scheduler

The player stops at every decision and asks which process gets the CPU. Use the rule, SRTF: the least work left wins, and a tie never takes the CPU away from the process already running.

Make 3 predictions: step forward and answer each question before the next step plays.

0 of 3 made

05Try it

Take turns: round robin

Shortest-first is unfair to long jobs, and it needs to know the future. Round robin needs neither: processes take turns of at most one quantum, and a process whose turn is up goes to the back of the queue. Try the same three jobs with a quantum of 1 and of 4, and compare the context switches and when each job first runs.

Quantum 1: every job starts within a tick or two, but the CPU switches 11 times.
Quantum 4: far fewer switches, but P3 waits longer for its first turn. Type your own jobs, e.g. rr 2: 0-5, 0-3, 1-4, or try fcfs, sjf, srtf or priority.

A quantum longer than every burst means no job is ever cut short: round robin becomes FCFS. The art is a quantum long enough that switching is a small cost, short enough that nobody waits long for a turn.

06Check yourself

Check yourself

  1. Question 1 of 6

    Three jobs arrive at time 0 needing 8, 2 and 1 units, in that order. Under FCFS, what is the average waiting time?

    Your answer comes back in Review in a few days, so it sticks.

  2. Question 2 of 6

    The same jobs (8, 2 and 1 units, all at time 0) under SJF: what is the average waiting time?

  3. Question 3 of 6

    Under SRTF, P1 (needs 8) starts at 0. At t = 1, P2 arrives needing 2. What happens?

  4. Question 4 of 6

    Four jobs of 3 units each arrive together. Which policy gives the lowest average turnaround?

  5. Question 5 of 6

    An operating system wants SJF’s short waiting times. What does it actually do?

  6. Question 6 of 6

    Which policy can leave a process waiting for ever?

07Explain it back

Say it in your own words

How does a CPU scheduler decide who runs next, and why does no single policy win at everything?

08Recap

What to remember

  • A scheduler picks which ready process runs, whenever the CPU frees up or (if it preempts) a better candidate arrives.
  • Turnaround = finish − arrival, waiting = turnaround − burst, response = first run − arrival.
  • FCFS is simple but forms convoys behind long jobs. SJF/SRTF minimise average waiting but need predicted burst lengths. Round robin keeps response times short at the cost of context switches; with a huge quantum it becomes FCFS. Priority can starve low priorities unless waiting jobs age upwards.
  • Heaps meet schedulers here: SRTF and priority scheduling keep the ready queue in a priority queue keyed by remaining time or priority, so picking the next process costs O(log⁡n)O(\log n) instead of a scan.
Back to path