encyclopegia.org

Context switch

In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes to share a single central processing unit (CPU) and is an essential feature of a multiprogramming or multitasking operating system. In a traditional CPU, each process a program in execution uses the various CPU registers to store data and hold the current state of the running process. However, in a multitasking operating system, the operating system switches between processes or threads to allow the execution of multiple processes simultaneously. For every switch, the operating system must save the state of the currently running process, followed by loading the next process state, which will run on the CPU. This sequence of operations that stores the state of the running process and loads the following running process is called a context switch. The precise meaning of the phrase "context switch" varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an interrupt, such as when a task needs to access disk storage, freeing up CPU time for other tasks.…

In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes to share a single central processing unit (CPU) and is an essential feature of a multiprogramming or multitasking operating system. In a traditional CPU, each process a program in execution uses the various CPU registers to store data and hold the current state of the running process. However, in a multitasking operating system, the operating system switches between processes or threads to allow the execution of multiple processes simultaneously. For every switch, the operating system must save the state of the currently running process, followed by loading the next process state, which will run on the CPU. This sequence of operations that stores the state of the running process and loads the following running process is called a context switch. The precise meaning of the phrase "context switch" varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an interrupt, such as when a task needs to access disk storage, freeing up CPU time for other tasks. Some operating systems also require a context switch to move between user mode and kernel mode tasks. The process of context switching can have a negative impact on system performance. == Switching cases == There are three potential triggers for a context switch: ===No context switch needed=== Considering a general arithmetic addition operation The instruction is stored in the instruction register, and the program counter is incremented. A and B are read from memory and are stored in registers R1, R2 respectively. In this case, is calculated and written in R1 as the final answer. This operation only requires sequential reads and writes, and there're no waits for function calls used, hence no context switch/wait takes place in this case. ===Context switch caused by interrupt=== Suppose a process A is running and a timer interrupt occurs. The user registers — program counter, stack pointer, and status register — of process A are then implicitly saved by the CPU onto the kernel stack of A. Then, the hardware switches to kernel mode and jumps into interrupt handler for the operating system to take over. Then the operating system calls the switch() routine to first save the general-purpose user registers of A onto A's kernel stack, then it saves A's current kernel register values into the PCB of A, restores kernel registers from the PCB of process B, and switches context, that is, changes kernel stack pointer to point to the kernel stack of process B. The operating system then returns from interrupt. The hardware then loads user registers from B's kernel stack, switches to user mode, and starts running process B from B's program counter. == Performance == Context switching itself has a cost in performance, due to running the task scheduler, TLB flushes, and indirectly due to sharing the CPU cache between multiple tasks. Switching between threads of a single process can be faster than between two separate processes because threads share the same virtual memory maps, so a TLB flush is not necessary. The time to switch between two separate processes is called the process switching latency. The time to switch between two threads of the same process is called the thread switching latency. The time from when a hardware interrupt is generated to when the interrupt is serviced is called the interrupt latency. Switching between two processes in a single address space operating system can be faster than switching between two processes in an operating system with private per-process address spaces. OS Context Switching - Computerphile

References

No reference rows published for this article.

Cite this article

MLA

"Context switch." encyclopegia.org, 2026, https://encyclopegia.org/wiki/context-switch. Accessed 24 November 2026.

Chicago

"Context switch." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/context-switch.

APA

encyclopegia.org. (2026). Context switch. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/context-switch

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

hydrocortisone valerate

stub · references available

Anarchism

enwiki · references available

Aristotle

enwiki · references available

Academy Award for Best Production Design

enwiki · references available

Academy Awards

enwiki · references available

Actrius

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1375310774 · 0.5 · snapshot enwiki-001375310774

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.