Sub topic 10
The CPU Scheduler, the Dispatcher, and Preemption
Assume you know process states, interrupts, queues, and basic arithmetic. This chapter explains how an operating system selects a ready process and transfers control to it.
Scheduler and Dispatcher
The CPU scheduler chooses one process from the ready queue. Its decision determines which process should run next. The scheduler may consider arrival time, priority, estimated CPU burst, or how long a process has waited.
The dispatcher performs the choice. It gives the selected process the CPU by:
- Switching from kernel mode to the process's user mode.
- Loading the process's saved registers and program counter.
- Jumping to the next instruction of that process.
The time needed for this transfer is called dispatch latency. A smaller latency improves responsiveness, especially in interactive systems.
| Component | Main job |
|---|---|
| Scheduler | Selects the next ready process |
| Dispatcher | Transfers CPU control to that process |
| Context switch | Saves one process state and restores another |
| Timer interrupt | Allows the kernel to regain control periodically |
Preemptive and Non-Preemptive Scheduling
In non-preemptive scheduling, a running process keeps the CPU until it terminates or blocks for an event such as input. The scheduler cannot normally take the CPU away from it.
In preemptive scheduling, the operating system can interrupt a running process and select another one. A hardware timer usually generates an interrupt after a fixed interval. The kernel saves the interrupted process's context, changes its state to ready, and invokes the scheduler.
Preemption improves response time and prevents one CPU-bound process from monopolizing the CPU. It also creates overhead. Every context switch uses CPU time without directly advancing user work. Very frequent switches can reduce throughput.
Tracing a Scheduling Decision
Suppose process P1 is running when its time quantum expires. The timer interrupt enters the kernel. The operating system saves P1's registers and program counter in its process control block. If P1 still has work, the kernel places it at the end of the ready queue. The scheduler selects another ready process, such as P2. The dispatcher restores P2's saved context, and P2 runs.
The same sequence can happen when a higher-priority process becomes ready. The difference is that the new process may immediately preempt the current process.
Common Errors
- Treating the scheduler and dispatcher as the same component.
- Forgetting that context-switch time contributes to overhead.
- Assuming every scheduling algorithm is preemptive.
- Moving a process to the waiting state after a timer interrupt. A timer normally moves it to ready; an input request moves it to waiting.
Exercise
P1 runs from time 0 to 4, when its quantum expires. P2 is ready, so it runs next. What event causes the scheduler to reconsider the running process, and what state does P1 enter?
Show answer
The timer interrupt causes reconsideration. P1 enters the ready state if it still has remaining CPU work.