What is Context Switch?
A context switch is the process of storing and restoring state (context) of a CPU so
that execution can be resumed from the same point at a later time. This way, a
single CPU can handle multiple processes .
It’s because of context switch that a single core processor can still handle when
the user uses excel, word and paint at a time. This has become an inevitable
need in the world of multitasking.
How does context switch work?
For example, process A with its address space
and stack is currently being executed by the CPU and there is a system call to
jump to a higher priority process B; the CPU will remember the current state of
the process A so that it can suspend its operation, begin executing the new
process B and when done, it returns to its previously executing process A.
Hence, Context switches are resource intensive.
Context switches can relate to either a process
switch, a thread switch within a process or a register switch. The major need
for a context switch arises when CPU has to switch between user mode and kernel
mode.
So when does Context Switch occur?
Context switch occurs when any of the following
happens in CPU:
1. Multitasking
In a preemptive multitasking operating system, the scheduler allows every task to run
for some certain amount of time, called its time slice.
Within this scheduling time, one process needs to be switched out of the CPU so
another process will get a chance to run. So this ensures that the CPU cannot be monopolized by any one
processor-intensive application.
2. Interrupt handling
If the CPU requests data from a disk, for example, it does not need to wait until the read is over, it can issue the request and continue with some other execution; when the read is over, the CPU will be interrupted and presented with the read.
When a transition happens between user mode and kernel mode,
the CPU makes a context switch.
No comments:
Post a Comment