Chapter 4: CPU Scheduling Algorithms
Scheduling, Resource Allocation & CPU Scheduling — Syllabus 9.3
1. What is CPU Scheduling?
CPU Scheduling is the process of selecting which process from the ready queue should be allocated the CPU next. The CPU can execute only one process at a time (on a single-core processor), while many processes may be waiting.
Example: Suppose four programs are open: Chrome, MS Word, Music Player, Calculator. Only one process can use the CPU at a particular instant. The OS decides: “Who gets the CPU first?”
Why CPU Scheduling is Needed
| Without Scheduling | With Scheduling |
|---|---|
| Some processes may wait forever | Fair CPU allocation |
| CPU may remain idle unnecessarily | Improved CPU utilization |
| System becomes slow and unfair | Better throughput and responsiveness |
| Scheduling Cycle |
|---|
| Process Created ↓ Ready Queue ↓ CPU Scheduler ↓ Running → Finished |
| Running ↓ Waiting (I/O) ↓ Ready Queue (cycle repeats) |
2. Scheduling Criteria
These parameters judge the quality of a scheduling algorithm.
Criteria & Formulas
| Criterion | Formula | Goal |
|---|---|---|
| CPU Utilization | Busy Time / Total Time × 100 |
✅ Maximum |
| Throughput | Completed Processes / Total Time |
✅ Maximum |
| Turnaround Time (TAT) | TAT = Completion Time (CT) − Arrival Time (AT) |
✅ Minimum |
| Waiting Time (WT) | WT = TAT − Burst Time (BT) |
✅ Minimum |
| Response Time (RT) | RT = First CPU Allocation − Arrival Time |
✅ Minimum |
📌 Memory Trick — CUTTR: C → CPU Utilization | U → Throughput | T → Turnaround Time | T → Waiting Time | R → Response Time
3. Types of Scheduling
Preemptive vs Non-Preemptive
| Feature | Non-Preemptive | Preemptive |
|---|---|---|
| CPU taken away? | No — process keeps CPU until done or blocked | Yes — OS can interrupt a running process |
| Complexity | Simple | More complex |
| Overhead | Lower | Higher |
| Responsiveness | Less responsive | Better responsiveness |
| Examples | FCFS, Non-preemptive SJF, Non-preemptive Priority | Round Robin, SRTF, Preemptive Priority |
4. First Come First Serve (FCFS)
The process that arrives first gets the CPU first. Rule: First In, First Out (FIFO).
Solved Example
| Process | Arrival Time (AT) | Burst Time (BT) |
|---|---|---|
| P1 | 0 | 4 |
| P2 | 1 | 3 |
| P3 | 2 | 2 |
| Gantt Chart | |
|---|---|
| P1 (0–4) | P2 (4–7) | P3 (7–9) | | Timeline: 0, 4, 7, 9 |
| Process | CT | TAT = CT − AT | WT = TAT − BT |
|---|---|---|---|
| P1 | 4 | 4 − 0 = 4 | 4 − 4 = 0 |
| P2 | 7 | 7 − 1 = 6 | 6 − 3 = 3 |
| P3 | 9 | 9 − 2 = 7 | 7 − 2 = 5 |
| Average | 5.67 | 2.67 |
| Advantages | Disadvantages |
|---|---|
| Simple and easy to implement | Poor average waiting time |
| Fair by arrival order | Suffers from Convoy Effect |
📌 Convoy Effect: A long process at the front makes all short processes wait unnecessarily.
5. Shortest Job First (SJF)
The process with the smallest CPU burst time executes first.
| Process | Burst Time |
|---|---|
| P1 | 8 |
| P2 | 4 |
| P3 | 2 |
| P4 | 1 |
Execution order: P4 → P3 → P2 → P1
| Advantages | Disadvantages |
|---|---|
| Minimum average waiting time (when burst times are known) | Burst time must be known or estimated in advance |
| Optimal for batch processing | Long processes may suffer starvation |
Starvation: A process waits indefinitely because shorter processes keep arriving and get priority.
6. Priority Scheduling
The process with the highest priority executes first. Common convention: lower number = higher priority.
| Process | Priority |
|---|---|
| P1 | 3 |
| P2 | 1 (highest) |
| P3 | 2 |
Execution order: P2 → P3 → P1
| Advantages | Disadvantages |
|---|---|
| Important tasks finish quickly | Low-priority processes may starve |
📌 Solution to Starvation — Aging: Gradually increase the priority of waiting processes over time. This prevents any process from waiting forever.
7. Round Robin (RR)
Each process receives a fixed amount of CPU time called the Time Quantum. If it does not finish within that time, it goes to the end of the ready queue.
| Process | Burst Time |
|---|---|
| P1 | 5 |
| P2 | 4 |
| P3 | 3 |
Time Quantum = 2 ms
Execution order: P1(2) → P2(2) → P3(2) → P1(2) → P2(2) → P3(1) → P1(1)
| Time Quantum Choice | Effect |
|---|---|
| Too small | Better responsiveness but excessive context switching overhead |
| Too large | Less overhead but behaves like FCFS |
| Advantages | Disadvantages |
|---|---|
| Fair to all processes | Too small quantum → excessive context switching |
| Good response time | Too large quantum → degrades to FCFS |
| Ideal for interactive / time-sharing systems | Higher average TAT than SJF |
8. Multilevel Queue Scheduling
Processes are permanently assigned to different queues based on their type. Each queue may use its own scheduling algorithm.
| Queue Level | Process Type | Algorithm |
|---|---|---|
| Queue 1 (Highest) | System processes | FCFS |
| Queue 2 | Interactive processes | Round Robin |
| Queue 3 (Lowest) | Batch processes | SJF |
Higher-priority queues are always served first. Lower queues execute only when upper queues are empty.
9. Multilevel Feedback Queue
Unlike Multilevel Queue, processes can move between queues based on their CPU behavior.
| Feature | Multilevel Queue | Multilevel Feedback Queue |
|---|---|---|
| Queue assignment | Permanent | Dynamic (can change) |
| Flexibility | Rigid | More flexible |
| Starvation | Possible | Rare (aging applied) |
| Usage | Simple systems | Most modern operating systems |
Algorithm Comparison
| Algorithm | Preemptive | Starvation | Best For | Easy to Implement |
|---|---|---|---|---|
| FCFS | No | No | Simple batch systems | Yes |
| SJF | Optional | Yes | Minimum avg wait (known BT) | Moderate |
| Priority | Optional | Yes (use aging) | Critical task systems | Yes |
| Round Robin | Yes | No | Interactive / time-sharing | Moderate |
| Multilevel Queue | Depends | Possible | Mixed workloads | Complex |
| Multilevel Feedback | Yes | Rare | Modern general-purpose OS | Complex |
Resource Allocation (Basic Idea)
Resource allocation means assigning system resources (CPU, Memory, Disk, Printer) to processes.
| Goal | Description |
|---|---|
| Fairness | Every process gets a chance |
| Efficiency | Resources are not wasted |
| Avoid Deadlocks | Prevent circular resource waiting |
| Maximize Utilization | Keep resources busy as much as possible |
Resource allocation strategies are explored in depth when studying deadlocks and synchronization.
Frequently Asked Exam Questions
| Question | Answer |
|---|---|
| Which algorithm is the simplest? | FCFS |
| Which algorithm gives minimum average waiting time? | SJF (when burst times are known) |
| Which algorithm is used in time-sharing systems? | Round Robin |
| Which algorithms may cause starvation? | SJF and Priority Scheduling |
| How can starvation be prevented? | Aging |
| What is the convoy effect? | Long process blocks shorter ones in FCFS |
| What is a time quantum? | Fixed CPU time slice given to each process in Round Robin |
Chapter 4 Summary
| Topic | Key Point |
|---|---|
| CPU Scheduling | Decides which process gets the CPU next |
| Main Criteria | CPU Utilization, Throughput, TAT, WT, RT |
| FCFS | Simple FIFO; suffers from convoy effect |
| SJF | Lowest average WT; may cause starvation |
| Priority Scheduling | Highest-priority runs first; aging prevents starvation |
| Round Robin | Time quantum; fair; ideal for interactive systems |
| Multilevel Queue | Fixed queue assignment per process type |
| Multilevel Feedback Queue | Processes can move between queues; used in modern OS |
Practice MCQs
-
1. Which scheduling algorithm follows FIFO?
A. Round Robin B. FCFS C. SJF D. Priority
✅ Answer: B — FCFS -
2. Which scheduling algorithm generally provides the minimum average waiting time?
A. FCFS B. Round Robin C. SJF D. Priority
✅ Answer: C — SJF -
3. Which scheduling algorithm is best suited for interactive systems?
A. FCFS B. SJF C. Round Robin D. Batch Scheduling
✅ Answer: C — Round Robin -
4. Which problem can occur in SJF and Priority Scheduling?
A. Deadlock B. Starvation C. Fragmentation D. Thrashing
✅ Answer: B — Starvation -
5. Which technique helps prevent starvation?
A. Paging B. Segmentation C. Aging D. Spooling
✅ Answer: C — Aging