The Need For Context Switching
The Need For Context Switching
A context switching helps to share a single CPU across all processes to complete its execution
and store the system's tasks status. When the process reloads in the system, the execution of the
process starts at the same point where there is conflicting.
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use
the CPU's resource to accomplish its tasks and store its context. We can resume the
service of the process at the same point later. If we do not store the currently running
process's data or context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be
shut down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the
old process goes into a ready state to wait for its execution in the CPU. Context switching
stores the state of the process to resume its tasks in an operating system. Otherwise, the
process needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status
is saved as registers using context switching. After resolving the interrupts, the process
switches from a wait state to a ready state to resume its execution at the same point later,
where the operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests
simultaneously without the need for any additional processors.
Suppose that multiple processes are stored in a Process Control Block (PCB). One process is
running state to execute its task with the use of CPUs. As the process is running, another process
arrives in the ready queue, which has a high priority of completing its task using CPU. Here we
used context switching that switches the current process with the new process requiring the CPU
to finish its tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution of the process
when the new process stops the old process. If we do not save the state of the process, we have to
start its execution at the initial level. In this way, context switching helps the operating system to
switch between the processes, store or reload the process when it requires executing its tasks.
Current Time 0:00
/
Duration 18:10
Â
Context switching triggers
/user mode is Following are the three types of context switching triggers as follows.
1. Interrupts
2. Multitasking
3. Kernel/User switch
Interrupts: A CPU requests for the data to read from a disk, and if there are any interrupts, the
context switching automatic switches a part of the hardware that requires less time to handle the
interrupts.
Multitasking: A context switching is the characteristic of multitasking that allows the process to
be switched from the CPU so that another process can be run. When switching the process, the
old state is saved to resume the process's execution at the same point in the system.
Kernel/User Switch: It is used in the operating systems when switching between the user mode,
and the kernelperformed.
A PCB (Process Control Block) is a data structure used in the operating system to store all data
related information to the process. For example, when a process is created in the operating
system, updated information of the process, switching information of the process, terminated
process in the PCB.
There are several steps involves in context switching of the processes. The following diagram
represents the context switching of two processes, P1 to P2, when an interrupt, I/O needs, or
priority-based process occurs in the ready queue of PCB.
As we can see in the diagram, initially, the P1 process is running on the CPU to execute its task,
and at the same time, another process, P2, is in the ready state. If an error or interruption has
occurred or the process requires input/output, the P1 process switches its state from running to the
waiting state. Before changing the state of the process P1, context switching saves the context of
the process P1 in the form of registers and the program counter to the PCB1. After that, it loads
the state of the P2 process from the ready state of the PCB2 to the running state.
1. First, thes context switching needs to save the state of process P1 in the form of the
program counter and the registers to the PCB (Program Counter Block), which is in the
running state.
2. Now update PCB1 to process P1 and moves the process to the appropriate queue, such as
the ready queue, I/O queue and waiting queue.
3. After that, another process gets into the running state, or we can select a new process from
the ready state, which is to be executed, or the process has a high priority to execute its
task.
4. Now, we have to update the PCB (Process Control Block) for the selected process P2. It
includes switching the process state from ready to running state or from another state like
blocked, exit, or suspend.
5. If the CPU already executes process P2, we need to get the status of process P2 to resume
its execution at the same time point where the system interrupt occurs.
Similarly, process P2 is switched off from the CPU so that the process P1 can resume execution.
P1 process is reloaded from PCB1 to the running state to resume its task at the same point.
Otherwise, the information is lost, and when the process is executed again, it starts execution at
the initial level.
← prev next →
Java
PHP
HTML
CSS
Bootstrap
JavaScript
jQuery
jQuery UI
Projects
Interview Q
Operating System
next → ← prev
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use
the CPU's resource to accomplish its tasks and store its context. We can resume the
service of the process at the same point later. If we do not store the currently running
process's data or context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be
shut down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the
old process goes into a ready state to wait for its execution in the CPU. Context switching
stores the state of the process to resume its tasks in an operating system. Otherwise, the
process needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status
is saved as registers using context switching. After resolving the interrupts, the process
switches from a wait state to a ready state to resume its execution at the same point later,
where the operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests
simultaneously without the need for any additional processors.
Suppose that multiple processes are stored in a Process Control Block (PCB). One process is
running state to execute its task with the use of CPUs. As the process is running, another process
arrives in the ready queue, which has a high priority of completing its task using CPU. Here we
used context switching that switches the current process with the new process requiring the CPU
to finish its tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution of the process
when the new process stops the old process. If we do not save the state of the process, we have to
start its execution at the initial level. In this way, context switching helps the operating system to
switch between the processes, store or reload the process when it requires executing its tasks.
Java
PHP
HTML
CSS
Bootstrap
JavaScript
jQuery
jQuery UI
Projects
Interview Q
Operating System
Operating System Types of Operating Systems
Process Management
Process Management in OS Attributes of a Process Process States Process Schedulers Process
Queues Times Related to Process CPU Scheduling Scheduling Algorithms FCFS Scheduling
Convoy Effect in FCFS FCFS with overhead SJF Scheduling Burst Time Prediction SRTF
scheduling SRTF GATE 2011 Example Round Robin Scheduling RR scheduling Example
HRRN Scheduling HRNN Example Priority Scheduling Non Preemptive Priority Preemptive
Priority SRTF:IO bound processes
Synchronization
Introduction Critical Section Problem Lock Variable Mechanism TSL Mechanism Priority
Inversion in TSL Turn Variable Interested Variable Paterson Solution Without Busy Waiting
Sleep and Wake Semaphore Introduction Counting Semaphore Problem on counting semaphore
Binary Semaphore
Deadlocks
Introduction strategies Handling Deadlock Prevention Deadlock Avoidance Resource Allocation
Graph Detection using RAG Detection and Recovery
Memory Management
Introduction Fixed Partitioning Dynamic Partitioning Compaction Bit Map for Dynamic
Partitioning Linked List for Dynamic Partitioning Partitioning Algorithms GATE on Best Fit &
First Fit Need for Paging Paging with Example Binary Addresses Physical & Logical Address
Page Table Mapping from page table Page Table Entry Page Table Size Finding Optimal Page
Size Virtual Memory Look aside Buffer GATE question on TLB Demand Paging Inverted Page
Table Page Replacement Gate on LRU and FIFO Numerical on LRU, FIFO Beladys Anamoly
Segmentation Paging VS Segmentation Segmented Paging
File Management
Attributes of the File Operations on the File File Access Methods Directory Structure Single
level Directory Two level Directory Tree structured Directory Acyclic Graph Directories File
System File System Structure Master Boot Record On Disk Data Structures In memory Data
structures Directory Implementation Allocation Methods Contiguous Allocation Linked List
Allocation File Allocation Table Indexed Allocation Linked Index Allocation Inode Free space
Management Disk Scheduling FCFS Scheduling SSTF Scheduling SCAN and C-SCAN Look
and C-Look Numerical on SSTF Numerical on Disk
Misc
Functions of OS Mobile OS Swapping in OS Threads in OS Fedora Operating System Uses of
Operating System Producer-Consumer problem Dining Philosophers Problem Readers Writers
Problem History Of OS Banker's Algorithm in OS What is the context switching in the
operating system Internal vs. External Fragmentation Multiprocessing Operating system
Multiprogramming vs Multitasking Network Operating System Latest Operating System System
Calls in Operating System Multitasking Operating System Distributed Operating System
Fragmentation in Operating System Multithreading Models in Operating system Operating
system services Batch Operating System Embedded Operating System Time-Sharing vs Real-
Time Operating System Multi-User Operating System Memory Management Monolithic
Structure of Operating System MS-DOS Operating System Spooling in Operating System
Network Operating System vs Distributed Operating System Operating system vs Application
software System Software vs Operating System Real-Time operating system Booting in
Operating System Layered Structure of Operating System Multiprogramming vs. Time Sharing
Operating System Distributed File System Multiprogramming Operating System Server
Operating System 32 bit vs 64 bit Operating System Single User Operating System Process vs
Program Operating System Security Components of Operating System GUI Operating System
Android Operating System Buffering in Operating System Device Driver in Operating System
Best Operating System for Hacking Clustered Operating System Android vs Windows OS
Scheduler vs Dispatcher Deadlock vs Starvation Semaphore vs Monitor Boot Block and Bad
Block in Operating System Hard and Soft Real-Time Operating System Concurrency in
Operating System Microkernel in Operating System Protection in Operating System Benefits of
Multithreading Host vs Guest Operating System Livelock in Operating System Dual Mode
Operations in Operating System What is Access Token in Operating System Deadlock Detection
in Distributed Systems CUI vs GUI Monolithic vs layered Operating System Mutex vs
Semaphore What is Interleaved Memory Trap vs Interrupt in Operating System Open-Source
Operating System Multiple Processors Scheduling in Operating System Starvation and Aging in
Operating Systems Best Android Operating System for PC Device Management in Operating
System Disk Controller in Operating System Views of Operating System Tasks in Real-Time
Systems What is RPC in Operating System C-SCAN vs SSTF Disk Scheduling Algorithm Look
vs C-Look Disk Scheduling Algorithm SCAN vs C-SCAN Disk Scheduling Algorithm SCAN vs
FCFS Disk Scheduling Algorithm Lamport's Bakery Algorithm Basic Disk vs Dynamic Disk
What is Zombie Process FCFS vs SSTF Disk Scheduling Algorithm LRU vs LFU Page
Replacement Algorithm SSTF vs C-LOOK Disk Scheduling Algorithm C-LOOK vs C-SCAN
Disk Scheduling Algorithm Rotational Latency vs Disk Access Time in Disk Scheduling SSTF
vs LOOK Disk Scheduling Algorithm Virtual vs Physical Address Address binding in Operating
System Preemptive vs Non-Preemptive Scheduling Properties of Operating System What is
Processor Affinity Seek Time vs Disk Access Time in Disk Scheduling Seek Time vs Transfer
Time in Disk Scheduling Page vs Block in Operating System Virtual Memory vs Job Pool What
is Memory Ballooning Embedded OS vs Desktop OS Microkernel vs Monolithic Kernel
Spinlock vs Semaphore What are Demand Paging and Pre-paging What is Interrupt in OS
Swapping vs Context Switching System Call vs Library Call Ubuntu vs Kubuntu Routine vs
Process Overlays in Memory Management fork() vs exec() Latency vs Jitter Process Contention
Scope vs System Contention Scope Implement Threads in User Space ISR vs Function Call
POSIX Threads in OS Best Portable Operating System Time-Sharing vs Distributed Operating
System UNIX vs Windows Operating System Swap-Space Management in Operating System
User View vs Hardware View vs System View in Operating System Internal Structure of UNIX
File system Best Operating System for Programming Disk Attachment in Operating System
Multiprocessor and Multicore System in Operating System Resource Deadlocks vs
Communication Deadlocks in Distributed Systems What is Buddy System Why must User
Threads be mapped to Kernel Thread Best Secure Operating System Best UNIX Operating
System What is Hashed Page Table in Operating System What is UEFI and How It Differ from
BIOS MacOS vs OpenBSD Disk Operating System Commands IPC through Shared Memory
IPC using Message Queues Causes of Operating System Failure IoT Operating Systems What is
VSE Operating System Allocation of Frames in OS Cooperating Process in OS Page Fault
Handling in OS Client OS vs Server OS long term Scheduler vs short term Scheduler Best Cloud
Operating Systems Firmware vs Operating System Linux vs Solaris Mobile OS vs Desktop OS
FreeBSD Operating System Tails Operating System Best Real-Time Operating System Disk
Management in Operating System Best books for Operating System Implementation of Access
matrix in the operating system KaiOS Operating System Best Alternative Operating System
Operating System Hardening Best courses for the operating system Business Operating System
BSD Based Operating System Processor vs Operating System Standalone vs Network Operating
System 5 State Process Model in Operating System Best operating system for gaming Two State
Process Model in Operating System Best Book for Linux Operating System Facts about Android
Operating System Facts about Windows Operating System Best Operating System for Netbook
Best Alternative Operating System for Android Best Operating System for Chromebook Design
approaches in Operating System Best 32 bit Linux Operating System Android vs Linux physical
vs logical file systems Shared Memory vs Message Passing Best Ubuntu-based Linux Distros
DOS vs UNIX File Models in Distributed Operating System AIX OS Commands Features of
Blackberry 10 OS Contiguous and Non-Contiguous Memory Allocation in Operating System
AIX vs Linux OS Parallel Computing vs Distributed Computing Multilevel Queue Scheduling in
Operating System Best SSD for Operating System Interesting Facts about the iOS Operating
System Best Terminal Operating Systems Batch OS vs Multiprogramming OS Real-Time os vs
general-purpose os Static and Dynamic Loading in Operating System Arch Linux Operating
System Kylin Operating System Parrot Operating System HP-UX Operating System Tizen
Operating System VxWorks Operating System eCos Operating System Haiku Operating System
Knoppix Operating System Puppy Linux Operating System AliOS Operating System Gentoo
Linux Operating System Skyos Operating System Best Operating System for Raspberry Pi gOS
Operating System Edubuntu OS Harmony OS IRIX OS Security vs Protection Cold booting vs
Warm booting in OS FAT32 vs NTFS in OS Paging vs Swapping in OS Symmetric vs
Asymmetric Multiprocessing in OS Difference between Buffering and Caching in Operating
System Difference between Interrupt and Polling in Operating System Difference between
Multitasking and Multithreading in Operating System Difference between System call and
System Program in Operating System Deadlock Prevention vs Deadlock Avoidance in OS fork()
vs vfork() Coupled vs Tightly Coupled Multiprocessor System Maskable vs Non-Maskable
Interrupts Oracle Linux vs Red Hat Enterprise Linux Preemptive and Non-Preemptive Kernel
Difference between CentOS and Red Hat Enterprise Linux OS Difference between Kubuntu and
Debian Operating System Difference between Preemptive and Cooperative Multitasking
Difference between Spinlock and Mutex in Operating System Difference between Cinnamon and
MATE Difference between Device Driver and Device Controller in Operating System
Difference between Full Virtualization and Paravirtualization in Operating System Difference
between GRUB and LILO in the operating system Difference between Virtualbox and VMware
What is a distributed shared memory? And its advantages Difference between AIX and Solaris
Operating System Difference between Concurrency and Parallelism in Operating System
Difference between QNX and VxWorks Operating System Difference between User level and
Kernel level threads in Operating System Nucleus Operating System Input/Output Hardware and
Input/Output Controller Privileged and Non-Privileged Instructions in Operating System CPU
Scheduling Algorithms in Operating Systems Mass Storage Structure in Operating Systems N-
Process Peterson Algorithm Xv6 Operating System - Adding a New System Call Non-
Contiguous Memory Allocation in Operating System Types of Linux OS Operating System
Structure Operating System Examples What is Starvation in Operating System What is Google
Chrome Operating System Introduction to Robot Operating System Which Operating System to
Choose For Web Development Necessary Condition for Deadlock in OS Race Condition in
Operating Systems Simple Structure in Operating Systems What is the Process in Operating
Systems
MCQ
Operating System MCQ OS MCQ Part-2
next → ← prev
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use
the CPU's resource to accomplish its tasks and store its context. We can resume the
service of the process at the same point later. If we do not store the currently running
process's data or context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be
shut down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the
old process goes into a ready state to wait for its execution in the CPU. Context switching
stores the state of the process to resume its tasks in an operating system. Otherwise, the
process needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status
is saved as registers using context switching. After resolving the interrupts, the process
switches from a wait state to a ready state to resume its execution at the same point later,
where the operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests
simultaneously without the need for any additional processors.
Suppose that multiple processes are stored in a Process Control Block (PCB). One process is
running state to execute its task with the use of CPUs. As the process is running, another process
arrives in the ready queue, which has a high priority of completing its task using CPU. Here we
used context switching that switches the current process with the new process requiring the CPU
to finish its tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution of the process
when the new process stops the old process. If we do not save the state of the process, we have to
start its execution at the initial level. In this way, context switching helps the operating system to
switch between the processes, store or reload the process when it requires executing its tasks.
next →← prev
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use
the CPU's resource to accomplish its tasks and store its context. We can resume the
service of the process at the same point later. If we do not store the currently running
process's data or context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be
shut down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the
old process goes into a ready state to wait for its execution in the CPU. Context switching
stores the state of the process to resume its tasks in an operating system. Otherwise, the
process needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status
is saved as registers using context switching. After resolving the interrupts, the process
switches from a wait state to a ready state to resume its execution at the same point later,
where the operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests
simultaneously without the need for any additional processors.
Suppose that multiple processes are stored in a Process Control Block (PCB). One process is
running state to execute its task with the use of CPUs. As the process is running, another process
arrives in the ready queue, which has a high priority of completing its task using CPU. Here we
used context switching that switches the current process with the new process requiring the CPU
to finish its tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution of the process
when the new process stops the old process. If we do not save the state of the process, we have to
start its execution at the initial level. In this way, context switching helps the operating system to
switch between the processes, store or reload the process when it requires executing its tasks.
Operating System
Operating System Types of Operating Systems
Process Management
Process Management in OS Attributes of a Process Process States Process Schedulers Process
Queues Times Related to Process CPU Scheduling Scheduling Algorithms FCFS Scheduling
Convoy Effect in FCFSFCFS with overheadSJF SchedulingBurst Time PredictionSRTF
schedulingSRTF GATE 2011 ExampleRound Robin SchedulingRR s
Synchronization
Introduction Critical Section Problem Lock Variable Mechanism TSL Mechanism Priority
Inversion in TSL Turn Variable Interested Variable Paterson Solution Without Busy Waiting
Sleep and Wake Semaphore Introduction Counting Semaphore Problem on counting semaphore
Binary Semaphore
Deadlocks
Introduction strategies Handling Deadlock Prevention Deadlock Avoidance Resource Allocation
Graph Detection using RAG Detection and Recovery
Memory Management
Introduction Fixed Partitioning Dynamic Partitioning Compaction Bit Map for Dynamic
Partitioning Linked List for Dynamic Partitioning Partitioning Algorithms GATE on Best Fit &
First Fit Need for Paging Paging with Example Binary Addresses Physical & Logical Address
Page Table Mapping from page table Page Table Entry Page Table Size Finding Optimal Page
Size Virtual Memory Look aside Buffer GATE question on TLB Demand Paging Inverted Page
Table Page Replacement Gate on LRU and FIFO Numerical on LRU, FIFO Beladys Anamoly
Segmentation Paging VS Segmentation Segmented Paging
File Management
Attributes of the File Operations on the File File Access Methods Directory Structure Single
level Directory Two level Directory Tree structured Directory Acyclic Graph Directories File
System File System Structure Master Boot Record On Disk Data Structures In memory Data
structures Directory Implementation Allocation Methods Contiguous Allocation Linked List
Allocation File Allocation Table Indexed Allocation Linked Index Allocation Inode Free space
Management Disk Scheduling FCFS Scheduling SSTF Scheduling SCAN and C-SCAN Look
and C-Look Numerical on SSTF Numerical on Disk
Misc
Functions of OS Mobile OS Swapping in OS Threads in OS Fedora Operating System Uses of
Operating System Producer-Consumer problem Dining Philosophers Problem Readers Writers
Problem History Of OS Banker's Algorithm in OS What is the context switching in the
operating system Internal vs. External Fragmentation Multiprocessing Operating system
Multiprogramming vs Multitasking Network Operating System Latest Operating System System
Calls in Operating System Multitasking Operating System Distributed Operating System
Fragmentation in Operating System Multithreading Models in Operating system Operating
system services Batch Operating System Embedded Operating System Time-Sharing vs Real-
Time Operating System Multi-User Operating System Memory Management Monolithic
Structure of Operating System MS-DOS Operating System Spooling in Operating System
Network Operating System vs Distributed Operating System Operating system vs Application
software System Software vs Operating System Real-Time operating system Booting in
Operating System Layered Structure of Operating System Multiprogramming vs. Time Sharing
Operating System Distributed File System Multiprogramming Operating System Server
Operating System 32 bit vs 64 bit Operating System Single User Operating System Process vs
Program Operating System Security Components of Operating System GUI Operating System
Android Operating System Buffering in Operating System Device Driver in Operating System
Best Operating System for Hacking Clustered Operating System Android vs Windows OS
Scheduler vs Dispatcher Deadlock vs Starvation Semaphore vs Monitor Boot Block and Bad
Block in Operating System Hard and Soft Real-Time Operating System Concurrency in
Operating System Microkernel in Operating System Protection in Operating System Benefits of
Multithreading Host vs Guest Operating System Livelock in Operating System Dual Mode
Operations in Operating System What is Access Token in Operating System Deadlock Detection
in Distributed Systems CUI vs GUI Monolithic vs layered Operating System Mutex vs
Semaphore What is Interleaved Memory Trap vs Interrupt in Operating System Open-Source
Operating System Multiple Processors Scheduling in Operating System Starvation and Aging in
Operating Systems Best Android Operating System for PC Device Management in Operating
System Disk Controller in Operating System Views of Operating System Tasks in Real-Time
Systems What is RPC in Operating System C-SCAN vs SSTF Disk Scheduling Algorithm Look
vs C-Look Disk Scheduling Algorithm SCAN vs C-SCAN Disk Scheduling Algorithm SCAN vs
FCFS Disk Scheduling Algorithm Lamport's Bakery Algorithm Basic Disk vs Dynamic Disk
What is Zombie Process FCFS vs SSTF Disk Scheduling Algorithm LRU vs LFU Page
Replacement Algorithm SSTF vs C-LOOK Disk Scheduling Algorithm C-LOOK vs C-SCAN
Disk Scheduling Algorithm Rotational Latency vs Disk Access Time in Disk Scheduling SSTF
vs LOOK Disk Scheduling Algorithm Virtual vs Physical Address Address binding in Operating
System Preemptive vs Non-Preemptive Scheduling Properties of Operating System What is
Processor Affinity Seek Time vs Disk Access Time in Disk Scheduling Seek Time vs Transfer
Time in Disk Scheduling Page vs Block in Operating System Virtual Memory vs Job Pool What
is Memory Ballooning Embedded OS vs Desktop OS Microkernel vs Monolithic Kernel
Spinlock vs Semaphore What are Demand Paging and Pre-paging What is Interrupt in OS
Swapping vs Context Switching System Call vs Library Call Ubuntu vs Kubuntu Routine vs
Process Overlays in Memory Management fork() vs exec() Latency vs Jitter Process Contention
Scope vs System Contention Scope Implement Threads in User Space ISR vs Function Call
POSIX Threads in OS Best Portable Operating System Time-Sharing vs Distributed Operating
System UNIX vs Windows Operating System Swap-Space Management in Operating System
User View vs Hardware View vs System View in Operating System Internal Structure of UNIX
File system Best Operating System for Programming Disk Attachment in Operating System
Multiprocessor and Multicore System in Operating System Resource Deadlocks vs
Communication Deadlocks in Distributed Systems What is Buddy System Why must User
Threads be mapped to Kernel Thread Best Secure Operating System Best UNIX Operating
System What is Hashed Page Table in Operating System What is UEFI and How It Differ from
BIOS MacOS vs OpenBSD Disk Operating System Commands IPC through Shared Memory
IPC using Message Queues Causes of Operating System Failure IoT Operating Systems What is
VSE Operating System Allocation of Frames in OS Cooperating Process in OS Page Fault
Handling in OS Client OS vs Server OS long term Scheduler vs short term Scheduler Best Cloud
Operating Systems Firmware vs Operating System Linux vs Solaris Mobile OS vs Desktop OS
FreeBSD Operating System Tails Operating System Best Real-Time Operating System Disk
Management in Operating System Best books for Operating System Implementation of Access
matrix in the operating system KaiOS Operating System Best Alternative Operating System
Operating System Hardening Best courses for the operating system Business Operating System
BSD Based Operating System Processor vs Operating System Standalone vs Network Operating
System 5 State Process Model in Operating System Best operating system for gaming Two State
Process Model in Operating System Best Book for Linux Operating System Facts about Android
Operating System Facts about Windows Operating System Best Operating System for Netbook
Best Alternative Operating System for Android Best Operating System for Chromebook Design
approaches in Operating System Best 32 bit Linux Operating System Android vs Linux physical
vs logical file systems Shared Memory vs Message Passing Best Ubuntu-based Linux Distros
DOS vs UNIX File Models in Distributed Operating System AIX OS Commands Features of
Blackberry 10 OS Contiguous and Non-Contiguous Memory Allocation in Operating System
AIX vs Linux OS Parallel Computing vs Distributed Computing Multilevel Queue Scheduling in
Operating System Best SSD for Operating System Interesting Facts about the iOS Operating
System Best Terminal Operating Systems Batch OS vs Multiprogramming OS Real-Time os vs
general-purpose os Static and Dynamic Loading in Operating System Arch Linux Operating
System Kylin Operating System Parrot Operating System HP-UX Operating System Tizen
Operating System VxWorks Operating System eCos Operating System Haiku Operating System
Knoppix Operating System Puppy Linux Operating System AliOS Operating System Gentoo
Linux Operating System Skyos Operating System Best Operating System for Raspberry Pi gOS
Operating System Edubuntu OS Harmony OS IRIX OS Security vs Protection Cold booting vs
Warm booting in OS FAT32 vs NTFS in OS Paging vs Swapping in OS Symmetric vs
Asymmetric Multiprocessing in OS Difference between Buffering and Caching in Operating
System Difference between Interrupt and Polling in Operating System Difference between
Multitasking and Multithreading in Operating System Difference between System call and
System Program in Operating System Deadlock Prevention vs Deadlock Avoidance in OS fork()
vs vfork() Coupled vs Tightly Coupled Multiprocessor System Maskable vs Non-Maskable
Interrupts Oracle Linux vs Red Hat Enterprise Linux Preemptive and Non-Preemptive Kernel
Difference between CentOS and Red Hat Enterprise Linux OS Difference between Kubuntu and
Debian Operating System Difference between Preemptive and Cooperative Multitasking
Difference between Spinlock and Mutex in Operating System Difference between Cinnamon and
MATE Difference between Device Driver and Device Controller in Operating System
Difference between Full Virtualization and Paravirtualization in Operating System Difference
between GRUB and LILO in the operating system Difference between Virtualbox and VMware
What is a distributed shared memory? And its advantages Difference between AIX and Solaris
Operating System Difference between Concurrency and Parallelism in Operating System
Difference between QNX and VxWorks Operating System Difference between User level and
Kernel level threads in Operating System Nucleus Operating System Input/Output Hardware and
Input/Output Controller Privileged and Non-Privileged Instructions in Operating System CPU
Scheduling Algorithms in Operating Systems Mass Storage Structure in Operating Systems N-
Process Peterson Algorithm Xv6 Operating System - Adding a New System Call Non-
Contiguous Memory Allocation in Operating System Types of Linux OS Operating System
Structure Operating System Examples What is Starvation in Operating System What is Google
Chrome Operating System Introduction to Robot Operating System Which Operating System to
Choose For Web Development Necessary Condition for Deadlock in OS Race Condition in
Operating Systems Simple Structure in Operating Systems What is the Process in Operating
Systems
MCQ
Operating System MCQ OS MCQ Part-2
next → ← prev
Context Switching in OS (Operating System)
The Context switching is a technique or method used by the operating system to switch a process
from one state to another to execute its function using CPUs in the system. When switching
perform in the system, it stores the old running process's status in the form of registers and
assigns the CPU to a new process to execute its tasks. While a new process is running in the
system, the previous process must wait in a ready queue. The execution of the old process starts
at that point where another process stopped it. It defines the characteristics of a multitasking
operating system in which multiple processes shared the same CPU to perform multiple tasks
without the need for additional processors in the system.
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use
the CPU's resource to accomplish its tasks and store its context. We can resume the
service of the process at the same point later. If we do not store the currently running
process's data or context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be
shut down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the
old process goes into a ready state to wait for its execution in the CPU. Context switching
stores the state of the process to resume its tasks in an operating system. Otherwise, the
process needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status
is saved as registers using context switching. After resolving the interrupts, the process
switches from a wait state to a ready state to resume its execution at the same point later,
where the operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests
simultaneously without the need for any additional processors.
Suppose that multiple processes are stored in a Process Control Block (PCB). One process is
running state to execute its task with the use of CPUs. As the process is running, another process
arrives in the ready queue, which has a high priority of completing its task using CPU. Here we
used context switching that switches the current process with the new process requiring the CPU
to finish its tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution of the process
when the new process stops the old process. If we do not save the state of the process, we have to
start its execution at the initial level. In this way, context switching helps the operating system to
switch between the processes, store or reload the process when it requires executing its tasks.
There are several steps involves in context switching of the processes. The following diagram
represents the context switching of two processes, P1 to P2, when an interrupt, I/O needs, or
priority-based process occurs in the ready queue of PCB.
As we can see in the diagram, initially, the P1 process is running on the CPU to execute its task,
and at the same time, another process, P2, is in the ready state. If an error or interruption has
occurred or the process requires input/output, the P1 process switches its state from running to
the waiting state. Before changing the state of the process P1, context switching saves the
context of the process P1 in the form of registers and the program counter to the PCB1. After
that, it loads the state of the P2 process from the ready state of the PCB2 to the running state.
The following steps are taken when switching Process P1 to Process 2:
1. First, thes context switching needs to save the state of process P1 in the form of the
program counter and the registers to the PCB (Program Counter Block), which is in the
running state.
2. Now update PCB1 to process P1 and moves the process to the appropriate queue, such as
the ready queue, I/O queue and waiting queue.
3. After that, another process gets into the running state, or we can select a new process from
the ready state, which is to be executed, or the process has a high priority to execute its
task.
4. Now, we have to update the PCB (Process Control Block) for the selected process P2. It
includes switching the process state from ready to running state or from another state like
blocked, exit, or suspend.
5. If the CPU already executes process P2, we need to get the status of process P2 to resume
its execution at the same time point where the system interrupt occurs.
Similarly, process P2 is switched off from the CPU so that the process P1 can resume execution.
P1 process is reloaded from PCB1 to the running state to resume its task at the same point.
Otherwise, the information is lost, and when the process is executed again, it starts execution at
the initial level.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
…..
Whenever the transaction between user and kernel mode is required, a context switch is not
necessary; simply a mode transition is not a context switch. Because, depending on the operating
system, a context switch may also take place at this time.
The values of the CPU registers are generally saved in the process table of the process
which was running before the clock interrupt occurred.
To run next the registers are loaded from the process and picked by the CPU scheduler.
Consider a multi-programmed single processor system, the context switches occur frequently so
that all processes appear to run concurrently. If a process has more than one thread, the Operating
System uses the context switching technique to schedule the threads so they appear to execute in
parallel.
It is the case if threads are implemented at the kernel level. Threads can also be implemented at
the user level in run-time libraries. Because, no thread scheduling is provided by the Operating
System, and it is the responsibility of programmer to provide the CPU frequently enough in each
thread so all threads in the process can make progress
Context switches among processes are very expensive. Before a process is switched its process
control block (PCB) has to be saved by the operating system.
Whenever the PCB of the currently executing process is saved by the operating system, then that
loads the PCB of the next process that has to be run on the CPU. This is a heavy task and it takes
more time.
Let us see the diagram below of how the context switching takes place between the processes −
……………..’’’..
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
’’’’’’’’’’’’’’’’’
Context Switching is the mechanism that allows multiple processes to use a single CPU. Context
Switching stores the status of the ongoing process so that the process can be reloaded from the
same point from where it was stopped.
Scope
This article explains What is Context Switching, Steps involved in Context Switching,
Example of Context Switching, and the advantages and disadvantage of Context
Switching.
This article does not cover any practical implementation of performing Context
Switiching.
Bob: Yes, Professor, multiprocessing systems are the type of systems where multiple processes
can be executed even if the system contains only one CPU.
Professor: Very good, Bob! But have you ever wondered how multiple processes are executed
on the system having a single CPU?
Professor: The magic of executing multiple processes on a single CPU takes place with the help
of Context Switching.
When any process is utilizing CPU, and some other process requires the CPU, Context Switching
is carried out before passing the control of CPU from the old process to the new process.
Alice: Professor, what does context switching actually do, and why is Context Switching
necessary?
Context Switching stores the state of the current process, and the state includes the information
about the data that was stored in registers, the value of the program counter, and the stack
pointer. Context Switching is necessary because if we directly pass the control of CPU to the
new process without saving the state of the old process and later if we want to resume the old
process from where it was stopped, we won't be able to do that as we don't know what was the
last instruction the old process executed. Context Switching overcomes this problem by storing
the state of the process.
Bob: Yes, imagine if Context Switching is not there, and you are taking notes in a word file, and
each time you browse something on the web and switch back, you need to rewrite the entire
notes. Giggles
Professor: Haha! That's a nice way to put the concept. Now let's see in which situations the
context switch takes place.
Professor: No worries, Bob. It's good you are asking the question.
PCB stands for Process Control Block. PCB is a special data structure that is used by the
operating system to store all the process-relevant data in it. PCB also stores the state of the
process, which depicts whether the process is ready, running, or waiting.
As we can see in the image, there are two processes, namely P1 and P2. P1 is being executed by
the CPU currently. If process P1 requires performing some I/O(Input-Output) task or if any
interrupt takes place, the state of the process needs to change, but before changing the state, the
Context Switching takes place.
Following are the steps which takes place while switching process P1 and process P2:
1. The data present in the register and program counter will be stored in PCB of process P1,
let's say PCB1, and change the state in PCB1 .
2. Process P1 will be moved to the appropriate queue, which can be either ready queue, I/O
queue, or waiting queue.
3. The next process will be picked from the ready queue, let's say P2.
4. The state of the process P2 will be changed to running state, and if P2 was the process
that was previously executed by CPU, it would resume the execution from where it was
stopped.
5. If we need to execute the process P1, we need to carry out the same tasks mentioned in
step 1 to step 4.
Alice: Professor, I understood the entire steps involved in context switching but can you provide
one example for simplicity?
1. Multitasking: In the multitasking environment, when one process is utilizing CPU, and
there is a need for CPU by another process, Context Switching triggers. Context
Switching saves the state of the old process and passes the control of the CPU to the new
process.
2. Interrupt Handling: When an interrupt takes place, the CPU needs to handle the
interrupt. So before handling the interrupt, the Context Switching gets triggered, which
saves the state of the process before handling the interrupt.
3. User and Kernel Mode Switching: The user mode is the normal mode in which the user
application can execute with limited access, whereas kernel mode is the mode in which
the process can carry out the system-level operations that are not available in the user
mode. So, whenever the switching takes from user mode to kernel mode, mode switching
triggers the Context Switching, which stores the state on an ongoing process.
Note: Interrupt is the event raised by hardware or software which needs immediate attention.
Professor: As we have seen when the Context Switching triggers, we can now look at the steps
involved in context switching.
Tarun Luthra
Java Course Online for Beginners
45k+ enrolled
Rahul Janghu
Free Python Certification Course: Master the essentials
28k+ enrolled
Prateek Narang
Free C++ Course: Learn the Essentials
24k+ enrolled
35,262+ learners have attended these Courses.
If the priority number of the new process is less than the older process, the execution of the older
process continues. But if the priority number of a new process is higher than the older process,
Context Switching will get triggered. During the context switch, the data of registers, program
counter, and state of the process is stored in the Process Control Block. The older process is
placed in the waiting queue, while the state of the newly created process will be changed to a
running state.
Once the execution of the newly created process is over, the older process present in the waiting
queue will be fetched, and it will resume the execution from where it was stopped.
Alice: Thank you, Professor. I clearly understood the working of context switching.
Professor: Now, we will learn about the advantages of context switching.
Conclusion
During a context switch, the context of the old process is saved, and the CPU is allocated
to the new process. Once the new process gets completed and the CPU is returned to the
old process, it can continue the execution from where it was stopped.
Context Switching gets triggered during multiprocessing, interrupt handling, and
switching from user mode to kernel mode.
During a context switch, the data and state of the old process are stored in PCB, and the
CPU is allotted to the new process.
Context Switching gives the impression to the user that the system has multiple CPUs by
executing multiple processes.
Context Switching is considered as overhead as the CPU remains idle during that time.
Context Switching also leads to frequent flushing of TLB(Translation Lookaside Buffer)
and Cache.