Virtual runtime
Fair share means every task gets its share of the CPU. Linux's Completely Fair Scheduler (CFS) keeps no priority queues. It counts each task's virtual runtime (vruntime): roughly, how much CPU it has had. The task with the least runs next.
Three tasks start at 0 ms, all with vruntime 0. CFS keeps runnable tasks in a red-black tree, a structure that stays sorted, here by vruntime. The leftmost task runs next. Ties keep their arrival order, so A goes first.
A's slice is its share of the 48 ms period: 48 × 1024 / 2383 = 20.6 ms, where 2383 is all three weights added up. After it, A's vruntime is 20.6, so A moves right. B is now leftmost.
Weights and nice
B asked to be nice: nice 5, weight 335 instead of 1024. Its slice is only 6.7 ms. But its vruntime grows about three times faster than real time: 6.7 × 1024 / 335 = 20.6. B lands level with A.
C runs 20.6 ms, then waits for the disk. After this 48 ms period all three reached vruntime 20.6. A and C each had 20.6 ms of CPU, B had 6.7 ms: shares in proportion to weight.
Sleeping and waking
A sleeping task leaves the tree. Only A and B share the 48 ms period now, so both slices grow: A runs 36.2 ms, B gets 11.8 ms. A ends at vruntime 56.8, so B is leftmost and runs.
At 96 ms C wakes. Its vruntime is still 20.6, far behind A and B at 56.8: it would run 36.2 ms straight to catch up. CFS instead lifts it to the smallest vruntime in the tree, 56.8.
All three share again: A runs 20.6 ms, B 6.7 ms, then C 20.6 ms from 123.4 ms. Sleeping earned C no extra CPU, and A and B were never shut out. At 144 ms all three stand at 77.4.
With thousands of tasks, a sorted list is slow to keep in order. In a red-black tree, inserting and removing a task take O(log n) steps. Linux ran CFS from 2.6.23; since 6.6 it has moved to EEVDF, which still tracks virtual run time.
What happens
;; step 1/9 · fair
ideal: n tasks, each gets 1/n of the CPU at once
real: one core, one task at a time, so CFS keeps
a counter per task and repairs the gap
vruntime the task's CPU time, scaled by its weight
rule run the task with the smallest vruntime
period sched_latency = 48 ms: every runnable task
runs once in this time
floor min_granularity = 6 ms: no slice is shorter
The figure plays an exact simulation of these rules with one CPU core; a context switch is taken to cost no time, and times are rounded to 0.1 ms. A waking task is lifted to the smallest vruntime in the tree, as OSTEP describes. The settings follow OSTEP chapter 9: a 48 ms period (sched_latency), a 6 ms minimum slice (min_granularity), slice = period × weight / total weight, and vruntime += runtime × 1024 / weight, with the nice-to-weight table from the Linux kernel (nice 0 = 1024, nice 5 = 335). Real kernels tune these numbers and add adjustments this lesson leaves out.