# Context Switching

> A context switch is the act of saving the running process's execution state (its program counter and CPU registers) into its Process Control Block and loading another process's saved state, so the CPU can pause one task and resume another correctly. It creates the illusion that many processes run at once, but every switch adds real overhead.

Canonical: https://devsunite.com/resources/core-subjects/operating-systems/context-switching
Last updated: 2026-11-22

Subject: Operating Systems · Category: Process Management · 4 min read

A single CPU core can only truly execute one process's instructions at any given instant. The illusion that many processes run "at the same time" comes from the OS rapidly switching which process's instructions the CPU is actually executing, and each one of those switches is called a context switch.

## What actually happens during a context switch

1. **The currently running process is interrupted**, either because its time slice expired, a higher-priority process became ready, or it voluntarily blocked waiting on I/O.
2. **The OS saves the current process's context**: its program counter and all CPU register values, into that process's Process Control Block (PCB, see the dedicated article on that structure).
3. **The scheduler picks the next process to run**, according to whichever scheduling algorithm the OS is using (FCFS, Round Robin, priority-based, and so on).
4. **The OS loads the next process's saved context** from its PCB back into the actual CPU registers.
5. **The CPU resumes executing that next process**, picking up exactly at the instruction its program counter points to, with no awareness that any time passed since it last ran.

## Why context switches have real overhead

None of the five steps above do any useful application work, they're pure bookkeeping. During a context switch, the CPU also typically loses whatever data it had cached (in fast per-core caches like the CPU's L1/L2 cache) for the outgoing process, since the incoming process's data isn't in that cache yet, that cached data has to be reloaded from slower memory as the new process runs, which is a real, measurable performance cost beyond just the switch itself. This is exactly why an OS with too many processes competing for too few CPU cores, or a badly tuned scheduler causing excessive switching, can spend a surprising fraction of total CPU time on switching overhead rather than actual application work.

## A concrete example

Say Process A is running and its scheduling time slice (a fixed slot of CPU time, common in Round Robin scheduling) expires. The OS saves A's program counter and registers into A's PCB, the scheduler picks Process B from the ready queue, loads B's previously saved register values from B's PCB into the CPU, and B resumes running exactly where it left off last time. All five steps happen in a tiny fraction of a second, but they still happen every single time the CPU switches which process it's serving, potentially hundreds or thousands of times per second on a busy system.

## Where this matters in real systems

Real operating systems specifically try to minimize unnecessary context switching, because it's pure overhead with no application benefit: a scheduler tuned poorly (switching far more often than actually needed) measurably reduces the total useful work a system can do, since more time goes to saving/restoring state and less to actually running application code. This is one real reason threads are often preferred over separate processes for tightly cooperating tasks: switching between threads in the same process is generally cheaper than switching between fully separate processes, since threads already share the same memory space, so less needs to be swapped out.

## What interviewers actually ask about context switching

- **"What is a context switch?"** Saving the currently running process's complete execution state (into its PCB) and loading a different process's previously saved state, so the CPU can pause one process and resume another with no loss of correctness.
- **"Why does context switching have overhead if it's 'just bookkeeping'?"** Because none of the steps do useful application work, and switching also typically evicts the outgoing process's data from fast CPU caches, which then has to be reloaded from slower memory as the new process runs.
- **"What triggers a context switch?"** A process's time slice expiring, a higher-priority process becoming ready, or the running process voluntarily blocking on I/O.
- **"Why is switching between threads generally cheaper than switching between processes?"** Threads in the same process already share memory, so less state needs to be saved and reloaded compared to switching between fully separate, isolated processes.

## Key takeaways

- A context switch saves the outgoing process's execution state and loads the incoming process's saved state.
- It's pure overhead, no application work happens during the switch itself, and it also typically costs CPU cache data that has to be reloaded afterward.
- Excessive or poorly tuned context switching measurably reduces useful CPU throughput.
- Switching between threads is generally cheaper than switching between processes, since threads already share memory.

## Related

- [Previous: Process Control Block (PCB)](https://devsunite.com/resources/core-subjects/operating-systems/process-control-block-pcb)
- [Next: CPU-Bound vs I/O-Bound Processes](https://devsunite.com/resources/core-subjects/operating-systems/cpu-bound-vs-io-bound-process)
- [More in Process Management: Processes in an Operating System](https://devsunite.com/resources/core-subjects/operating-systems/processes-in-os)
- [More in Process Management: Process vs Program vs Thread](https://devsunite.com/resources/core-subjects/operating-systems/process-vs-program-vs-threads)
- [More in Process Management: IPC and Pipes](https://devsunite.com/resources/core-subjects/operating-systems/ipc-and-pipes)
- [More in Process Management: Zombie vs Orphan Process](https://devsunite.com/resources/core-subjects/operating-systems/zombie-vs-orphan-process)
- [More in Process Management: Important Terms Related to Processes](https://devsunite.com/resources/core-subjects/operating-systems/important-terms-related-to-process)
- [All 205 OS interview questions](https://devsunite.com/resources/core-subjects/operating-systems)
