What is a Context Switch?
Learn what a context switch is — saving and restoring CPU state, the PCB, and why it costs overhead — with examples and OS interview questions.
Expected Interview Answer
A context switch is the process of saving the CPU state of a currently running task and loading the saved state of another task, so a single CPU core can appear to run many processes or threads at once.
When the scheduler decides to suspend a running process — because its time slice expired, it made a blocking call, or a higher-priority task must run — the kernel saves the program counter, registers, and stack pointer into that process’s process control block (PCB). It then loads the saved state of the next task from its own PCB and resumes execution exactly where that task left off. Context switches are pure overhead: no useful work happens during the switch itself, and switching between threads of the same process is cheaper than switching between processes because the address space and TLB entries do not need to be reloaded. Excessive context switching, caused by too many runnable tasks or overly short time slices, wastes CPU cycles and hurts throughput.
- Enables time-sharing of one CPU across many tasks
- Preserves exact execution state via the process control block
- Thread switches are cheaper than process switches
- Understanding it explains scheduler and multitasking overhead
AI Mentor Explanation
A context switch is like a bowler being taken off mid-over and a new bowler brought on: the umpire notes exactly where the over stood — balls bowled, field placements — before the change (saving state), then the new bowler’s own run-up and figures are loaded in. When the first bowler returns later, everything resumes from the noted point. Constant, needless bowling changes waste time between deliveries, just as excessive context switching wastes CPU cycles.
Step-by-Step Explanation
Step 1
Trigger
Scheduler decides to preempt — timer interrupt, higher-priority task, or a blocking system call.
Step 2
Save state
CPU registers, program counter, and stack pointer are written into the current task’s PCB.
Step 3
Schedule next task
The scheduler picks the next task to run from the ready queue.
Step 4
Restore state
The next task’s saved registers and program counter are loaded, and execution resumes exactly there.
What Interviewer Expects
- A clear definition tied to saving and restoring CPU state
- Role of the process control block (PCB)
- Why thread switches are cheaper than process switches
- Awareness that context switching is pure overhead
Common Mistakes
- Confusing a context switch with a mode switch (user to kernel)
- Thinking context switches do useful work themselves
- Not knowing thread switches skip address-space reload
- Ignoring the cost of excessive context switching (thrashing)
Best Answer (HR Friendly)
“A context switch is when the CPU pauses one task, saves exactly where it left off, and loads a different task to run instead. It is what lets one processor juggle many programs smoothly, but doing it too often wastes time on the switching itself rather than actual work.”
Code Example
struct context {
void *sp; /* stack pointer */
void *pc; /* program counter */
long regs[8];
};
void context_switch(struct context *old, struct context *new) {
save_registers(old); /* write CPU state into old->regs, old->sp, old->pc */
load_registers(new); /* load new->regs, new->sp, new->pc into the CPU */
/* execution now resumes at new->pc, as if new had never stopped */
}Follow-up Questions
- What is the difference between a process switch and a thread switch?
- What triggers a context switch besides a timer interrupt?
- How does the TLB affect the cost of a context switch?
- What is thrashing and how does it relate to excessive context switching?
MCQ Practice
1. What is saved during a context switch?
The kernel saves the full CPU state — registers, program counter, and stack pointer — into the process control block.
2. Why are thread switches generally cheaper than process switches?
Since threads of the same process share the address space, switching between them avoids reloading page tables and flushing the TLB.
3. Context switch overhead primarily hurts?
Time spent saving and restoring state is pure overhead that reduces the CPU cycles available for actual computation.
Flash Cards
What is a context switch? — Saving the CPU state of a running task and loading another task’s saved state so it can run.
Where is the saved state kept? — In the process control block (PCB) of the suspended task.
Why are thread switches cheaper? — Threads share an address space, so no page table or TLB reload is needed.
What triggers a context switch? — A timer interrupt, a blocking call, or a higher-priority task becoming ready.