◀ LEFTMOST RUNS NEXT020406080MSCPUA NICE 01024B NICE 5335C NICE 01024WAITING FOR DISKA0.0B0.0C0.0TIME0.0 msSLICE-ABC
Left: the run queue, each task as far right as its vruntimeRight: the CPU and the disk shelf; front: weights, vruntime, clockBlocks: tasks A (nice 0), B (nice 5) and C (nice 0, sleeps)The run queue on the left: one lane per task, and a ruler of vruntime, so the leftmost task runs next. The CPU and the waiting-for-disk shelf are on the right; in front, the weights, the live vruntime of each task and the clock. A and C (weight 1024) run 20.6 ms slices; B (nice 5, weight 335) runs only 6.7 ms, yet each lands at the same vruntime. C sleeps from 48 to 96 ms, wakes at 20.6 and is lifted to 56.8, the smallest vruntime in the tree. 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.

Virtual runtime

  1. 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.

Weights and nice

Sleeping and waking

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.