What Is Context Switching in an Operating System?
Learn what context switching is in operating systems, how the CPU saves and restores task state, what triggers it, and why it has real performance overhead.
Expected Interview Answer
Context switching is the process by which the CPU saves the current state of a running process or thread and loads the saved state of another, so the CPU appears to run multiple tasks at once even though it executes only one at a time.
When the scheduler decides to stop the currently running task, whether because its time slice expired, it made a blocking call, or a higher-priority task arrived, the OS saves that task's registers, program counter, and stack pointer into its process control block. It then loads the saved context of the next task to run and resumes it exactly where it left off. This mechanism is what enables multitasking on a single CPU core, but it is not free: every switch consumes CPU cycles for saving and restoring state, and it can also invalidate CPU caches and the TLB, slowing down the next task's initial execution.
- Enables multitasking on a single CPU core
- Lets the OS enforce fairness and priority between tasks
- Keeps interactive tasks responsive despite background work
- Allows the CPU to run other work while a task waits on I/O
- Preserves exact execution state so a task resumes correctly
AI Mentor Explanation
Context switching is like an umpire pausing the over when rain interrupts play, carefully noting the exact ball, score, and field placements before covers go on. When another match resumes on the same ground later, everything is reset from those notes so play continues as if no interruption happened, though re-setting the field and re-reading the pitch does cost a few minutes.
Step-by-Step Explanation
Step 1
Trigger occurs
A timer interrupt, I/O block, or higher-priority task arrival tells the scheduler to switch tasks.
Step 2
Save current context
The OS stores the running task's registers, program counter, and stack pointer into its process control block.
Step 3
Scheduler picks next task
The scheduling algorithm selects which ready task should run next.
Step 4
Load new context
The OS restores the chosen task's saved registers and program counter from its control block.
Step 5
Resume execution
The CPU continues the new task exactly where it last left off, unaware of the interruption.
What Interviewer Expects
- Explains what state gets saved and restored
- Mentions the process control block
- Understands context switching has real CPU overhead
- Knows what triggers a switch (interrupt, I/O, priority)
- Can mention cache/TLB effects that add indirect cost
Common Mistakes
- Saying context switching is free or instantaneous
- Confusing it with a full process termination
- Forgetting the program counter must be saved and restored
- Not connecting it to scheduling and multitasking
Best Answer (HR Friendly)
“Context switching is how a computer pauses one running task, remembers exactly where it left off, and then starts or resumes a different task, so it looks like several things are happening at once even on a single processor. It happens constantly and lets a computer stay responsive, though switching too often wastes some processing time.”
Code Example
# Simplified process control block
PCB = {
"pid": 42,
"program_counter": 0x4010,
"registers": {"eax": 12, "ebx": 7, "esp": 0x7ffee20},
"state": "ready",
}
# On switch:
# 1. save current CPU registers/PC into running task's PCB
# 2. mark it 'ready' or 'waiting'
# 3. load next task's saved registers/PC from its PCB
# 4. set its state to 'running' and jump to its program_counter
Follow-up Questions
- What is stored in a process control block?
- How does a context switch differ between processes and threads?
- What causes context switching overhead beyond saving registers?
- How does the scheduler decide which task runs next?
- What is a voluntary versus involuntary context switch?
MCQ Practice
1. What is saved during a context switch?
The OS saves the current task's CPU registers, program counter, and stack pointer so it can resume exactly where it stopped.
2. Where is a task's saved state typically stored?
The process control block holds the saved execution state used to restore a task when it is scheduled again.
3. Which of these can trigger a context switch?
A timer interrupt signaling the end of a time slice is a classic trigger for the scheduler to perform a context switch.
Flash Cards
What is context switching? — Saving a running task's state and loading another task's saved state so the CPU can switch between them.
Where is a task's state saved during a switch? — In its process control block (PCB).
Is context switching free? — No — it consumes CPU cycles and can cause cache/TLB misses, adding real overhead.
Name one trigger for a context switch. — A timer interrupt ending the current task's time slice.