5. Scheduling Theory and Policy
A bare-metal multiprocessor provides a fixed number of physical CPU cores, each capable of advancing a single hardware instruction stream on any clock cycle. When the number of runnable threads exceeds the available core count, the hardware itself has no mechanism to enforce proportional fairness, bounded interactive latency, or priority arbitration. Without supervisory intervention, the first thread dispatched onto a core would retain control of the fetch-decode-execute pipeline until it halted or terminated, starving all other workloads on the machine.
The kernel scheduler resolves this physical contention by time-multiplexing hardware cores across competing execution contexts. Consistent with the architectural principle established in Section 1.3, the scheduling subsystem strictly separates hardware-enforced mechanism from kernel policy:
- Mechanism (Hardware and Dispatch Layer): Periodic hardware timer interrupts (the per-core Local APIC Timer on x86-64 and the ARM Generic Timer on ARM64), architectural trapframe serialization, and context-switch routines that exchange stack pointers (
RSP/SP_EL1), thread-local base registers (FS_BASE/TPIDR_EL0), and page-table roots (CR3/TTBR0_EL1). - Policy (Arbitration Algorithm): The selection rule that evaluates the set of runnable scheduling entities to determine which thread occupies a physical core next, the duration of its execution quantum, and the target core selected to preserve hardware cache locality.
5.1 Scheduling Dynamics and Performance Metrics
The kernel invokes the scheduler at four deterministic state transitions within a thread’s lifecycle: when a running thread blocks voluntarily on an I/O descriptor or synchronization primitive (transitioning to a wait queue), when an external hardware interrupt or lock release awakens a blocked thread (transitioning to a ready queue), when a running thread exits, or when the hardware timer interrupt asserts preemption upon expiration of the thread’s allotted execution quantum.
The operational behavior of a scheduling policy is quantified by three primary metrics:
- Turnaround Time (
T_completion - T_arrival): The total elapsed interval from the instant a job enters the ready queue until its final instruction retires. Throughput-oriented batch pipelines and compilation workloads optimize primarily for minimal mean turnaround time. - Wait Time: The cumulative duration a job resides in the ready queue, runnable and awaiting dispatch onto a physical core, excluding productive CPU execution and voluntary I/O wait intervals.
- Response Time (
T_first_run - T_arrival): The latency interval between a job’s arrival (or wakeup from a blocked state) and its initial dispatch onto an execution core. Interactive user interfaces, real-time audio pipelines, and low-latency network RPC services optimize primarily for bounded tail response time.
These metrics represent competing optimization objectives. Minimizing mean turnaround time requires executing short jobs to completion without interruption, which reduces context-switch frequency and preserves cache locality; minimizing response time requires frequent preemption so that newly awakened interactive threads receive processor time within strict latency bounds, even at the cost of increased context-switch overhead and reduced throughput for long-running compute jobs.
5.2 Fundamental Scheduling Policies
Classical scheduling theory establishes three foundational policies that formalize the trade-offs between throughput, fairness, and response latency:
- First-Come, First-Served (FCFS): Dispatches runnable jobs non-preemptively in strict arrival order via a FIFO queue. Although dispatch complexity is O(1), FCFS exhibits the convoy effect: a single compute-intensive task arriving ahead of multiple short interactive tasks monopolizes the processor until completion, inflating both mean turnaround time and response latency for all subsequent arrivals.
- Shortest Job First (SJF) and Shortest Remaining Time First (SRTF): Selects the runnable job possessing the minimum remaining CPU burst duration. For any static arrival set, SJF achieves the provably optimal minimum mean turnaround time. In general-purpose operating systems, however, future execution burst durations cannot be determined a priori without historical estimation, and a sustained stream of short tasks induces indefinite starvation for long-running compute workloads.
- Round-Robin (RR): Enforces preemptive time-slicing by assigning each runnable thread a fixed execution quantum (
q). Upon quantum expiration, the timer interrupt preempts the active thread and appends it to the tail of the ready queue. For N runnable threads, Round-Robin bounds maximum wait latency prior to dispatch at(N - 1) × q. Selection ofqrequires balancing preemption responsiveness against microarchitectural overhead: an excessively small quantum causes context-switch register saves and L1/L2 cache refills to consume a substantial fraction of total core cycles, whereas an excessively large quantum degrades interactive responsiveness toward FCFS behavior.
Listing 5-1. A simulator for FCFS, SJF, and RR (C++20). (code/ch05/sched_sim.cpp)
// ch05/sched_sim.cpp: FCFS, SJF, and round-robin, computed by hand inside a simulator.
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
struct Job {
std::string name;
int arrival;
int burst;
};
struct Stats {
int completion;
int turnaround;
int wait;
};
// First-Come, First-Served: run jobs in arrival order, each to completion.
std::vector<Stats> fcfs(const std::vector<Job>& jobs) {
std::vector<Stats> out;
int t = 0;
for (const Job& j : jobs) {
t = std::max(t, j.arrival) + j.burst;
out.push_back({t, t - j.arrival, t - j.arrival - j.burst});
}
return out;
}
// Shortest Job First: the order in which SJF runs jobs that arrive together.
// Running this order with fcfs() gives the SJF schedule.
std::vector<Job> shortest_first(std::vector<Job> jobs) {
std::stable_sort(jobs.begin(), jobs.end(),
[](const Job& a, const Job& b) { return a.burst < b.burst; });
return jobs;
}
// Round-Robin: rotate through a ready queue in fixed time slices.
std::vector<Stats> rr(const std::vector<Job>& jobs, int quantum) {
std::deque<int> ready;
std::vector<int> remaining(jobs.size());
std::vector<int> completion(jobs.size());
for (size_t i = 0; i < jobs.size(); ++i) {
remaining[i] = jobs[i].burst;
ready.push_back(static_cast<int>(i));
}
int t = 0;
while (!ready.empty()) {
int i = ready.front();
ready.pop_front();
int run = std::min(quantum, remaining[i]);
t += run;
remaining[i] -= run;
if (remaining[i] == 0) {
completion[i] = t;
} else {
ready.push_back(i);
}
}
std::vector<Stats> out;
for (size_t i = 0; i < jobs.size(); ++i) {
out.push_back({completion[i], completion[i] - jobs[i].arrival,
completion[i] - jobs[i].arrival - jobs[i].burst});
}
return out;
}
void report(const std::string& title, const std::vector<Job>& jobs,
const std::vector<Stats>& stats) {
double avg_turnaround = 0, avg_wait = 0;
std::cout << title << "\n";
std::cout << " job completion turnaround wait\n";
for (size_t i = 0; i < jobs.size(); ++i) {
std::cout << " " << jobs[i].name << " "
<< std::setw(6) << stats[i].completion << " "
<< std::setw(6) << stats[i].turnaround << " "
<< std::setw(6) << stats[i].wait << "\n";
avg_turnaround += stats[i].turnaround;
avg_wait += stats[i].wait;
}
avg_turnaround /= jobs.size();
avg_wait /= jobs.size();
std::cout << " average turnaround " << avg_turnaround
<< ", average wait " << avg_wait << "\n\n";
}
int main() {
const std::vector<Job> jobs = {
{"A", 0, 10},
{"B", 0, 5},
{"C", 0, 5},
};
report("FCFS (arrival order)", jobs, fcfs(jobs));
const std::vector<Job> by_burst = shortest_first(jobs);
report("SJF (shortest first)", by_burst, fcfs(by_burst));
report("RR (quantum 5)", jobs, rr(jobs, 5));
}
Output 5-1. The completion times agree with Figure 5-2.
FCFS (arrival order)
job completion turnaround wait
A 10 10 0
B 15 15 10
C 20 20 15
average turnaround 15, average wait 8.33333
SJF (shortest first)
job completion turnaround wait
B 5 5 0
C 10 10 5
A 20 20 10
average turnaround 11.6667, average wait 5
RR (quantum 5)
job completion turnaround wait
A 20 20 10
B 10 10 5
C 15 15 10
average turnaround 15, average wait 8.33333
Listing 5-2. The same simulator in Rust. (code/ch05/sched_sim.rs)
// ch05/sched_sim.rs: the same three policies, simulated in Rust.
use std::collections::VecDeque;
#[derive(Clone)]
struct Job {
name: &'static str,
arrival: i32,
burst: i32,
}
#[derive(Clone, Copy)]
struct Stats {
completion: i32,
turnaround: i32,
wait: i32,
}
// First-Come, First-Served: run jobs in arrival order, each to completion.
fn fcfs(jobs: &[Job]) -> Vec<Stats> {
let mut out = Vec::new();
let mut t = 0;
for j in jobs {
t = t.max(j.arrival) + j.burst;
out.push(Stats {
completion: t,
turnaround: t - j.arrival,
wait: t - j.arrival - j.burst,
});
}
out
}
// Shortest Job First: the order in which SJF runs jobs that arrive together.
// Running this order with fcfs() gives the SJF schedule.
fn shortest_first(jobs: &[Job]) -> Vec<Job> {
let mut sorted = jobs.to_vec();
sorted.sort_by_key(|j| j.burst);
sorted
}
// Round-Robin: rotate through a ready queue in fixed time slices.
fn rr(jobs: &[Job], quantum: i32) -> Vec<Stats> {
let mut ready: VecDeque<usize> = VecDeque::new();
let mut remaining: Vec<i32> = jobs.iter().map(|j| j.burst).collect();
let mut completion = vec![0; jobs.len()];
for i in 0..jobs.len() {
ready.push_back(i);
}
let mut t = 0;
while let Some(i) = ready.pop_front() {
let run = quantum.min(remaining[i]);
t += run;
remaining[i] -= run;
if remaining[i] == 0 {
completion[i] = t;
} else {
ready.push_back(i);
}
}
(0..jobs.len())
.map(|i| Stats {
completion: completion[i],
turnaround: completion[i] - jobs[i].arrival,
wait: completion[i] - jobs[i].arrival - jobs[i].burst,
})
.collect()
}
fn report(title: &str, jobs: &[Job], stats: &[Stats]) {
let avg_turnaround: f64 =
stats.iter().map(|s| s.turnaround as f64).sum::<f64>() / stats.len() as f64;
let avg_wait: f64 =
stats.iter().map(|s| s.wait as f64).sum::<f64>() / stats.len() as f64;
println!("{title}");
println!(" job completion turnaround wait");
for (i, j) in jobs.iter().enumerate() {
println!(
" {} {:6} {:6} {:6}",
j.name, stats[i].completion, stats[i].turnaround, stats[i].wait
);
}
println!(" average turnaround {avg_turnaround:.2}, average wait {avg_wait:.2}\n");
}
fn main() {
let jobs = [
Job { name: "A", arrival: 0, burst: 10 },
Job { name: "B", arrival: 0, burst: 5 },
Job { name: "C", arrival: 0, burst: 5 },
];
report("FCFS (arrival order)", &jobs, &fcfs(&jobs));
let by_burst = shortest_first(&jobs);
report("SJF (shortest first)", &by_burst, &fcfs(&by_burst));
report("RR (quantum 5)", &jobs, &rr(&jobs, 5));
}
Output 5-2.
FCFS (arrival order)
job completion turnaround wait
A 10 10 0
B 15 15 10
C 20 20 15
average turnaround 15.00, average wait 8.33
SJF (shortest first)
job completion turnaround wait
B 5 5 0
C 10 10 5
A 20 20 10
average turnaround 11.67, average wait 5.00
RR (quantum 5)
job completion turnaround wait
A 20 20 10
B 10 10 5
C 15 15 10
average turnaround 15.00, average wait 8.33
5.3 Proportional-Share Scheduling: Linux CFS and EEVDF
Historical Unix schedulers employed Multi-Level Feedback Queues (MLFQ), partitioning runnable threads across discrete priority levels and demoting threads that exhausted full execution quanta while retaining I/O-bound interactive threads at elevated priorities. While effective in practice, heuristic MLFQ policies depend on empirically tuned priority-decay constants and permit adversarial workloads to preserve artificially high priority by relinquishing the processor immediately prior to quantum expiration.
To establish a rigorous mathematical model for processor allocation, Linux replaced discrete feedback queues with proportional-share scheduling, implemented initially by the Completely Fair Scheduler (CFS) and superseded in Linux 6.6 by EEVDF (Earliest Eligible Virtual Deadline First).
5.3.1 Virtual Runtime (vruntime) Accounting
CFS models an idealized fluid-flow processor capable of executing N equal-priority runnable tasks concurrently, allocating exactly 1 / N of the physical execution rate to each task at every instant. Because a physical core executes only one thread at any clock cycle, the kernel tracks each task’s cumulative weighted CPU consumption in a monotonically increasing 64-bit nanosecond counter termed virtual runtime (vruntime).
When task i executes for an actual physical duration Δexec_i, the scheduler increments its virtual runtime in inverse proportion to its priority weight weight_i:
Δvruntime_i = Δexec_i × (weight_0 / weight_i)
In this formulation, weight_0 = 1,024 represents the baseline weight corresponding to POSIX priority nice = 0. Linux maps the forty integer nice values (-20 highest priority to +19 lowest priority) onto a geometric weight table in which each unit decrement in nice increases CPU share by approximately 10% (a weight ratio of approximately 1.25 per step).
To illustrate the arithmetic directly, consider three runnable threads, Task A (nice = -10, weight = 9,548), Task B (nice = 0, weight = 1,024), and Task C (nice = +10, weight = 110), each executing for an identical physical duration of 4.00 ms:
| Task | Nice Level | CFS Weight | Physical Execution (Δexec) | Virtual Runtime Increment (Δvruntime) | Scheduling Effect |
|---|---|---|---|---|---|
| Task A | -10 (High) | 9,548 | 4.00 ms | 4.00 × (1024 / 9548) = +0.43 ms | Advances ~9.3× slower than wall time; remains nearest rb_leftmost |
| Task B | 0 (Default) | 1,024 | 4.00 ms | 4.00 × (1024 / 1024) = +4.00 ms | Advances at a 1:1 ratio with physical wall-clock execution |
| Task C | +10 (Batch) | 110 | 4.00 ms | 4.00 × (1024 / 110) = +37.24 ms | Advances ~9.3× faster than wall time; migrates to the right subtree |
This weighted accounting enforces starvation freedom without ad hoc priority-boost timers. Every physical cycle consumed by Task A strictly increases its vruntime, guaranteeing eventual dispatch of Task B and Task C, while Task A’s attenuated virtual clock rate allocates each runnable thread a long-term CPU share proportional to weight_i / Σ weight_j.
5.3.2 Red-Black Tree Runqueue and the Transition to EEVDF
To identify the runnable entity with the minimum vruntime efficiently under high thread counts, each per-CPU runqueue organizes its runnable scheduling entities in a self-balancing Red-Black Tree keyed strictly by vruntime:
- O(1) Selection (
rb_leftmost): The runqueue caches a direct pointer (rb_leftmost) referencing the leftmost node in the tree, corresponding to the runnable thread with the minimumvruntime. Selecting the next thread for dispatch executes in constant O(1) time. - O(log N) Insertion and Removal: Enqueueing a newly awakened thread or re-inserting a preempted thread requires logarithmic O(log N) time relative to the core’s local runnable count N. To prevent a thread that slept for an extended duration from monopolizing the processor upon wakeup with an outdated
vruntime, the kernel clamps its initialvruntimeagainst the runqueue’s monotonic floor (min_vruntime). - Minimum Execution Granularity (
min_granularity): Preempting the active thread upon every microsecond shift invruntimewould induce excessive context-switch overhead and L1/L2 cache thrashing. CFS enforces a minimum execution interval (sysctl_sched_min_granularity, typically 0.75–3.0 ms) prior to involuntary preemption. - EEVDF (Earliest Eligible Virtual Deadline First): Under pure CFS, equal-weight tasks received identical dispatch priority irrespective of whether a task required a 0.5 ms interactive wakeup or a 20 ms batch slice. Linux 6.6 adopted EEVDF, augmenting each scheduling entity with two formal parameters:
- Eligibility (
lag >= 0): A task is eligible for dispatch only when its accumulated virtual runtime does not exceed the weighted mean virtual runtime of the runqueue. - Virtual Deadline (
d_i = vruntime_i + slice_i / weight_i): Among all eligible tasks, EEVDF selects the entity with the earliest virtual deadline. Latency-sensitive threads configured with short request slices (slice_i) obtain earlier virtual deadlines and preempt long-slice compute tasks deterministically while preserving long-term proportional CPU share.
- Eligibility (
5.4 Priority Inversion and Priority Inheritance Protocols
In priority-preemptive scheduling environments (including real-time Linux SCHED_FIFO/SCHED_RR, Darwin/XNU audio and compositor subsystems, and embedded avionics kernels), strict priority preemption combined with mutual exclusion locks introduces a critical liveness pathology termed unbounded priority inversion.
Consider three threads sharing a single execution core: a high-priority real-time thread THigh, a medium-priority compute-bound thread TMed, and a low-priority thread TLow that shares mutex M with THigh. The failure evolves across four sequential steps:
- T = 0 ms: TLow executes, acquires mutex M, and enters its critical section.
- T = 2 ms: THigh becomes runnable upon an interrupt. Because prio(THigh) > prio(TLow), the scheduler preempts TLow and dispatches THigh. THigh invokes
lock(M), observes that M is held by TLow, and blocks on M’s wait queue. - T = 3 ms: TMed becomes runnable. Although TMed does not reference mutex M, THigh is blocked and prio(TMed) > prio(TLow); consequently, the scheduler dispatches TMed ahead of TLow.
- Unbounded Blocking: TMed, along with any subsequently arriving medium-priority threads, preempts TLow indefinitely. Because TLow receives zero CPU cycles to complete its critical section and invoke
unlock(M), THigh experiences unbounded blocking latency induced by lower-priority work. This exact interaction produced repeated watchdog resets on the 1997 Mars Pathfinder spacecraft when medium-priority communications tasks preempted a low-priority meteorological task holding a shared bus mutex required by the high-priority bus management task.
Production kernels mitigate unbounded priority inversion through two synchronization protocols enforced within mutex acquisition:
- Priority Inheritance Protocol (
PTHREAD_PRIO_INHERIT, LinuxFUTEX_LOCK_PI, Darwinos_unfair_lock): When THigh blocks on mutex M owned by TLow, the kernel elevates TLow’s effective scheduling priority to max(eff_prio(TLow), prio(THigh)). Executing at THigh’s priority, TLow immediately preempts TMed, completes its critical section, and releases M. Uponunlock(M), the kernel restores TLow’s base priority and dispatches THigh. Priority inheritance propagates transitively across nested lock-ownership chains. - Priority Ceiling Protocol (
PTHREAD_PRIO_PROTECT): Each mutex M is assigned a static ceiling priority equal to the maximum priority of any thread permitted to acquire M. Immediately upon locking M, the acquiring thread is elevated to M’s ceiling priority. This protocol prevents both unbounded priority inversion and circular-wait deadlocks, at the cost of requiring complete static knowledge of lock acquisition sets prior to deployment.
Unbounded priority inversion arises when a medium-priority thread indirectly blocks a high-priority waiter by preempting the low-priority owner of a shared mutex. Priority inheritance restores bounded blocking latency by temporarily elevating the lock owner’s effective priority to match the highest-priority blocked waiter until unlock().
5.5 Multi-Core Work Stealing, Cache Affinity, and NUMA Domains
In symmetric multiprocessing (SMP) and Non-Uniform Memory Access (NUMA) architectures, maintaining a single global ready queue creates a severe serialization bottleneck: every context switch, wakeup, and timer tick must acquire a single global runqueue spinlock, saturating inter-core cache coherence interconnects.
Modern kernels resolve this contention by partitioning scheduling state into per-CPU runqueues managed by a topology-aware load balancer:
- Cache Affinity: While a thread executes on Core 2, its active working set populates Core 2’s private L1/L2 caches and TLBs. Dispatching that thread back onto Core 2 following a brief blocking interval eliminates cold cache refills and cross-core Modified-Exclusive-Shared-Invalid (
MESI) coherence invalidations. Schedulers enforce soft affinity automatically by biasing wakeups toward the thread’s previous core, while providing explicit pinning interfaces (sched_setaffinity) for hard affinity in deterministic latency-critical deployments. - Work-Stealing Load Balancing: Partitioned runqueues permit transient imbalance in which one core holds multiple queued threads while a sibling core becomes idle. Upon emptying its local runqueue, or at periodic load-balancing ticks, an idle core invokes work stealing, migrating a cache-cold runnable thread from an overloaded sibling runqueue.
- Hierarchical Scheduling Domains and NUMA Awareness: The kernel encodes physical hardware topology as a hierarchy of scheduling domains:
- Simultaneous Multithreading (
SMT) / Hyperthread Siblings: Hardware threads sharing a physical core and L1/L2 caches (minimal migration cost). - Core / Last-Level Cache (LLC) Domain: Distinct cores sharing a unified L3 cache (moderate migration cost limited to L1/L2 warm-up).
- NUMA Socket Domain: Independent processor sockets connected across inter-socket links (Intel Ultra Path Interconnect (
UPI), AMD Infinity Fabric) with local Dynamic Random-Access Memory (DRAM) controllers. Migrating a thread across NUMA boundaries decouples it from its locally allocated physicalDRAMpages, increasing remote memory latency by 30–100%. Load balancers therefore equalize queue depth within an LLC domain first and migrate threads across NUMA nodes only when load imbalance exceeds the remote-memory access penalty.
- Simultaneous Multithreading (
Modify Listing 5-1 (code/ch05/sched_sim.cpp) to add one long-running 500 ms CPU-bound job at t = 0 ahead of five 5 ms interactive jobs. Compare the average response time under First-Come, First-Served (FCFS) versus Round-Robin (RR) with a 10 ms quantum to observe the convoy effect directly.
Treating nice values or thread priorities as hard real-time guarantees. Under proportional-share schedulers (CFS / EEVDF), a low-priority background thread still receives non-zero CPU share, and priority inversion can stall a high-priority thread indefinitely unless priority inheritance is enabled on the contended mutex.