The problem
Three jobs, A, B and C, arrive together at time 0. Each needs 5 ms of CPU time. They wait in the ready queue, the line of jobs ready to run. The CPU can run only one at a time.
First come, first served runs A to the end, then B, then C. Shortest job first picks the same order here, since the jobs are equal. C waits 10 ms before it first runs. Average response time: 5 ms.
Round robin gives each job a short turn, a time slice (or quantum), here 1 ms. A hardware timer interrupts the CPU at a steady rate, so the kernel gets control back when the slice is used up.
One rotation
The scheduler takes the job at the head of the queue, A, and starts it on the CPU. B and C each move up one place.
A runs for 1 ms, then the timer interrupts. The interrupt forces the CPU back into the kernel, even though A never asked to stop. A has 4 ms of work left.
A still has work, so the kernel saves A's registers and puts A at the back of the queue. Then it loads B's registers and B starts its slice. This swap is a context switch.
B runs 1 ms, then C runs 1 ms. By 3 ms every job has started: response times 0, 1 and 2 ms, average 1 ms. With n jobs, none waits more than (n - 1) × slice = 2 ms for a turn.
The timer keeps ending turns: A, B, C, A, B, C... A finishes at 13 ms, B at 14 and C at 15. A job with no work left leaves the queue for good. Average turnaround time: 14 ms.
Choosing the slice
Round robin: response 1 ms, turnaround 14 ms. Running each to the end: response 5 ms, turnaround 10 ms. Sharing the CPU fairly makes every job start sooner but finish later.
Every switch costs time. With a 10 ms slice and a 1 ms switch, about a tenth of the CPU is lost; a 100 ms slice loses under 1%. But a very long slice turns round robin back into first come, first served.
Linux offers round robin as the real-time policy SCHED_RR. Its slice defaults to 100 ms; sched_rr_get_interval() reads it. Ordinary programs use a fair-share scheduler instead, covered in the lesson "Fair-share scheduling and Linux CFS".
What happens
;; step 1/11 · jobs job arrives needs A 0 ms 5 ms of CPU B 0 ms 5 ms of CPU C 0 ms 5 ms of CPU ready queue head -> A B C <- tail CPU one job at a time response time = first run - arrival turnaround time = finish - arrival
OSTEP tells this example in seconds (5-second jobs, a 1-second slice). The lesson uses milliseconds, closer to real slices; the arithmetic is the same. The worked example treats a context switch as free, as OSTEP does. Real switches take time, and they also cost the cache, TLB and branch-predictor state the job had built up.