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
  • firewall as a service (FWaaS)

    Firewall as a service (FWaaS), also known as a cloud firewall, is a service that provides cloud-based network traffic analysis ...

  • private 5G

    Private 5G is a wireless network technology that delivers 5G cellular connectivity for private network use cases.

  • NFVi (network functions virtualization infrastructure)

    NFVi (network functions virtualization infrastructure) encompasses all of the networking hardware and software needed to support ...

Security
  • virus (computer virus)

    A computer virus is a type of malware that attaches itself to a program or file. A virus can replicate and spread across an ...

  • Certified Information Security Manager (CISM)

    Certified Information Security Manager (CISM) is an advanced certification that indicates that an individual possesses the ...

  • cryptography

    Cryptography is a method of protecting information and communications using codes, so that only those for whom the information is...

CIO
  • B2B (business to business)

    B2B (business-to-business) is a type of commerce involving the exchange of products, services or information between businesses, ...

  • return on investment (ROI)

    Return on investment (ROI) is a crucial financial metric investors and businesses use to evaluate an investment's efficiency or ...

  • big data as a service (BDaaS)

    Big data as a service (BDaS) is the delivery of data platforms and tools by a cloud provider to help organizations process, ...

HRSoftware
  • talent acquisition

    Talent acquisition is the strategic process an organization uses to identify, recruit and hire the people it needs to achieve its...

  • human capital management (HCM)

    Human capital management (HCM) is a comprehensive set of practices and tools used for recruiting, managing and developing ...

  • Betterworks

    Betterworks is performance management software that helps workforces and organizations to improve manager effectiveness and ...

Customer Experience
  • martech (marketing technology)

    Martech (marketing technology) refers to the integration of software tools, platforms, and applications designed to streamline ...

  • transactional marketing

    Transactional marketing is a business strategy that focuses on single, point-of-sale transactions.

  • customer profiling

    Customer profiling is the detailed and systematic process of constructing a clear portrait of a company's ideal customer by ...

Close