Part 3 of 6 · 1 min read
Two different switches
In plain words: Two terms are often mixed up. A mode switch changes the processor from user mode to kernel mode, or back, as in a system call (Level 0, page 2). The same process continues to be the current one, and only the privilege level changes. A context switch changes which process is running.
| Mode switch | Context switch | |
|---|---|---|
| What changes | The privilege level | The running process |
| Typical cause | A system call, a trap or an interrupt | A time slice ending, or a wait |
| Cost | Tens to hundreds of nanoseconds | About a microsecond or more |
The earlier measurement on the test machine showed a system call (a mode switch in and out) costing about 87 ns. A context switch costs a good deal more, as the measurement below shows. Every context switch starts with a mode switch, because the kernel must be running to do it, but most mode switches do not lead to a context switch. A call to getppid enters and leaves the kernel and returns to the same process.
Mode switch
A mode switch is a change of the processor between user mode and kernel mode, caused by a system call, an interrupt or a trap, in which the process that is running stays the same.
Example: a call to getpid switches to kernel mode and back to the same process, so it is a mode switch and not a context switch.
Check yourself: 1. Does a system call always cause a context switch? 2. Which is more expensive, a mode switch or a context switch? 3. What is the first step of every context switch?