What Is Context Switching ?
Operating system uses this technique to switch a process between states to execute its function. It is the storing of the context or states of process, so that the process can be resumed from the same point. It occurs when process switches between one and other. It stores the old running process context (suspend state) in form of registers and assign CPU for new process (ready state).
Context switching happens in multitasking system, i.e. multiple processes running in one CPU.
When Context Switching Takes Place?
It takes place due to following reasons:
- When an high priority process comes in ready state. In such case execution of the running process needs to be stopped. So the state of old process is stored and the new process is loaded.
- When an interruption occurs, CPU needs to allocate some hardware to the interupt and then any process.
- When transition between user and kernel mode is required.
Context Switching Triggers:
Multitasking: Process is switched out so that other process can be run easily. The old process state is stored and the new process will be loaded. So the old process can be resumed when needed.
Interrupt Handling: When an interrupt occurs, CPU needs to handle the interrupt first and then process, so the state of process is stored before handling the interupt.
User and Kernel Mode Switching: Whenever there is some switching or transition occurs between user and kernel mode then context switching take place.