Context Switching in Operating System

About Context Switching in Operating System

When we talk about multitasking in operating systems, the first term that comes into play is Context Switching. It’s the backbone of how an OS efficiently handles multiple processes simultaneously without slowing down or crashing.

On this page, we will learn the concepts of Context switching in Operating System. Context switching refers to the methodology of switching a task or process from one state to another using CPU and other resources. It helps to complete the execution of all the tasks. It explains the properties of a multitasking operating system in which multiple processes shared the same CPU to perform multiple tasks

context switching in operating system

Context Switch in OS

Context Switching is a cost and time saving measure performed by the CPU that is handing task 1 and has to stop executing this task to priority execute another task 2.

  • To do this effectively, the system stores the initial task in its processed form so that when this task is resumed, it can be loaded and resumed from the same progress point as earlier.
  • This allows the computer to multitask and multiple processes can share the same CPU. This is also done to make sure that tasks do conflict one another, as they may be holding a same resource for execution like memory etc.
Context Switching in OS

Steps Leading to Context Switching

In the above diagram the following happens –

  1. Process 1 running in the system
  2. Interrupt or system call appears
  3. Causes Process 1 to be saved into PCB 1 and switched out
  4. Process 2 is loaded from PCB 2 and executed
  5. Again interrupt or system call appears
  6. Process 2 is saved into PCB 2 and process 1 loaded from PCB 1
  7. Process 1 is executed

Triggers for Context Switching

  1. Multitasking
  2. Interrupt Handling
  3. User and Kernel mode Switch – System may switch from user mode to the kernel mode, you can think of this as windows 10 running and system is switched to boot mode.

How Context Switching Works: Step-by-Step

Let’s break it down into a sequence of steps:

  • Interrupt Occurs: The currently running process is interrupted. This could be due to a timer interrupt, I/O request, or a higher-priority task needing CPU time.
  • Save Current Process State: The CPU saves the current state (registers, program counter, stack pointer, etc.) of the running process in its PCB (Process Control Block).
  • Update PCB: The OS updates the PCB with the saved values to preserve the context.
  • Choose Next Process: The scheduler selects the next process from the ready queue based on a scheduling algorithm like Round Robin, Priority Scheduling, etc.
  • Load New Process Context: The CPU loads the context of the new process from its PCB.
  • Transfer Control: The CPU starts executing the new process from where it left off.

This entire cycle happens so fast (in microseconds) that users don’t even notice it.

What is Saved During a Context Switch?

During a context switch, the following information is typically saved:

  • Program Counter: Points to the next instruction in the process.
  • Stack Pointer: Points to the top of the process’s stack in memory.
  • General Purpose Registers: Store intermediate data.
  • CPU Registers: Including status registers and index registers.
  • Memory Management Info: Like page tables, segment registers.
  • Process State Info: The status (Running, Ready, Waiting) of the process.

Context Switching in Different Scenarios

  • Between Processes
    The most common type of context switching. The entire process context, including memory mapping, must be saved and restored.
  • Between Threads (Thread Switching)
    If threads belong to the same process, only CPU registers and stack pointers need to be saved. This makes it much faster than process-level context switching.
  • Between User Mode and Kernel Mode
    When a system call is made, or an interrupt occurs, the context must switch from user mode to kernel mode and vice versa.

Types of Context Switches

Process Context Switch

  • Occurs when the CPU switches from one process to another.
  • Heavier because it requires saving/restoring more data.

Thread Context Switch

  • Faster and lighter as threads share the same memory space.
  • Requires saving/restoring only CPU registers and the program counter.

Mode Switch

  • Not exactly a context switch but involves switching from user mode to kernel mode.
  • Doesn’t switch between processes but changes access levels.

Scheduling Algorithms That Trigger Context Switching

Context switches are triggered by the CPU scheduler. Some algorithms that influence how often switching occurs include:

  • Round Robin: Switches processes after a fixed time slice.
  • Priority Scheduling: Switches to higher-priority processes.
  • Multilevel Queue: Switches between queues with different priority levels.
  • Shortest Job First (SJF): Switches to the shortest available job.

The frequency and efficiency of context switches directly impact overall system performance.

Advantages of Context Switching

  • Multitasking Support
    Enables the CPU to handle multiple tasks effectively.
  • Better CPU Utilization
    Prevents the CPU from sitting idle when a process waits for I/O.
  • Fair Resource Distribution
    Allocates CPU time slices equally among processes or based on priority.
  • Improved System Responsiveness
    Crucial for real-time systems and interactive applications.

Context Switching Time

Context Switching Time is the time taken by the CPU to switch from one process/thread to another. It includes:

  • Time to save the old process context
  • Time to load the new process context

This time should be as small as possible because it’s a pure overhead—no useful work is done during a switch. On modern systems, it’s typically in the range of a few microseconds.

FAQs

No, it’s considered overhead. It doesn’t contribute to actual process execution.

Not entirely. But we can minimize its frequency using efficient algorithms and better thread management.

Context switching is primarily handled at the OS level, written in low-level languages like C and Assembly.

Yes, especially in real-time operating systems (RTOS) where multitasking is required.