Skip to main content

CPU Scheduling

The scheduler decides which ready process runs next. Goals: maximise CPU utilisation and throughput, minimise waiting and response time, stay fair.

Key metrics​

TermMeaning
Arrival timeWhen the process enters the ready queue
Burst timeCPU time required
Turnaround timeCompletion time − arrival time
Waiting timeTurnaround time − burst time
Response timeFirst run time − arrival time

Algorithms​

AlgorithmIdeaPre-emptiveDrawback
FCFSFirst come, first servedNoConvoy effect (short jobs wait behind long ones)
SJFShortest burst firstNoStarvation of long jobs; needs burst estimates
SRTFShortest remaining time firstYesStarvation, frequent switches
Round RobinEach process gets a time quantumYesQuantum too small = overhead; too large = FCFS
PriorityHighest priority firstEitherStarvation (fix with aging)
MLFQSeveral queues; jobs move between themYesComplex to tune

Round Robin example​

Processes P1 (burst 5), P2 (3), P3 (2), all arriving at time 0, quantum = 2.

Gantt: | P1 | P2 | P3 | P1 | P2 | P1 |
0 2 4 6 8 9 10

Completion times: P3 = 6, P2 = 9, P1 = 10. Average waiting time = ((10−5) + (9−3) + (6−2)) / 3 = 5.

A tiny simulator​

round_robin.py
from collections import deque

def round_robin(bursts, quantum):
queue = deque(bursts.items()) # (name, remaining)
time, finish = 0, {}
while queue:
name, left = queue.popleft()
run = min(quantum, left)
time += run
if left - run:
queue.append((name, left - run))
else:
finish[name] = time
return finish

print(round_robin({"P1": 5, "P2": 3, "P3": 2}, 2))
# {'P3': 6, 'P2': 9, 'P1': 10}