what is context switch


1)            Context Switch –

·        Whenever an interrupt arrives, the CPU must do a state-save of the currently running process, and then switch into kernel mode to handle the interrupt, and then do a state-restore of the interrupted process.
·        Similarly, a context switch occurs when the time slice for one process has expired and a new process is to be loaded from the ready queue. This will be instigated by a timer interrupt, which will then cause the current process's state to be saved and the new process's state to be restored.
·        Saving and restoring states involves saving and restoring all of the registers and program counter(s), as well as the process control blocks described above.
·        Context switching happens VERY frequently, and the overhead of doing the switching is just lost CPU time, so context switches (state saves & restores) need to be as fast as possible. Some hardware has special provisions for speeding this up, such as a single machine instruction for saving or restoring all registers at once.

·               A context switch is the mechanism to store and restore the state or context of a CPU in Process Control block

·                So that a process execution can be resumed from the same point at a later time.

·                Using this technique, a context switcher enables multiple processes to share a single CPU.

·               Context switching is an essential part of a multitasking operating system features.

·               When the scheduler switches the CPU from executing one process to execute another, the state from the current running process is stored into the process control block.

·               After this, the state for the process to run next is loaded from its own PCB and used to set the PC, registers, etc.

·                At that point, the second process can start executing.