0% found this document useful (0 votes)
132 views13 pages

Unit-2 Part 3 Context Switching

Context switching is a method used by operating systems to switch processes, allowing multitasking by sharing a single CPU among multiple processes. It involves saving the state of the currently running process and loading the state of a new process, which can occur due to high-priority tasks, interrupts, or I/O requests. While context switching enables efficient CPU usage, it also incurs overhead costs and can lead to performance delays during the switching process.

Uploaded by

preathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views13 pages

Unit-2 Part 3 Context Switching

Context switching is a method used by operating systems to switch processes, allowing multitasking by sharing a single CPU among multiple processes. It involves saving the state of the currently running process and loading the state of a new process, which can occur due to high-priority tasks, interrupts, or I/O requests. While context switching enables efficient CPU usage, it also incurs overhead costs and can lead to performance delays during the switching process.

Uploaded by

preathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

AIPC602 - EMBEDDED

SYSTEMS AND INTERNET


OF THINGS(IOT)
UNIT II
Context Switching in OS
What is the context switching in the 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.
When does context switching
happen?
1. When a high-priority process comes to a ready state (i.e. with higher
priority than the running process)
2. An Interrupt occurs
3. User and kernel-mode switch (It is not necessary though)
4. Preemptive CPU scheduling used.
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.
• 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.
• 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.
• 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.
• 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.
• A context switching allows a single CPU to handle multiple process requests simultaneously without the
need for any additional processors.
Example of Context Switching
• 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.
Context switching triggers
• Following are the three types of context switching triggers as follows.
• Interrupts
• Multitasking
• 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 kernel/user mode is performed.
What is the PCB?

• 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.
Steps for Context Switching
• 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
Context Switching
• The following steps are taken when switching Process P1 to Process 2:
• 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.
• 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.
• 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.
• 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.
• 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.
Context Switching Cost

• Context Switching leads to an overhead cost because of TLB flushes,


sharing the cache between multiple tasks, running the task scheduler
etc. Context switching between two threads of the same process is
faster than between two different processes as threads have the
same virtual memory maps. Because of this TLB flushing is not
required.
Advantage of Context Switching
• Context switching is used to achieve multitasking i.e.
multiprogramming with time-sharing.
• Multitasking gives an illusion to the users that more than one process
are being executed at the same time. But in reality, only one task is
being executed at a particular instant of time by a processor. Here, the
context switching is so fast that the user feels that the CPU is
executing more than one task at the same time.
The disadvantage of Context
Switching
• The disadvantage of context switching is that it requires some time for
context switching i.e. the context switching time. Time is required to
save the context of one process that is in the running state and then
getting the context of another process that is about to come in the
running state. During that time, there is no useful work done by the
CPU from the user perspective. So, context switching is pure overhead
in this condition.
• https://www.javatpoint.com/what-is-the-context-switching-in-the-op
erating-system
• https://afteracademy.com/blog/what-is-context-switching-in-
operating-system

You might also like