Operating System Interview Questions · Question 08

What is a context switch, and why is it considered overhead?

Interview preparation resource from Gate Smashers.

Interview-ready answer

A context switch changes the CPU from one task to another by saving the outgoing execution state and restoring the incoming state. It is overhead because the switch itself completes no application work.

Operating System Interview Questions diagram explaining What is a context switch, and why is it considered overhead
Understand it clearly

The OS saves registers, program counter and scheduling state for the outgoing task, chooses another runnable task and restores its saved context. Depending on the switch, address-space and privilege-related state may also change.

The direct save-and-restore cost is only part of the overhead. The incoming task may suffer cold CPU caches, branch-predictor disruption and TLB misses. Excessive switching therefore reduces useful throughput even when each individual switch appears short.

The cost comes from saving and restoring registers, changing memory mappings and disturbing CPU caches or translation buffers. A thread switch within one process is often cheaper because the address space may remain unchanged, although it still has scheduling overhead.