$linuxjunkies
>

context switch

also: context switching, process switch, task switch

The process by which a CPU switches from executing one process to another, saving the current process's state and restoring the next process's state.

A context switch occurs when the operating system pauses a running process, saves its execution context (registers, memory pointers, program counter), and loads the context of a different process to run instead. This enables multitasking by allowing many processes to share a single CPU.

The CPU's context includes all the information needed to resume a process exactly where it left off: the instruction pointer, stack pointers, CPU registers, and memory management information. The kernel typically triggers a context switch through a timer interrupt (time slice exhausted), an I/O wait (process needs disk data), or a system call.

While context switches are essential for multitasking, they have a cost: saving and restoring state consumes CPU cycles. You can monitor context switches on Linux using vmstat or top. For example, vmstat 1 shows columns for voluntary and involuntary context switches per second.

Related terms