Context Switching in Operating System

Interrupts cause the operating system to change a CPU from its current task and to run a kernel routine and such operations happen frequently on general-purpose systems. when processes are being executed if an interrupt occurs or if a process of higher priority comes then that particular process has to stop its execution and it has to allow the process that is causing the interrupt to execute first and after that is completed the previous process can resume its execution. So interrupts cause the operating system to change a CPU from its current task and return the kernel routine so what does this mean we know that when processes are being executed the CPU is being assigned to that particular process. So when the interrupt occurs the CPU has to be assigned to that process or that thing that is causing the interrupt so that that will be executed first and when that is completed the CPU can be reassigned back to the process that was previously been executed so that is what we mean by this line and this kind of operations. They frequently happen on a general-purpose system so when an interrupt occurs the system needs to save the current context of the process currently running on the CPU so that it can restore that context when it’s processing is done essentially suspending the process and then resuming it and the context is represented in the PCB of the process.

When a process is running and if an interrupt occurs then that process has to stop its execution and then the interrupt or the process causing the interrupt has to be executed first so when an interrupt occurs the process that was currently running needs to save its context to the CPU.

What do we mean by context? 

So, context means it consists of the current state or the current information about that particular process and why we need it is because when the process will resume its execution it needs to know where did it stop executing at that particular moment so that when it resumes it will know from where it has to resume its operation again.

Context Switching Working

When we go and execute another process and come back we know that from where we have to resume so that is the concept of context so switching the CPU to another process requires performing a state safe of the current process and a state restore of a different process. When we are switching the CPU to another process that means when you are going to do another task you need to save the state of the current process you need to make sure to save the state of the current process so that when you come back to this process and resume the execution and a state restore of a different process so a different process is going to be executed so that process might be a new process or a partially complete process so that state of that process has to be restored so that you know even this process from where you need to continue its execution so we perform a state safe of a current process and the state restore of a different process and this task is known as a context switch.

Context switching where we save the state of one process and restore the state of another process when a particular process is interrupted by another process during its execution so we see that when one process is executing another process interrupts it and that process which interrupts has to be executed first so the current process has to stop its execution and it has to save its context and the process that is causing the interrupt has to be executed. So that is what we mean by context switch we are switching from one process to another process and then the context of these two processes are stored in the CPU that means one is saved and the other one is restored.

Context Switching Characteristics

you need to remember context switch time is pure overhead because the system does no useful work while switching and it’s 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 to store all resistors and typical speeds are a few milliseconds so the main important thing that you need to remember is context switching time is pure overhead now what do you mean by overhead overhead means the course that is involved in doing something now we are not talking about the financial cost over here in terms of operating system when we say cost cost can mean many things like the resources that we use the time that we spend and so on now we say that context switching time is pure overhead because the system does no useful work while it is switching so we say that when the context switching is happening the system is not doing any useful work while the switching is happening well you may say that well context switching itself is a useful task yeah it is we need to have context switching.

Context switching has to be performed but when we discussed about the objectives of multi programming and time sharing and all that we saw that our main objective to have some process running at all times so that we can maximize the utilization of our CPU and hence we can make efficient utilization of our resources but when the context switching is happening we see that the CPU is switched from one process to another so at that moment only the switching parties working only the switching part is going on there is no process executing at that instant when the switching is happening so that is why it says no useful work is done while switching and then the speed of switching it varies from machine to machine from hardware to hardware and on many factors as it is mentioned in this second point and the typical speeds are few milliseconds so the typical speed that is consumed for switching may be a few milliseconds so these are some of the points which you need to remember about context switching and always be clear about the main idea of context switching when one process is suspended and another process is executed due to an interrupt at that time the context is switched and that is what we mean by context switching.

Leave a Comment