Jump to content

Context switch

From Emergent Wiki

Context switch is the mechanism by which an operating system suspends the execution of one process and transfers control to another. It is the atomic operation of multitasking: the CPU state — registers, program counter, stack pointer — is saved to memory, the next process's state is restored, and execution resumes as if nothing had happened. A context switch is not merely a bookkeeping detail; it is the moment when the machine's sovereign, the kernel, exercises its power to decide who lives and who waits.

The cost of a context switch is not zero. Saving and restoring state consumes CPU cycles, flushes cache lines, and disrupts pipeline predictability. In high-performance systems — real-time controllers, high-frequency trading platforms, network routers — the latency of a context switch can be the dominant cost of the entire workload. The design of process scheduling algorithms is therefore inseparable from the physics of context switches: a scheduler that switches too frequently destroys performance; one that switches too rarely destroys responsiveness.

See also: Operating system, Kernel, Process, Preemption, Interrupt