AlgoViz
—
← Back to trail

Context Switch

Medium+12 XP

Watch the OS save one process's state and load another's — the swap that makes CPU-sharing possible.

No visualization loaded.

Watch

—

i

Press Run to begin.

To share one CPU among many processes, the OS keeps swapping which one runs. Each swap is a 'context switch'. Press Run to watch it save Process A and load Process B so each can resume exactly where it left off.

▸What's being 'saved' and 'loaded'?

A process's 'context' — the values the CPU was holding for it and a marker of which instruction it was on. Save that into A's storage, load B's back in, and B continues as if it never paused. It's like saving and loading a video game.

▸Why does it have to save A before running B?

Because A and B share the same CPU. If the OS just let B start without saving A's stuff, A's work would be lost — it wouldn't know where it was when its turn comes again. Saving first means A can be resumed perfectly later.

▸Is a context switch 'free'?

No — and that's the catch. During the save-and-load, NO real work gets done; it's pure overhead. So switching too often wastes the CPU on bookkeeping. The OS balances switching often enough to feel responsive, but not so often that it's all overhead.

▸How often does this happen?

Constantly — many times per second. That's how dozens of programs feel like they're running at once on one CPU: the OS switches between them faster than you can notice.

🧠A context switch = save the running process's state, load the next one's, so each resumes exactly where it left off. It's what makes CPU-sharing possible — but it's pure overhead, so the OS won't do it more than it must.
the process now getting the CPUstate safely savedthe paused process