
Recall first from Priority scheduling
- Strict priority can indefinitely delay a ready task while higher-priority work stays runnable.
- Aging can guarantee CPU service when waiting tasks reach the highest priority and ties rotate in FIFO order.
- A sorted queue and fixed per-priority queues can both select the next task in constant time with respect to the number of ready tasks.
Divide CPU time by weight
4 min readThe last lesson used fixed priorities. Now you contrast stride and lottery share-based schedulers with MLFQ, and name CFS and EEVDF as the production generalizations.
A control panel and a background computation can compete for the same CPU on an embedded system. The panel needs time when it has an update to process, and the computation can use the CPU while the panel is waiting. Linux exposes a related distinction through its normal scheduling policy, where nice values influence how CPU time is shared among competing threads. sched(7): Linux scheduling policies
Start with a single CPU in the teaching kernel. Suppose two tasks stay runnable, A has weight 3, and B has weight 1. A a task's weight divided by the total weight of the runnable tasks competing for the CPU gives A a target of three quarters of the CPU time and B one quarter. These fractions describe repeated service. When A blocks and B is the only runnable task, B can use the whole CPU until another task is ready. The fractions are relative to the runnable set, so the scheduler needs both the weights and current task states.
Lottery scheduling treats each unit of weight as one ticket. In the OSTEP model, the scheduler draws one ticket uniformly from the tickets held by runnable tasks each time it chooses a task. Give A tickets 0 through 2 and B ticket 3. A draw of 3 selects B. With both tasks still runnable, the next draw uses the same four tickets because winning leaves each task's ticket count unchanged. B can win twice in a row even though A has three times as many tickets. Over many independent draws, A's fraction of wins tends toward three quarters. With equal CPU time per win, that fraction also describes A's CPU share. If tasks often block before their slices end, CPU time and dispatch counts can differ, so record actual run time when evaluating CPU share.
Stride scheduling uses the same weights and makes a deterministic choice. In the equal-slice model, assign each task a stride of scale / weight. Use a scale divisible by both weights so the arithmetic is exact. Each task also has a a value used to order runnable tasks, increased by one stride after the task completes a slice. Select the runnable task with the smallest pass. A larger weight produces a smaller stride, so a high-weight task's pass advances more slowly as it uses the CPU.
Use scale 12, start both passes at zero, and break ties in favor of A. A's stride is 4 and B's is 12. On the first dispatch A wins the tie, and the passes become (4, 0). B now has the smaller pass, so the next dispatch leaves (4, 12). A runs twice more, giving (8, 12) and then (12, 12). Both counters have advanced by the same amount, so the same relative order recurs if both tasks remain runnable. This trace assumes a fixed set of runnable tasks that use full slices. A scheduler that admits new tasks or handles wakeups also needs a pass initialization rule. For example, initializing a late arrival at zero after everyone else's pass has grown large can let it run repeatedly until its pass approaches theirs.
Change queue priority using CPU history
3 min readRecall the scheduling distinction from M3 between interactive tasks that run briefly and wait for an event, and CPU-bound tasks that routinely use their entire time slice. At task creation, the kernel may have very little information about which behavior to expect. A a scheduler with several priority queues that changes a task's queue based on its observed CPU use uses that behavior to decide where to schedule the task next. The queue level can change again as the workload changes.
In the teaching policy, new tasks start at the highest priority. The scheduler chooses from the highest nonempty runnable queue and uses round robin among tasks at the same level. Give higher queues shorter slices and lower queues longer ones. Short slices limit the time one task can run ahead of another at the same high priority. Longer slices let CPU-bound tasks run for more time per dispatch when the higher queues are empty. If a high-priority task wakes while a lower-priority task is running, the scheduler can preempt the lower-priority task and select from the higher queue.
For a simple trace, use three queues with slices of two, four, and eight ticks from highest to lowest. Let A perform a continuous computation and let B process input in one-tick bursts. A starts in the high queue. After using the full two ticks there, A is demoted to the middle queue. If A later uses all four ticks at that level, it moves to the low queue. At the lowest level, it stays there when it exhausts another slice. B initially blocks after one tick, so it has used only half its high-queue allowance. When B wakes, it can run at high priority ahead of A.
A scheduler should account for time a task uses at each level across dispatches. In this example, each level's allotment is one slice's worth of CPU time. If B uses another tick at high priority after waking, it has used two ticks there in total and is due for demotion. If that tick ends with B blocking, the demotion determines where it next becomes runnable. Resetting the allowance on every block or voluntary yield would let a CPU-bound task repeatedly stop just before its slice expires and stay at high priority. Keeping the remaining allowance prevents that pattern.
A periodic priority boost moves runnable tasks back to the top queue and resets their allotments. In this teaching policy, blocked tasks also receive the new level and allotment, and join the top queue when they wake. The boost gives a task that has spent a long time at low priority another chance to run alongside newer tasks. It also gives A a chance to receive high-priority service if its behavior changes from continuous computation to short bursts. A shorter boost interval returns tasks to the top more often. A longer interval gives the queue levels more time to reflect recent CPU use.
How Linux schedules ordinary tasks
2 min readGoing deeperExplore the mechanism
Linux's fair scheduler provides the production generalization of weighted CPU sharing for ordinary tasks. Classic CFS, the Completely Fair Scheduler, accounts for service with CPU execution time adjusted by the scheduling entity's weight. For the same amount of execution time, a higher-weight entity advances less in virtual runtime. That resembles the stride calculation. Classic CFS orders queued entities by virtual runtime in a red-black tree and uses the smallest virtual runtime as its selection rule. The tree supports ordered selection and updates as entities enter and leave the run queue.
The fair scheduler in Ubuntu 24.04's Linux 6.8 kernel uses EEVDF, short for Earliest Eligible Virtual Deadline First. EEVDF retains weighted virtual runtime accounting and uses two criteria when choosing which entity to run. First it determines eligibility from lag, the difference between the service an entity is owed under fair sharing and the service it has received. Nonnegative lag makes an entity eligible. Then it selects the eligible entity with the earliest virtual deadline. The virtual deadline reflects the requested slice in weighted virtual time. For otherwise comparable entities, a shorter request can give an earlier deadline and let that entity run sooner. Linux uses an augmented red-black tree to support the eligible deadline search.
Consider two runnable entities. One has already received more service than its fair share and has negative lag. The other is eligible because it is still owed service. Even if the first has the earlier virtual deadline, the second wins this selection. A virtual deadline is used for ordering within the fair scheduler. Linux's SCHED_DEADLINE is a separate scheduling policy. EEVDF Scheduler
CFS and EEVDF describe the production Linux scheduler here. The teaching kernel keeps the simpler policies already covered in the module. The lottery, stride, and MLFQ traces show how changing the selection rule changes the information a scheduler must maintain. Compare two traces by asking which task runs next, what state changes because it ran, and how the result depends on which other tasks are runnable.
A scheduling decision can update a task's CPU accounting and its position in a run queue. The next lesson, Sharing state and interior mutability, asks how kernel code can update shared scheduler state through a shared Rust reference.
Key takeaways
- Lottery scheduling uses tickets to set selection probabilities, so short runs can differ from the expected share.
- Stride scheduling selects the runnable task with the smallest pass and increases its pass after service.
- MLFQ uses CPU history to change priorities and periodically boosts tasks to the top queue.
- Linux's fair scheduler in kernel 6.8 uses EEVDF selection and accounts for CPU time using weighted virtual runtime.
Reflect
- Explain to a colleague, in two sentences: calculate each runnable task's probability of winning a lottery draw.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Man pagesched(7): Linux scheduling policies
- Kernel documentationCFS Scheduler: weighted runtime and the run queue
- Kernel documentationEEVDF Scheduler: eligibility and virtual deadlines
Official documentation first. Links checked when the lesson was published.