All lessons · Guided routes

Simulator · Operating systems

CPU scheduler

Four programs share one CPU core on a Quillfern depot server: a long invoice batch, label printing, scan ingestion, and an engineer's ssh session. Pick how the kernel schedules them and see who waits.

At Quillfern Parcels. This is the system from chapter 1 of the Quillfern story, "One server under the desk": The booking site and its database run on one Linux server in the office. A month-end invoice job makes the whole server freeze, and every reboot for updates stops bookings.

What the model simplifies

One core, no context-switch cost, no input or output: each program only needs CPU time, counted in ticks of 10 ms. First come, first served and shortest job first let a program run to the end; round robin gives each a time slice in turn. Real kernels (Linux CFS and EEVDF) behave more like a weighted round robin with priorities. Every number on this page comes from one shared set of calculations, which the simulation questions use too, so a question can never disagree with what the simulator shows.

The drills, written out

A long batch job freezes the server (the convoy effect)
  1. Healthy state. With round robin and a 2-tick slice the four programs take turns. The ssh session waits 5 ticks and the average wait is 4.75 ticks.
  2. Trigger. At month end the invoice batch needs 24 ticks of CPU, and the server was set to first come, first served, so once the batch starts it runs to the end.
  3. What is affected. Everything that arrives after the batch: label printing, scan ingestion, and the engineer's ssh session.
  4. Symptoms. The ssh session shows nothing for 260 ms after each command, label printing stalls, and the depot reports a slow server, while the CPU is busy all the time.
  5. Metrics. Average wait rises from 4.75 to 18.50 ticks. The load average stays near 1: one program running, the rest queued behind it.
    invoice-batch: waits 0 ticks, finishes at 24
    label-print: waits 23 ticks, finishes at 27
    scan-ingest: waits 25 ticks, finishes at 29
    ssh session: waits 26 ticks, finishes at 30
  6. Logs. top shows one process using the whole CPU and the others ready but not running.
    top - load average: 1.00, 0.97, 0.62
    %Cpu(s): 99.6 us,  0.4 sy,  0.0 id
      PID S  %CPU COMMAND
     4120 R  99.6 invoice-batch
     4188 R   0.0 label-print
     4191 R   0.0 scan-ingest
  7. Reasoning. The CPU is not too slow; it is held. First come, first served never takes the CPU back, so one long job makes every short job wait behind it. That is the convoy effect.
  8. Mitigation. Give the CPU back in slices: with round robin the ssh session waits 5 ticks even while the 24-tick batch runs.
  9. Recovery. The batch now finishes at tick 30 instead of 24: time slicing costs the long job a little and saves everyone else a lot.
  10. Prevention. Run batch work at a lower priority with nice, or in its own cgroup with a CPU limit, and watch run-queue length and scheduling delay, not only CPU percentage.

Learn it properly

All simulators · Curriculum map