1. Introduction
Context switching is one of the most important mechanisms in modern operating systems because it enables multitasking and concurrent execution. It allows the CPU to stop executing one process and begin executing another while preserving the state of both processes.
Since most systems execute multiple processes simultaneously, the operating system must continuously decide:
Which process should run
When a process should stop
Which process should execute next
Context switching provides the mechanism that makes these transitions possible.
On a single-core processor, only one process can execute at any instant. However, by rapidly switching between processes, the operating system creates the illusion that multiple programs are running simultaneously.
This capability forms the foundation of:
Multitasking
Time-sharing systems
Concurrent execution
Interactive computing
Although context switching improves responsiveness and CPU utilization, it also introduces overhead because the CPU must temporarily stop useful computation while switching execution.
For this reason, operating systems attempt to balance:
Responsiveness
Fairness
Performance
when performing context switches.
2. Definition
A Context Switch is defined as:
The process of saving the execution state (context) of a currently running process and restoring the state of another process so that execution can continue.
In simpler terms:
Process A is interrupted
Its current state is saved
Process B's previously saved state is restored
CPU begins executing Process B
Later, Process A can resume exactly where it stopped.
Without context switching:
Multitasking would be impossible
Only one process could execute until completion
Context switching therefore enables multiple processes to share the CPU efficiently.
3. What is "Context"?
The term context refers to all information required to pause a process and later resume it without losing its execution state.
A process can only continue correctly if the operating system preserves its complete execution environment.
The context typically includes:
Program Counter (PC)
CPU Registers
Stack Pointer
Process State
Memory Management Information
Most of this information is stored in the:
Process Control Block (PCB)
Program Counter (PC)
Stores:
Address of the next instruction to execute
Example:
Instruction 100
Instruction 101 ← PC
Instruction 102
When execution resumes:
CPU continues from Instruction 101
CPU Registers
Registers contain:
Intermediate calculations
Temporary values
Function parameters
Example:
R1 = 25
R2 = 100
These values must be preserved.
Stack Pointer (SP)
Points to:
Current top of the process stack
Required for:
Function calls
Local variables
Return addresses
Process State
Stores:
Ready
Running
Waiting
Terminated
state information.
Memory Management Information
Includes:
Page table pointers
Address space mappings
Virtual memory information
This information ensures the process can access the correct memory after resuming.
4. Why Context Switching is Needed
Modern operating systems rarely execute only one process.
Context switching enables efficient sharing of CPU resources among multiple processes.
Multitasking
Multiple applications can execute seemingly simultaneously.
Example:
Browser
Editor
Music Player
Terminal
All share CPU resources through context switching.
Concurrency
Processes make progress during overlapping time periods.
Even on a single CPU:
P1 → CPU
P2 → CPU
P3 → CPU
Rapid switching creates concurrent execution.
Responsiveness
Interactive systems require quick response times.
Example:
While:
Browser downloads a file
User can still:
Move mouse
Type text
Switch windows
This is possible because of context switching.
Resource Utilization
When one process waits for I/O:
Process A → Waiting
CPU → Process B
The CPU remains productive instead of idle.
5. When Does Context Switching Occur?
Context switches occur whenever the operating system decides that another process should execute.
5.1 Preemptive Scheduling
Most modern operating systems use:
Preemptive Scheduling
Each process receives a fixed time slice called a:
Time Quantum
When the quantum expires:
Running → Ready
The scheduler may select another process.
This is one of the most common causes of context switching.
5.2 I/O Request
A process may request:
Disk access
Network communication
Keyboard input
While waiting:
Running → Waiting
The CPU switches to another ready process.
Example:
Process A → Disk Read
CPU → Process B
5.3 Interrupts
Hardware devices generate interrupts.
Examples:
Keyboard input
Timer signals
Disk completion
The operating system may decide to perform a context switch while handling the interrupt.
5.4 System Calls
System calls transfer execution into kernel mode.
Examples:
read()
write()
fork()
During system call processing, the scheduler may decide to switch processes.
6. Context Switching Mechanism
Context switching involves both the operating system and the CPU scheduler.
6.1 Step-by-Step Process
Step 1: Interrupt or System Call Occurs
A scheduling event triggers the switch.
Examples:
Timer interrupt
I/O request
System call
Running Process
|
Interrupt/System Call
Step 2: CPU Switches to Kernel Mode
The operating system gains control.
User Mode
↓
Kernel Mode
This allows privileged operations.
Step 3: Save Current Process State
The operating system saves:
Program Counter
Registers
Stack Pointer
Process State
into the process's PCB.
CPU State
↓
PCB(Process A)
This preserves the process context.
Step 4: Scheduler Selects Next Process
The scheduler examines the Ready Queue.
Ready Queue
P1
P2
P3
A process is selected.
Example:
Selected Process = P2
Step 5: Load New Process State
The operating system loads:
Program Counter
Registers
Memory information
from PCB(P2).
PCB(P2)
↓
CPU
Step 6: CPU Switches Back to User Mode
Kernel completes switching.
Kernel Mode
↓
User Mode
Step 7: Execution Resumes
Process P2 continues execution exactly where it previously stopped.
Resume Execution
7. Role of Scheduler in Context Switching
The scheduler decides:
Which process runs next
When CPU allocation changes
Which process receives CPU time
The scheduler is therefore responsible for:
Scheduling Decisions
Examples:
Ready Queue
↓
Scheduler
↓
Selected Process
The scheduler does not perform the switch itself.
Dispatcher
The actual context switch is performed by:
Dispatcher
Responsibilities include:
Saving state
Loading state
Switching execution
Thus:
Scheduler
Decides who runs
Dispatcher
Performs the actual switch
8. Context Switch vs Process Switch vs Mode Switch
These terms are frequently confused.
8.1 Context Switch
A context switch involves:
Saving one process state
Restoring another process state
Example:
P1 → P2
Requires:
Full save
Full restore
8.2 Process Switch
Often used interchangeably with:
Context Switch
In most operating system discussions:
Process switch = Context switch
8.3 Mode Switch
A mode switch changes execution privilege level.
Example:
User Mode
↓
Kernel Mode
This occurs during:
System calls
Interrupts
Exceptions
Important Difference
A mode switch:
Does NOT necessarily change processes
Example:
Process A
User Mode
↓
Kernel Mode
↓
User Mode
Process A
Same process continues execution.
Thus:
| Operation | Process Change? |
|---|---|
| Context Switch | Yes |
| Process Switch | Yes |
| Mode Switch | Not necessarily |
9. Overhead of Context Switching
Context switching is useful but not free.
During a context switch:
CPU performs administrative work
No useful application computation occurs
This is known as:
Context Switching Overhead
9.1 Types of Overhead
9.1.1 Time Overhead
CPU must:
Save registers
Save PC
Save stack information
Load new state
Example:
Save State
Load State
Resume
All require CPU cycles.
9.1.2 Cache Effects
CPU caches contain process-specific data.
After switching:
Cache → Less Useful
New process may require:
Different instructions
Different data
This reduces cache efficiency.
9.1.3 TLB Impact
The TLB (Translation Lookaside Buffer) stores:
Virtual-to-physical address mappings
After a process switch:
Old Mapping Invalid
The TLB may need to be:
Flushed
Reloaded
This increases memory access overhead.
10. Performance Implications
Context switching introduces a trade-off.
Too Many Context Switches
Results in:
High overhead
Reduced throughput
Poor CPU efficiency
Example:
Switch
Switch
Switch
Switch
CPU spends excessive time switching.
Too Few Context Switches
Results in:
Poor responsiveness
Long waiting times
Interactive users may experience delays.
Operating System Goal
Balance:
Performance
Fairness
Responsiveness
This balance is achieved through scheduling algorithms.
11. Context Switching in Multi-Core Systems
Modern processors contain multiple CPU cores.
Example:
Core 1 → P1
Core 2 → P2
Core 3 → P3
Core 4 → P4
Each core executes independently.
Context Switching Still Exists
Each core maintains:
Its own running process
Its own scheduler decisions
Thus:
Core 1:
P1 → P5
Core 2:
P2 → P6
Context switching still occurs separately on each core.
Load Balancing
The operating system must also distribute processes evenly among cores.
This is called:
Load Balancing
Goal:
Prevent overloaded cores
Improve CPU utilization
12. Conceptual Timeline
The following timeline illustrates how processes share the CPU through context switching.
Time →
P1: ███ ███
P2: ███ ███
P3: ███ ███
Each block represents:
CPU execution interval
Each gap represents:
Time during which another process executes
The transitions between blocks occur because of:
Context Switches
Key Insight
The CPU never truly executes all processes simultaneously on a single-core system.
Instead:
Execute P1
Switch
Execute P2
Switch
Execute P3
Switch
Rapid context switching creates the illusion of parallel execution.