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 SchedulingWith Scheduling
Some processes may wait foreverFair CPU allocation
CPU may remain idle unnecessarilyImproved CPU utilization
System becomes slow and unfairBetter 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

CriterionFormulaGoal
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

FeatureNon-PreemptivePreemptive
CPU taken away?No — process keeps CPU until done or blockedYes — OS can interrupt a running process
ComplexitySimpleMore complex
OverheadLowerHigher
ResponsivenessLess responsiveBetter responsiveness
ExamplesFCFS, Non-preemptive SJF, Non-preemptive PriorityRound 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

ProcessArrival Time (AT)Burst Time (BT)
P104
P213
P322
Gantt Chart
| P1 (0–4) | P2 (4–7) | P3 (7–9) |Timeline: 0, 4, 7, 9
ProcessCTTAT = CT − ATWT = TAT − BT
P144 − 0 = 44 − 4 = 0
P277 − 1 = 66 − 3 = 3
P399 − 2 = 77 − 2 = 5
Average5.672.67
AdvantagesDisadvantages
Simple and easy to implementPoor average waiting time
Fair by arrival orderSuffers 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.

ProcessBurst Time
P18
P24
P32
P41

Execution order: P4 → P3 → P2 → P1

AdvantagesDisadvantages
Minimum average waiting time (when burst times are known)Burst time must be known or estimated in advance
Optimal for batch processingLong 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.

ProcessPriority
P13
P21 (highest)
P32

Execution order: P2 → P3 → P1

AdvantagesDisadvantages
Important tasks finish quicklyLow-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.

ProcessBurst Time
P15
P24
P33

Time Quantum = 2 ms

Execution order: P1(2) → P2(2) → P3(2) → P1(2) → P2(2) → P3(1) → P1(1)

Time Quantum ChoiceEffect
Too smallBetter responsiveness but excessive context switching overhead
Too largeLess overhead but behaves like FCFS
AdvantagesDisadvantages
Fair to all processesToo small quantum → excessive context switching
Good response timeToo large quantum → degrades to FCFS
Ideal for interactive / time-sharing systemsHigher 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 LevelProcess TypeAlgorithm
Queue 1 (Highest)System processesFCFS
Queue 2Interactive processesRound Robin
Queue 3 (Lowest)Batch processesSJF

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.

FeatureMultilevel QueueMultilevel Feedback Queue
Queue assignmentPermanentDynamic (can change)
FlexibilityRigidMore flexible
StarvationPossibleRare (aging applied)
UsageSimple systemsMost modern operating systems

Algorithm Comparison

AlgorithmPreemptiveStarvationBest ForEasy to Implement
FCFSNoNoSimple batch systemsYes
SJFOptionalYesMinimum avg wait (known BT)Moderate
PriorityOptionalYes (use aging)Critical task systemsYes
Round RobinYesNoInteractive / time-sharingModerate
Multilevel QueueDependsPossibleMixed workloadsComplex
Multilevel FeedbackYesRareModern general-purpose OSComplex

Resource Allocation (Basic Idea)

Resource allocation means assigning system resources (CPU, Memory, Disk, Printer) to processes.

GoalDescription
FairnessEvery process gets a chance
EfficiencyResources are not wasted
Avoid DeadlocksPrevent circular resource waiting
Maximize UtilizationKeep resources busy as much as possible

Resource allocation strategies are explored in depth when studying deadlocks and synchronization.


Frequently Asked Exam Questions

QuestionAnswer
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

TopicKey Point
CPU SchedulingDecides which process gets the CPU next
Main CriteriaCPU Utilization, Throughput, TAT, WT, RT
FCFSSimple FIFO; suffers from convoy effect
SJFLowest average WT; may cause starvation
Priority SchedulingHighest-priority runs first; aging prevents starvation
Round RobinTime quantum; fair; ideal for interactive systems
Multilevel QueueFixed queue assignment per process type
Multilevel Feedback QueueProcesses 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