Linux Scheduler – Conceptual Walkthrough
This document is a consolidated explanation of Linux scheduling concepts, based on a progressive
question-and-answer discussion. It is intended as a long-term reference for understanding how the
Linux scheduler works internally.
1. Completely Fair Scheduler (CFS) and Timeslices
CFS models an ideal multitasking processor where each runnable task receives CPU time
proportional to its weight (derived from nice value). Instead of fixed timeslices, CFS uses a target
latency, which is the time window in which all runnable tasks should run at least once.
To prevent excessive context switching when many tasks are runnable, CFS enforces a minimum
granularity (default 1 ms). This ensures that even low-priority tasks run for a meaningful amount of
time and that scheduling overhead remains bounded.
2. Nice Values, Weights, and CPU Share
Nice values do not directly specify timeslices. Instead, each nice value maps to a weight. CPU time
is divided proportionally based on these weights. What matters is the ratio between weights, not
their absolute values.
For example, two tasks with weight ratio 3:1 will receive approximately 75% and 25% of CPU time,
regardless of their absolute nice values.
3. Virtual Runtime (vruntime)
vruntime is the core accounting mechanism of CFS. It represents the amount of processor time a
task has received, normalized by its weight. High-priority tasks accumulate vruntime more slowly;
low-priority tasks accumulate it faster.
CFS always selects the runnable task with the smallest vruntime. This task is considered the most
behind relative to its fair share.
4. delta_exec and Scheduler Accounting
delta_exec measures the actual CPU time a task has consumed since the last accounting update.
This raw time is then weighted based on task priority and added to vruntime.
This incremental accounting ensures accurate fairness without expensive global recomputation.
5. Timeline-Based Examples (2 and 3 Tasks)
When multiple tasks are runnable, each task runs only until its vruntime is no longer the smallest.
Lower-priority tasks run for shorter durations because their vruntime increases more rapidly.
Adding more tasks does not change the rule: the scheduler always runs the task that is furthest
behind in weighted progress.
6. What 'Furthest Behind' Means
A task is furthest behind if it has received less CPU time relative to its entitlement. This is
determined by vruntime, not raw execution time.
7. What Happens If a Task Exceeds Its Share
If a task consumes more CPU than its proportional share, its vruntime rises faster relative to others,
making it ineligible to run until fairness is restored.
8. schedule() and Scheduler Classes
Linux uses multiple scheduler classes (Deadline, Real-Time, CFS, Idle). The schedule() function
selects the highest-priority class with runnable tasks, then lets that class choose a task.
CFS only governs normal tasks and never overrides real-time or deadline scheduling.
9. Kernel Preemption
Linux is a preemptive kernel, meaning kernel code can be interrupted and rescheduled, provided it
is safe. Safety is determined primarily by whether the current task holds locks.
10. preempt_count and need_resched
preempt_count tracks whether preemption is currently allowed. If it is zero and a higher-priority task
becomes runnable, the scheduler may preempt the current task.
need_resched is a flag indicating that a reschedule should occur at the next safe point.
11. Interrupts and Task Wakeups
Interrupt handlers run immediately when hardware events occur. They may wake sleeping tasks by
marking them runnable, but they do not perform scheduling themselves.
Actual task switching occurs after the interrupt finishes and the kernel verifies that preemption is
safe.
12. Priority Inversion vs Kernel Non-Preemptibility
Allowing a low-priority task to continue briefly while holding a lock is not true priority inversion. The
delay is bounded and required for correctness. Linux uses priority inheritance when true priority
inversion occurs due to blocking on locks.
13. Processor Affinity
Linux enforces hard processor affinity using a per-task CPU bitmask (cpus_allowed). A task may
only run on CPUs allowed by this mask.
The kernel enforces this at task creation, during affinity changes via migration threads, and during
load balancing.