AiTechWorlds
AiTechWorlds
Every service queue has a policy. A deli counter says "take a number" — first come, first served. A dentist might schedule the shortest appointment first to maximize patient flow. A co-working space gives everyone a 10-minute slot on the printer, then rotates. A VIP lounge sends priority members to the front.
CPU scheduling algorithms are those queue policies applied to processes competing for the CPU. Each solves a different problem, and each introduces its own tradeoffs.
Policy: The process that arrives first gets the CPU first. Non-preemptive — once a process starts, it runs to completion (or until it blocks on I/O).
| Process | Arrival Time | Burst Time |
|---|---|---|
| P1 | 0 | 24 ms |
| P2 | 0 | 3 ms |
| P3 | 0 | 3 ms |
Gantt Chart (arrival order: P1, P2, P3):
Waiting Times:
The major problem with FCFS: if a long CPU-bound process arrives first, all short processes pile up behind it. This is the convoy effect — short jobs are stuck behind a slow "convoy leader," just like cars behind a slow truck on a single-lane road. Average waiting time skyrockets.
Policy: Always run the process with the shortest next CPU burst. Provably optimal for minimizing average waiting time.
Same processes, but now scheduled shortest-first:
| Process | Burst Time |
|---|---|
| P2 | 3 ms |
| P3 | 3 ms |
| P1 | 24 ms |
Gantt Chart:
Waiting Times:
SJF requires knowing the next CPU burst length in advance. Processes do not declare their burst time. In practice, the OS predicts it using an exponential average of past burst lengths:
τ(n+1) = α × t(n) + (1−α) × τ(n)
Where t(n) is the actual last burst, τ(n) is the previous prediction, and α (typically 0.5) controls how much weight recent history gets.
If a new process arrives with a shorter burst than the currently running process, the scheduler preempts the current process and switches to the shorter one. This further minimizes average waiting time but adds context-switch overhead.
Policy: Each process gets a fixed time quantum (slice) of CPU time, then is preempted and placed at the back of the ready queue. Fair, preemptive, and designed for interactive systems.
Processes: P1 (burst=24), P2 (burst=3), P3 (burst=3), P4 (burst=12)
All arrive at time 0.
Wait — let's trace carefully:
Effect of Time Quantum Size:
| Quantum | Behavior |
|---|---|
| Very small (1 ms) | High responsiveness, but 90%+ overhead in context switches |
| Very large (∞) | Degenerates to FCFS |
| Optimal (10–100 ms) | Balance of responsiveness and efficiency |
Rule of thumb: 80% of CPU bursts should be shorter than the time quantum.
Policy: Each process has a priority number. The CPU is always given to the highest-priority process. Can be preemptive or non-preemptive. Convention varies: some systems use lower number = higher priority (Linux, Unix); others use higher number = higher priority.
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 10 ms | 3 |
| P2 | 1 ms | 1 (highest) |
| P3 | 2 ms | 4 |
| P4 | 1 ms | 5 (lowest) |
| P5 | 5 ms | 2 |
Gantt Chart (non-preemptive, all arrive at t=0):
Average Waiting Time: (0 + 1 + 6 + 16 + 18) / 5 = 8.2 ms
If high-priority processes continuously arrive, low-priority processes may never run. A process submitted on Monday might still be waiting on Friday. This is starvation.
Solution: Aging. Gradually increase the priority of waiting processes over time. A process waiting for 15 minutes gets a priority boost. After long enough waiting, even a low-priority process becomes high-priority and eventually runs.
| Algorithm | Preemptive? | Starvation? | Optimal For | Weakness |
|---|---|---|---|---|
| FCFS | No | No | Batch systems | Convoy effect, poor avg wait |
| SJF | No | Yes (long jobs) | Minimizing avg wait | Needs burst prediction |
| SRTF | Yes | Yes (long jobs) | Minimum avg wait | High overhead |
| Round Robin | Yes | No | Interactive/time-sharing | Quantum tuning needed |
| Priority | Both | Yes (low priority) | Mixed workloads | Starvation without aging |
In practice, no real OS uses a single pure algorithm. Linux's CFS (Completely Fair Scheduler) and Windows' multilevel priority scheduler both combine elements of Round Robin, priority scheduling, and dynamic priority adjustment to handle the diversity of real workloads.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises