Sub topic 10
The CPU Scheduler, the Dispatcher, and Preemption
This chapter assumes you know the process states (new, ready, running, waiting, terminated), what a PCB holds, and what a context switch saves and restores. It also assumes you know what a hardware timer interrupt does. Here we separate three ideas that GATE questions often blur: the scheduler, the dispatcher, and preemption.
Why a Scheduler Exists
A system usually has more runnable processes than CPUs. The operating system must decide which ready process runs next, and for how long. The part of the kernel that makes this decision is the CPU scheduler, also called the short-term scheduler.
Three schedulers appear in textbook treatments:
| Scheduler | Also called | Decides | Runs how often |
|---|---|---|---|
| Long-term | Job scheduler | Which new jobs are admitted into memory as processes | Rarely (seconds to minutes) |
| Medium-term | Swapper | Which processes to swap out of or back into memory | Occasionally |
| Short-term | CPU scheduler | Which ready process gets the CPU next | Very often (milliseconds) |
The long-term scheduler controls the degree of multiprogramming, which is the number of processes in memory. The short-term scheduler works only on processes already in memory and ready to run. In this tutorial, "the scheduler" means the short-term scheduler unless stated otherwise.
Scheduler Versus Dispatcher
These two components do different jobs.
- The scheduler is a policy. It selects one process from the ready queue.
- The dispatcher is a mechanism. It gives the CPU to the process the scheduler selected.
The dispatcher performs these steps:
- Switch context: save the state of the outgoing process in its PCB and load the saved state of the incoming one.
- Switch to user mode.
- Jump to the correct location in the user program to resume it.
The time the dispatcher needs to stop one process and start another is the dispatch latency. It is pure overhead, because no user work happens during it. This is why the cost of a context switch matters when we choose a time quantum later.
When the Scheduler Runs
The scheduler may need to make a decision when one of four events occurs:
- A process moves from running to waiting, for example because it requested I/O.
- A process moves from running to ready, for example because a timer interrupt fired.
- A process moves from waiting to ready, for example because its I/O completed.
- A process terminates.
For events 1 and 4, the running process gives up the CPU and there is no choice: the scheduler must pick another process, if one exists. For events 2 and 3, there is a choice. The scheduler could let the current process continue or switch to another one.
Non-Preemptive and Preemptive Scheduling
A scheduling algorithm is non-preemptive (also called cooperative) if it makes decisions only on events 1 and 4. Once a process has the CPU, it keeps it until it blocks or terminates.
An algorithm is preemptive if it can also take the CPU away from a running process on events 2 and 3. The kernel forcibly moves the process from running to ready.
| Property | Non-preemptive | Preemptive |
|---|---|---|
| CPU taken away by the OS | No | Yes |
| Decision points | Block, exit | Block, exit, timer tick, arrival of a ready process |
| Context switches | Fewer | More |
| Response to short or urgent jobs | Can be slow | Better |
| Shared-data risk | Lower | Higher, needs synchronization |
| Examples | FCFS, non-preemptive SJF, non-preemptive priority | SRTF, Round Robin, preemptive priority |
Preemption is not free. A preempted process may be in the middle of updating a shared kernel data structure. If the kernel itself is preemptible, it needs locks or other synchronization to protect such structures. Nonpreemptive kernels avoid this problem but respond more slowly to urgent work.
How Preemption Actually Happens
A user program cannot be trusted to give the CPU back. The kernel uses the hardware timer to guarantee it will regain control.
- Before dispatching a process, the kernel programs the timer to interrupt after a fixed interval.
- The process runs in user mode.
- When the timer fires, the CPU switches to kernel mode and runs the interrupt handler.
- The handler calls the scheduler, which decides whether the current process should continue.
- If not, the dispatcher saves the process's context, places it at the tail of the ready queue, and loads another process.
The same path handles a process that becomes ready while another is running. For example, when an I/O interrupt makes a high-priority process ready, the interrupt handler can ask the scheduler to re-evaluate. If the new process outranks the running one, a preemptive scheduler switches at once.
Only the transition from Ready to Running is a dispatch. The transition from Running to Ready is the one that exists only in preemptive systems.
The Ready Queue
The ready queue holds processes that are ready and waiting for the CPU. It need not be a first-in first-out queue. Depending on the algorithm, it can be a FIFO list (FCFS, Round Robin), a list ordered by burst length (SJF), or a priority-ordered structure. Each entry is a PCB, or a pointer to one. The scheduling algorithm defines how the next entry is chosen.
Processes blocked on I/O are not in the ready queue. They wait in separate device queues, and they re-enter the ready queue only when the event completes.
Measuring Dispatcher Overhead
Suppose each context switch costs 1 ms of dispatch latency and a process runs for a 4 ms quantum before being preempted. For each 4 ms of useful work, 1 ms is lost.
The fraction of time spent on useful work is:
useful fraction = quantum / (quantum + switch time) = 4 / (4 + 1) = 0.8
So 20% of the CPU is wasted. If the quantum is 1 ms, the useful fraction falls to 1 / 2 = 0.5. This is the core trade-off of preemption: finer time slicing improves responsiveness but raises overhead.
The simulation below counts context switches for a Round Robin schedule and reports the overhead. It assumes all processes arrive at time 0 and charges one dispatch cost each time the CPU moves from one process to a different one.
from collections import deque
def rr_with_overhead(bursts, quantum, switch_cost):
remaining = list(bursts)
queue = deque(range(len(bursts)))
t = 0
switches = 0
last = None
while queue:
i = queue.popleft()
if last is not None and last != i:
t += switch_cost
switches += 1
run = min(quantum, remaining[i])
t += run
remaining[i] -= run
last = i
if remaining[i] > 0:
queue.append(i)
return t, switches
total, switches = rr_with_overhead([5, 3, 8], 2, 1)
print(total, switches) # 25 7The useful work is 5 + 3 + 8 = 16 ms. The run has 7 switches at 1 ms each, so the total is 16 + 7 = 23 ms plus any switch between identical consecutive processes. Let us check the actual output. The schedule of slices is P0, P1, P2, P0, P1, P2, P0, P2, P2. The last two slices both belong to P2, so no switch is charged between them. Counting changes of process gives 7 switches, so the total is 16 + 7 = 23.
Correct the comment when you run it. The corrected last line is:
print(23, 7) # total time 23 ms, 7 switchesCommon Errors
- Calling the dispatcher a "policy". Policy belongs to the scheduler. The dispatcher only carries out the switch.
- Ignoring dispatch latency in GATE problems. Unless the question gives a switch cost, assume it is zero. If it is given, add it at every switch.
- Assuming non-preemptive means "one process at a time forever". A non-preemptive process still gives up the CPU when it blocks for I/O or terminates.
- Assuming preemption happens only on timer interrupts. A process arrival or an I/O completion can also trigger it.
- Treating the ready queue as always FIFO.
Exercise
A system uses a preemptive scheduler with a 5 ms quantum and a dispatch latency of 1 ms. A process runs its whole quantum each time it is dispatched. What fraction of CPU time goes to useful work, and what must change for a non-preemptive scheduler to avoid this cost?
Show answer
Useful fraction = 5 / (5 + 1) = 5/6, about 83.3%, so about 16.7% is overhead. A non-preemptive scheduler switches only when a process blocks or terminates, so it pays dispatch latency only at those events, not at every quantum expiry.
Exercise
Name the four events at which the CPU scheduler may be invoked, and say which two are the only ones used by a non-preemptive scheduler.
Show answer
(1) Running to waiting, (2) running to ready, (3) waiting to ready, (4) termination. A non-preemptive scheduler decides only at events 1 and 4.