Fair-share scheduling and Linux CFS
About this lesson
The multi-level feedback queue guesses which jobs are short. A fair-share scheduler asks a different question: has every task had its share of the CPU? Linux's Completely Fair Scheduler (CFS) answers it with one counter per task, the virtual runtime. It keeps runnable tasks sorted by that counter in a red-black tree and always runs the leftmost one. Weights from the nice value set each task's share, and a waking task is lifted so it cannot hog the CPU. Follow three tasks through 144 ms on one core: two ordinary ones and a nice one, one of which sleeps on the disk. The lessons on round robin and the time slice and on the multi-level feedback queue are the background; the context switch from the processes module is what happens each time a task leaves the CPU.
Fair share, not priority
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.
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 shorterRead the lesson step by step
Fair share, not priority
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.
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 shorterSorted by vruntime
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.
task nice weight A 0 1024 B 5 335 C 0 1024 the tree, leftmost first: A 0.0 B 0.0 C 0.0 t = 0 A is leftmost: A goes onto the CPU in the figure a task stands as far right on the run queue as its vruntimeA slice is a share
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.
slice = period × weight / (sum of runnable weights) = 48 × 1024 / (1024 + 335 + 1024) = 48 × 1024 / 2383 = 20.6 ms vruntime += runtime × 1024 / weight A: 0 + 20.6 × 1024 / 1024 = 20.6 t = 0..20.6 A runs t = 20.6 A back in the tree, at the rightNice tasks count faster
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.
B: slice = 48 × 335 / 2383 = 6.7 ms vruntime 0 + 6.7 × 1024 / 335 = 20.6 each step of nice is a factor of about 1.25: nice 5 vs nice 0: 1024 / 335 = 3.06 B as a real program (C99, POSIX): #define _XOPEN_SOURCE 700 #include <stdio.h> #include <sys/resource.h> int main(void) { /* be nice: nice 5, so CFS gives weight 335, not 1024 */ if (setpriority(PRIO_PROCESS, 0, 5) != 0) { perror("setpriority"); return 1; } printf("nice %d\n", getpriority(PRIO_PROCESS, 0)); volatile unsigned long x = 0; while (x < 50000000UL) x++; /* CPU-bound work */ return 0; }Equal vruntime is fair
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.
t = 27.4..48.0 C runs 20.6 ms, then reads the disk task CPU time share vruntime A 20.6 ms 43 % 20.6 B 6.7 ms 14 % 20.6 C 20.6 ms 43 % 20.6 equal vruntime = CPU time in proportion to weightFewer tasks, longer slices
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.
C sleeps: it is not in the tree, it is not counted A: slice = 48 × 1024 / (1024 + 335) = 36.2 ms B: slice = 48 × 335 / (1024 + 335) = 11.8 ms t = 48.0..84.2 A runs, vruntime 20.6 -> 56.8 t = 84.2..96.0 B runs, vruntime 20.6 -> 56.8Waking up: no catching up
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.
t = 96.0 B back in the tree at 56.8 C wakes: vruntime 20.6 smallest in the tree: 56.8 C lifted to 56.8 without the lift, C would be leftmost until its vruntime passed 56.8: 36.2 ms with A and B shut out the cost: a task that sleeps often loses a littleSharing again
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.
t = 96.0 A runs 20.6 ms vruntime 56.8 -> 77.4 t = 116.6 B runs 6.7 ms vruntime 56.8 -> 77.4 t = 123.4 C runs 20.6 ms vruntime 56.8 -> 77.4 over the 144 ms: A 77.4 ms of CPU B 25.3 ms of CPU C 41.3 ms of CPUWhy a tree
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.
CFS as a Python simulation: LATENCY, NICE0 = 48, 1024 # ms; weight of nice 0 weight = {"A": 1024, "B": 335, "C": 1024} # nice 0, 5, 0 nap = {"C": 48} # C waits 48 ms for the disk once vr = {j: 0.0 for j in weight} # vruntime tree = ["A", "B", "C"] # runnable, leftmost first asleep, t = {}, 0.0 while t < 144 - 1e-9: for j in [j for j in asleep if asleep[j] <= t + 1e-9]: del asleep[j] # wake: lift to the smallest vruntime vr[j] = max(vr[j], min(vr[k] for k in tree)) tree.append(j) tree.sort(key=lambda j: round(vr[j], 6)) # ties keep their order j = tree.pop(0) # the leftmost task runs total = weight[j] + sum(weight[k] for k in tree) s = LATENCY * weight[j] / total # its slice of the period v0 = vr[j] vr[j] += s * NICE0 / weight[j] print(f"{t:5.1f} {j} runs {s:4.1f} ms vruntime {v0:4.1f} -> {vr[j]:4.1f}") t += s if j in nap: asleep[j] = t + nap.pop(j) # sleeps: leaves the tree else: tree.append(j) prints: 0.0 A runs 20.6 ms vruntime 0.0 -> 20.6 20.6 B runs 6.7 ms vruntime 0.0 -> 20.6 27.4 C runs 20.6 ms vruntime 0.0 -> 20.6 48.0 A runs 36.2 ms vruntime 20.6 -> 56.8 84.2 B runs 11.8 ms vruntime 20.6 -> 56.8 96.0 A runs 20.6 ms vruntime 56.8 -> 77.4 116.6 B runs 6.7 ms vruntime 56.8 -> 77.4 123.4 C runs 20.6 ms vruntime 56.8 -> 77.4
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Three tasks are runnable. Their vruntimes are A 30.0, B 12.5 and C 18.0. Which does CFS run next?
CFS always picks the leftmost task in its tree: the one with the smallest vruntime, the one furthest behind its fair share.
02With a 48 ms period, tasks of weight 1024, 1024 and 335 are runnable. How long is the slice of the weight-335 task?
slice = period × weight / total weight = 48 × 335 / 2383 = 6.7 ms. The weight-1024 tasks get 48 × 1024 / 2383 = 20.6 ms each.
03Task B has nice 5 (weight 335). It runs 6.7 ms. How much does its vruntime grow?
vruntime grows by runtime × 1024 / weight. For weight 335 that is about 3.06 times real time, so 6.7 ms of CPU counts as 20.6. That is why B gets a smaller share yet keeps level with the others.
04Task C slept for 48 ms. It wakes with vruntime 20.6 while the tree's smallest vruntime is 56.8. What does CFS do?
Left at 20.6, C would stay leftmost for about 36 ms and shut the others out. Lifting it to the smallest vruntime in the tree avoids that; the cost is that tasks which sleep often get a little less than their share.
05Why does CFS keep runnable tasks in a red-black tree rather than a sorted list?
A task goes back into the tree after every slice. In a sorted list that insert scans the list; a balanced red-black tree keeps it logarithmic, and the leftmost task is the next to run.
Glossary
- Fair-share scheduling
- Scheduling that aims to give every task a set share of the CPU over time, instead of strict priorities. Also called proportional-share scheduling.
- Completely Fair Scheduler (CFS)
- The default Linux scheduler from 2.6.23. It runs the task with the smallest virtual runtime. Since Linux 6.6 it is being replaced by EEVDF.
- Virtual runtime (vruntime)
- A task's CPU time scaled by its weight: runtime × 1024 / weight. Equal vruntime means CPU time in proportion to weight.
- Weight
- A number CFS takes from the nice value. Nice 0 is 1024; each step of nice changes it by a factor of about 1.25.
- Nice value
- A Unix setting from -20 to +19 (default 0). A higher nice value means the task asks for less CPU.
- sched_latency
- The period in which every runnable task should run once; 48 ms in this lesson's example. Slices are shares of it.
- min_granularity
- The shortest slice CFS will give, so that very many tasks do not cause endless context switches; 6 ms in the example.
- Red-black tree
- A balanced binary search tree. Insert and delete take O(log n) steps, so it stays fast with thousands of tasks.
Notes and sources
- 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.
- 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.
- CFS was merged in Linux 2.6.23 and was the default scheduler for years. Since Linux 6.6 the kernel has moved to EEVDF (Earliest Eligible Virtual Deadline First), which also keeps a virtual run time per task.
- Lowering a nice value (asking for more CPU) needs privilege; raising it, as task B does, does not. Each step of nice changes the weight by a factor of about 1.25.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 9: Scheduling: Proportional Share
- Berkeley CS162, Lecture 12: Scheduling 3 (Linux CFS, proportional shares)
- Linux kernel documentation: CFS Scheduler
- Linux kernel documentation: EEVDF Scheduler
- Linux man-pages: sched(7)
- Linux man-pages: setpriority(2)
- MIT 6.1810 (2024), lecture notes: Threads and pre-emption
Quiz Unlocks after the last step.