Open In App

Context Switching in Operating System

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An operating system is a program loaded into a system or computer. and manage all the other program which is running on that OS Program, it manages the all other application programs. or in other words, we can say that the OS is an interface between the user and computer hardware.

So in this article, we will learn about what is Context switching in an Operating System and see how it works also understand the triggers of context switching and an overview of the Operating System.

What is Context Switching in an Operating System?

Context switching in an operating system involves saving the context or state of a running process so that it can be restored later, and then loading the context or state of another. process and run it.

Context Switching refers to the process/method used by the system to change the process from one state to another using the CPUs present in the system to perform its job.

Example of Context Switching

Suppose in the OS there (N) numbers of processes are stored in a Process Control Block(PCB). like The process is running using the CPU to do its job. While a process is running, other processes with the highest priority queue up to use the CPU to complete their job.

Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process. This task is known as a context switch. When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run. Context-switch time is pure overhead because the system does no useful work while switching. Switching speed varies from machine to machine, depending on the memory speed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers). A typical speed is a few milliseconds. Context-switch times are highly dependent on hardware support. For instance, some processors (such as the Sun UltraSPARC) provide multiple sets of registers. A context switch here simply requires changing the pointer to the current register set. Of course, if there are more active processes than there are register sets, the system resorts to copying register data to and from memory, as before. Also, the more complex the operating system, the greater the amount of work that must be done during a context switch

Need of Context Switching

Context switching enables all processes to share a single CPU to finish their execution and store the status of the system’s tasks. The execution of the process begins at the same place where there is a conflict when the process is reloaded into the system.

The operating system’s need for context switching is explained by the reasons listed below.

  • One process does not directly switch to another within the system. Context switching makes it easier for the operating system to use the CPU’s resources to carry out its tasks and store its context while switching between multiple processes.
  • Context switching enables all processes to share a single CPU to finish their execution and store the status of the system’s tasks. The execution of the process begins at the same place where there is a conflict when the process is reloaded into the system.
  • Context switching only allows a single CPU to handle multiple processes requests parallelly without the need for any additional processors.

Context Switching Triggers

The three different categories of context-switching triggers are as follows.

  • Interrupts
  • Multitasking
  • User/Kernel switch

Interrupts: When a CPU requests that data be read from a disc, if any interruptions occur, context switching automatically switches to a component of the hardware that can handle the interruptions more quickly.

Multitasking: The ability for a process to be switched from the CPU so that another process can run is known as context switching. When a process is switched, the previous state is retained so that the process can continue running at the same spot in the system.

Kernel/User Switch: This trigger is used when the OS needed to switch between the user mode and kernel mode.

When switching between user mode and kernel/user mode is necessary, operating systems use the kernel/user switch.

What is Process Control Block(PCB)? 

So, The Process Control block(PCB) is also known as a Task Control Block. it represents a process in the Operating System. A process control block (PCB) is a data structure used by a computer to store all information about a process. It is also called the descriptive process. When a process is created (started or installed), the operating system creates a process manager.

State Diagram of Context Switching

State Diagram of Context Switching

Working Process Context Switching 

So the context switching of two processes, the priority-based process occurs in the ready queue of the process control block. These are the following steps.

  • The state of the current process must be saved for rescheduling. 
  • The process state contains records, credentials, and operating system-specific information stored on the PCB or switch.
  • The PCB can be stored in a single layer in kernel memory or in a custom OS file.
  • A handle has been added to the PCB to have the system ready to run.
  • The operating system aborts the execution of the current process and selects a process from the waiting list by tuning its PCB.
  • Load the PCB’s program counter and continue execution in the selected process.
  • Process/thread values ​​can affect which processes are selected from the queue, this can be important.

FAQs on Context Switching in OS

Q.1: What is the context of the process?

Answer:

The context of a process consists its stack space, address space, virtual address space, register set image (e.g. Program Counter (PC), Instruction Register (IR), Program Status Word (PSW) and other general processor registers), Stack Pointer (SP).

Q.2: What causes a context switch?

Answer:

A context switch can also occur as the result of an interrupt, when a task needs to access disk storage, freeing up CPU time for other tasks. The process of context switching can have a negative impact on system performance.

Q.3: What is context switch time?

Answer:

A Context switch time is a time which spent between two processes (i.e., getting a waiting process for execution and sending an executing process to a waiting state).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads