Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Operating Systems
Operating Systemsmedium

What is context switching and why is it expensive?

Tags
#context-switch#scheduler#performance
Back to categoryPractice quiz

Answer

Context switching is when the CPU switches from one thread/process to another. It’s expensive because the OS must save/restore registers, switch memory mappings, and flush caches/TLBs.

Advanced answer

Deep dive

Switching work adds overhead and disrupts locality:

  • Save/restore registers and stack pointers.
  • TLB and cache effects reduce CPU efficiency.
  • Scheduler decisions add latency under load.
  • Frequent switches hurt throughput for CPU-bound tasks.

Examples

High thread count scenario:

1000 runnable threads -> frequent switches -> lower cache hit rate -> slower throughput

Common pitfalls

  • Creating too many threads for CPU-bound work.
  • Using blocking I/O without thread pools.
  • Confusing context switches with process forks (different costs).

Interview follow-ups

  • How do you measure context switch overhead?
  • When do you prefer async I/O over threads?
  • How do caches and TLBs affect performance?

Related questions

Operating Systems
Explain virtual memory and paging.
#virtual-memory#paging#performance
JavaScript
What are debounce and throttle and when would you use each?
#performance#debounce#throttle
MongoDB
Text indexes: when would you use them and what’s a limitation?
#mongo#text-index
#search
MongoDB
`$lookup`: what does it do and what is a common pitfall?
#mongo#lookup#aggregation
PostgreSQL
Materialized view vs view: what’s the difference?
#postgres#views#materialized-view