Browse Definitions :
Definition

context switch

What is a context switch?

A context switch is an operation that a computer's central processing unit (CPU) carries out when alternating between processes or threads while ensuring that the processes do not conflict. Effective context switching makes it possible to support a multitasking environment.

Each time the CPU makes a switch, the system temporarily suspends the currently executing task, saves its state (context) to a process control block (PCB), and then executes the next task in the queue. If that task has already been started, the CPU retrieves the state for that task so it can start the execution where it left off. These operations continue and repeat as the CPU rotates between the queued processes.

The ability of context switching to switch rapidly between tasks is a core capability of a multitasking operating system (OS) such as Windows, Linux or macOS. Context switching enables multiple processes to share a single CPU, while making it appear as though the CPU is executing multiple processes simultaneously. It is this capability that enables users to switch seamlessly between the open applications on their desktops.

How does a context switch work?

Each time the CPU switches to a different process, it saves the state data to its assigned PCB. The exact data can vary between systems, but it generally includes the pointer, program counter, registers and other information associated with the process.

A process control block is a data structure that is created for each process. In addition to state data, it also stores the process's execution state, process number and memory management information.

Context switching generally occurs as a result of one of the following triggers:

  • Multitasking operation. Context switching occurs when one process gives way to another process so the second one can be executed. In some cases, context switching takes place because the process has been allocated only a certain amount of CPU time. However, it can also occur when a process must cede the CPU to a process with a higher priority. Or a process might cede the CPU voluntarily, such as when waiting on an input/output operation.
  • Interrupt. Content switching occurs because of a system interrupt from a hardware or software component. For example, a keyboard might issue an interrupt when a user presses a key, or an application might issue an interrupt when it requires user input. An OS typically contains an interrupt handler to process and prioritize these requests.
  • User/kernel switching. In some cases, context switching can occur when the OS transitions between user mode and kernel mode. For example, a process might need to switch from user mode to kernel mode to access a system resource, which could require a context switch to accommodate the different privilege level.

When the CPU encounters one of these triggers, it kicks off a series of steps that alternate between executing processes. The following figure provides a high-level overview of how context switching might occur between two processes. The exact approach to context switching depends on the operating system and other variables. Conceptually, however, the various systems follow a similar pattern.

Diagram of how context switching works.
Context switching occurs to alternate between CPU processes and ensure that the processes do not conflict. The operation continues and repeats as the CPU rotates between queued processes.

The figure illustrates how the CPU alternates between Process 1 and Process 2, switching back and forth between executing and idle states. The following steps describe the basic sequence of events when moving between processes:

  1. The CPU executes Process 1.
  2. A triggering event occurs, such as an interrupt or system call.
  3. The system pauses Process 1 and saves its state (context) to PCB 1, the process control block that was created for that process.
  4. The system selects Process 2 from the queue and loads the process's state from PCB 2.
  5. The CPU executes Process 2, picking up from where it left off (if the process had already been running).
  6. When the next triggering event occurs, the system pauses Process 2 and saves its state to PCB 2.
  7. The Process 1 state is reloaded, and the CPU executes the process, once again picking up from where it left off. Process 2 remains in an idle state until it is called again.

The greater the number of active processes, the more complex these operations become, especially as processes are started and stopped. Even so, the CPU can still execute only one process at a time, no matter how many tasks wait in queue. Context switching also takes time to carry out because state data must be saved and retrieved for each transition. The more switching that occurs, the more time needed to alternate back and forth. If the overhead gets to be too much, system performance can degrade.

A context switch can be performed entirely in hardware, although this approach tends to be limited to older CPUs, such as those in the early x86 series. Today, context switching is software-based and typically handled in the kernel, which is faster and more efficient than hardware-based context switching. A modern system can perform hundreds of context switches per second. Although the computer appears to be performing multiple tasks in parallel, the CPU is alternating between operations at a high rate of speed.

Explore the differences between CPUs vs. microprocessors. Learn about the differences between monolithic and microkernel architectures and check out memory management techniques improve system performance.

This was last updated in February 2024

Continue Reading About context switch

Networking
  • What is wavelength?

    Wavelength is the distance between identical points, or adjacent crests, in the adjacent cycles of a waveform signal propagated ...

  • subnet (subnetwork)

    A subnet, or subnetwork, is a segmented piece of a larger network. More specifically, subnets are a logical partition of an IP ...

  • secure access service edge (SASE)

    Secure access service edge (SASE), pronounced sassy, is a cloud architecture model that bundles together network and cloud-native...

Security
  • What is exposure management?

    Exposure management is a cybersecurity approach to protecting exploitable IT assets.

  • intrusion detection system (IDS)

    An intrusion detection system monitors (IDS) network traffic for suspicious activity and sends alerts when such activity is ...

  • cyber attack

    A cyber attack is any malicious attempt to gain unauthorized access to a computer, computing system or computer network with the ...

CIO
  • What is a startup company?

    A startup company is a newly formed business with particular momentum behind it based on perceived demand for its product or ...

  • What is a CEO (chief executive officer)?

    A chief executive officer (CEO) is the highest-ranking position in an organization and responsible for implementing plans and ...

  • What is labor arbitrage?

    Labor arbitrage is the practice of searching for and then using the lowest-cost workforce to produce products or goods.

HRSoftware
  • organizational network analysis (ONA)

    Organizational network analysis (ONA) is a quantitative method for modeling and analyzing how communications, information, ...

  • HireVue

    HireVue is an enterprise video interviewing technology provider of a platform that lets recruiters and hiring managers screen ...

  • Human Resource Certification Institute (HRCI)

    Human Resource Certification Institute (HRCI) is a U.S.-based credentialing organization offering certifications to HR ...

Customer Experience
Close