Skip to main content
Definition Website Computer and Technology

Context Switch

Subject: Computer and Technology noun /ˈkɑːn.tɛkst swɪtʃ/ 1 definition

  1. Definition (noun)

    Entry #34 · Updated

    A Context Switch is the procedure of suspending an active task on a processor core, saving its execution state—such as CPU registers, program counter, and stack pointer—into memory, and loading the previously saved state of another task so it can resume seamlessly. By rapidly multiplexing processor time across competing workloads, it enables preemptive multitasking, responsive user interfaces, and fair resource sharing without dedicating a physical core to every runnable unit. Unlike a lightweight mode switch that only changes privilege levels between user space and kernel space within the same execution stream, a context switch transfers control to a different thread or process and incurs both direct state-saving overhead and indirect costs such as CPU cache and Translation Lookaside Buffer (TLB) misses.

    Common Types

    • Process Context Switch: Transfers CPU execution between separate processes, requiring the operating system kernel to save register state and swap virtual memory mappings such as page tables, which often causes TLB and cache penalties.

    • Thread Context Switch: Moves control between kernel-managed threads within the same process, updating registers, stack pointers, and thread-local state while preserving the shared virtual address space for lower overhead.

    • User-Space Context Switch: Shifts execution cooperatively between coroutines, fibers, or green threads entirely inside an application runtime, avoiding kernel traps by swapping only active stack pointers and callee-saved registers at explicit yield points.

    • Hardware Context Switch: Uses dedicated processor register banks or hardware schedulers—such as GPU warp schedulers or real-time microcontroller interrupt banks—to swap active execution states in minimal clock cycles without operating system software overhead.

    Example

    While a Linux server runs a background database backup alongside an Nginx web server, the kernel scheduler preempts the backup process when its CPU time slice expires so incoming web traffic stays responsive. The kernel saves the backup process's general-purpose registers and instruction pointer to its process control block and restores Nginx's saved registers and page-table pointer, resuming HTTP request handling within microseconds.