🧠 What is a Dispatcher?
The Dispatcher is the OS module responsible for giving control of the CPU to the process selected by the CPU Scheduler.
In simpler terms:
-
The Scheduler decides who should run.
-
The Dispatcher makes it happen.
⚙️ Responsibilities of the Dispatcher
Once the scheduler picks a process, the dispatcher:
| Step | Task |
|---|---|
| 🧠 1 | Saves the context of the currently running process |
| 📥 2 | Loads the context (registers, stack, PC) of the next process |
| 🖥️ 3 | Switches the CPU mode to user mode (from kernel mode) |
| 🚦 4 | Starts execution of the new process by jumping to its Program Counter (PC) |
This is called a context switch.
🕒 Dispatcher Latency
Dispatcher Latency is the time taken by the dispatcher to stop one process and start another.
This includes:
-
Saving/restoring registers
-
Memory mapping updates
-
Mode switching
⏱️ Low latency = faster context switching = better responsiveness
📍 Where Dispatcher Fits in the Flow
Ready Queue ──(CPU Scheduler chooses)──▶ Process P2
│
[Dispatcher]
↓
Starts executing P2
🧠 Interview-Ready Definition:
The Dispatcher is the operating system component that performs the actual context switch — saving the state of the old process, loading the state of the new one, and transferring CPU control. It acts immediately after the CPU scheduler has made its selection.
🛠️ Real-World Analogy
Think of a train station:
-
The scheduler decides which train goes next.
-
The dispatcher physically switches the tracks and signals the train to move.
Without it, trains just sit there.
🧩 Related Concepts
| Concept | Role |
|---|---|
| CPU Scheduler | Picks the process to run next |
| Dispatcher | Performs the switch to that process |
| Context Switch | The actual save/restore operation |
| Preemption | Triggers dispatcher to kick in |