What is context switching in an operating system?
Context switching is when the OS pauses the process or thread running on a CPU, saves its state (registers, program counter, stack pointer) in its control block, and loads the saved state of another one so it can continue. It lets many programs share one CPU, but each switch is pure overhead.
Explain it simply
Think of a doctor in a busy clinic. A patient is halfway through describing their symptoms when an emergency arrives. Before leaving, the doctor writes on the patient's file exactly where they stopped: "fever 3 days, checked BP, next ask about diet". Later, the doctor picks up the file and continues from that line as if nothing happened.
A CPU core can run only one thing at a time. To make dozens of programs appear to run together, the OS keeps switching between them. Before switching, it saves where the current one was. That save-and-restore is the context switch, and the patient file is the Process Control Block (PCB).
Example
You are compiling code and playing music on a single-core laptop.
time ->
CPU: | compiler | save/load | music player | save/load | compiler | ...
(switch) (switch)- The compiler is running. A timer interrupt fires because its time slice is over.
- The CPU jumps into the kernel. The kernel saves the compiler's registers, program counter and stack pointer into the compiler's PCB, and marks it "ready".
- The scheduler picks the music player.
- The kernel loads the music player's saved registers from its PCB, switches the memory mapping to its address space, and returns to user mode.
- The music player continues from the exact instruction where it stopped last time.
Each switch takes a few microseconds, so with hundreds per second you hear smooth music and the build keeps going.
How it works
The context is everything needed to resume a program: CPU registers, program counter, stack pointer, flags, and a pointer to its memory map. The PCB also stores the process ID, state, priority, open files and scheduling information.
A context switch can be triggered by:
- A timer interrupt in preemptive scheduling, when the time slice ends.
- A blocking system call, for example waiting for disk, network or keyboard input.
- A higher-priority process becoming ready.
- A process exiting or voluntarily giving up the CPU.
Switching between threads of the same process is cheaper than between processes, because they share the same address space. The OS does not have to change the page table, and the CPU's address translation caches (TLB) stay useful.
Note that a switch from user mode into the kernel for a system call (a mode switch) is not by itself a context switch. A context switch happens only when the kernel decides to run a different process or thread.
Real-world use
- Multitasking on every phone, laptop and server depends on it.
- In a Round Robin scheduler, the time slice is chosen to be much longer than the switch cost, otherwise the CPU spends most of its time switching.
- High-performance servers like Nginx and Node.js use a small number of threads with non-blocking I/O, partly to avoid the cost of thousands of context switches.
- On Linux,
vmstat 1shows context switches per second in thecscolumn, andpidstat -wshows them per process.
Common mistakes
- Thinking context switching does useful work. It is overhead; the CPU runs no user program during it.
- Forgetting the indirect cost: after a switch, the new process finds cold CPU caches and TLB, so it runs slower for a while.
- Confusing a mode switch (user to kernel) with a context switch.
What to say in the interview
"A context switch is the OS saving the state of the currently running process or thread, its registers, program counter and stack pointer, into its PCB, and restoring the saved state of another so it can resume. It is triggered by timer interrupts, blocking I/O, or a higher-priority task becoming ready. It is pure overhead, both directly and because caches and the TLB lose their warm data. Thread switches within a process are cheaper than process switches because the address space does not change."
Follow-up questions
- What is stored in a Process Control Block?
- Why is a thread switch cheaper than a process switch?
- What is the difference between a mode switch and a context switch?
- How does the time quantum in Round Robin relate to context switch overhead?