A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Context Switching and CPU Dispatch

College Depth 98 in the knowledge graph ☐ I know this ☆ Set as goal
34topics build on this
362prerequisites beneath it
See this on the map →
Process States and State TransitionsProcess Model Formalization→→CPU Scheduling FundamentalsProcessor Affinity and CPU Binding+1 more
scheduling performance cpu-management

Core Idea

Context switching is the OS mechanism to pause one process and resume another. The OS saves registers, memory management state, and other CPU context to the process control block, loads another process's context, and branches to its instruction pointer. Context switching overhead is critical to OS performance and responsiveness.

How It's Best Learned

Instrument a kernel or OS simulator to trace context switches and measure overhead such as cache misses and TLB flushes.

Explainer

From your study of process states and transitions, you know that a process can be in states like running, ready, or blocked, and that the OS moves processes between these states. Context switching is the mechanism that makes those transitions physically happen on the CPU. When the OS decides that process A should stop running and process B should start, it must perform a context switch — saving everything about A's execution state and loading everything about B's.

The "context" in a context switch is everything the CPU needs to resume a process exactly where it left off. This includes all general-purpose registers (the values the process was computing with), the program counter (which instruction it was about to execute), the stack pointer (where its call stack is), and processor status flags (like whether the last comparison was equal or the carry flag is set). It also includes memory management state: the page table base register that tells the CPU which virtual-to-physical address mappings to use. All of this is saved to a data structure called the process control block (PCB), which the OS maintains for every process. When switching to process B, the OS loads B's PCB values into the CPU registers and jumps to B's program counter. From B's perspective, it never stopped running.

The dispatcher is the OS component that performs the actual context switch. When the scheduler decides which process should run next, the dispatcher does the low-level work: it saves the current context, restores the new process's context, switches to the new process's address space (updating the page table register), and transfers control to the new process. The time this takes — the dispatch latency — is pure overhead during which no useful work is done. On modern hardware, the register save/restore itself is fast (microseconds), but the indirect costs are significant: switching address spaces invalidates the translation lookaside buffer (TLB), forcing expensive page table lookups on subsequent memory accesses, and the new process's data is unlikely to be in the CPU cache, causing cache misses that stall the pipeline.

This overhead is why context switch frequency matters for system design. An OS that switches too aggressively (very short time slices) spends a disproportionate amount of time on overhead rather than useful computation. An OS that switches too rarely keeps processes waiting and hurts responsiveness. The scheduler's time quantum — which you will encounter in scheduling algorithms — represents this tradeoff directly. Context switching also explains why threads within the same process are cheaper to switch between than separate processes: threads share an address space, so the TLB does not need to be flushed and much of the cache remains valid.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Process Model Formalization → Context Switching and CPU Dispatch

Longest path: 99 steps · 362 total prerequisite topics

Prerequisites (2)

Leads To (3)