Skip to content
We areTOP 1%

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

MediumConceptProcesses & Threads
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

A context switch happens when the OS stops the running process or thread, saves its state (program counter, registers and stack pointer) into its PCB, and loads the saved state of another one so the CPU continues that one instead. It is triggered by a timer interrupt, a blocking system call such as a disk read, or a higher-priority process becoming ready.

  • It is pure overhead: no useful user work is done while state is saved and restored, typically a few microseconds per switch.
  • A switch between processes also changes the address space. That may flush the TLB (the cache of address translations) and leaves the CPU caches "cold", so the new process runs slowly at first.
  • A switch between threads of the same process is cheaper, because the memory mappings stay the same.

Example: assume each switch costs 0.1 ms. With a 10 ms time slice, the overhead is 0.1 / (10 + 0.1) ≈ 1% of CPU time. With a 1 ms slice, it becomes 0.1 / (1 + 0.1) ≈ 9%. This is why the time quantum in Round Robin must be much larger than the switch time. Analogy: placing a bookmark in one book before picking up another, then finding your place again later.

More in Processes & Threads

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account