CSC 394 · Operating Systems
Our notes
2 min readWhy schedule
When several processes are ready, the short-term scheduler chooses which runs next. Preemptive scheduling can take the CPU away from a running process; non-preemptive lets it run until it finishes or blocks.
Criteria
- CPU utilisation (keep it busy) and throughput (jobs finished per unit time): maximise.
- Turnaround time = completion time − arrival time: minimise.
- Waiting time = turnaround time − burst time (time spent in the ready queue): minimise.
- Response time = first run − arrival: minimise (important for interactive systems).
Example data (used below)
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
First-Come, First-Served (FCFS)
Non-preemptive, in arrival order. Gantt: P1 0-8 | P2 8-12 | P3 12-21 | P4 21-26.
Waiting: P1 0, P2 8 − 1 = 7, P3 12 − 2 = 10, P4 21 − 3 = 18. Average = 35/4 = 8.75.
Simple, but short jobs wait behind long ones (convoy effect).
Shortest Job First (SJF, non-preemptive)
When the CPU is free, pick the ready job with the smallest burst. At 0 only P1 is there: P1 0-8. At 8, ready: P2 (4), P3 (9), P4 (5) → P2 8-12, P4 12-17, P3 17-26.
Waiting: P1 0, P2 7, P4 12 − 3 = 9, P3 17 − 2 = 15. Average = 31/4 = 7.75.
Shortest Remaining Time First (SRTF, preemptive SJF)
At every arrival, run the job with the least time left.
- 0: P1 starts. 1: P2 arrives (4) < P1's remaining 7 → switch to P2.
- P2 runs 1-5 (P3 at 2 with 9, P4 at 3 with 5 do not beat P2's remaining time).
- 5: remaining P1 7, P3 9, P4 5 → P4 5-10; then P1 10-17; then P3 17-26.
Gantt: P1 0-1 | P2 1-5 | P4 5-10 | P1 10-17 | P3 17-26.
Waiting (turnaround − burst): P1 (17 − 0) − 8 = 9; P2 (5 − 1) − 4 = 0; P3 (26 − 2) − 9 = 15; P4 (10 − 3) − 5 = 2. Average = 26/4 = 6.5.
SJF/SRTF give the lowest average waiting time, but burst lengths must be predicted and long jobs may starve.
Priority scheduling
Run the highest priority first (often the smallest number = highest). Problem: starvation of low-priority jobs. Fix: ageing (raise priority the longer a job waits).
Round Robin (RR)
Each process gets a time quantum (say q = 4); if not finished it goes to the back of the ready queue. Good for time-sharing.
With q = 4 on the data above:
- P1 0-4 (4 left). Queue: P2, P3, P4, P1 (P2, P3, P4 arrived during 0-4, before P1 rejoins).
- P2 4-8 (done). P3 8-12 (5 left). P4 12-16 (1 left). P1 16-20 (done). P3 20-24 (1 left). P4 24-25 (done). P3 25-26 (done).
Completion: P1 20, P2 8, P3 26, P4 25.
Waiting: P1 20 − 0 − 8 = 12; P2 8 − 1 − 4 = 3; P3 26 − 2 − 9 = 15; P4 25 − 3 − 5 = 17. Average = 47/4 = 11.75.
A very large quantum turns RR into FCFS; a very small one causes too many context switches.
Multilevel queues
Separate queues (system, interactive, batch) each with its own algorithm; multilevel feedback queues move a process between queues by its behaviour (CPU-heavy jobs sink to lower priority).
Exams usually ask: define the criteria; draw Gantt charts and compute average waiting and turnaround times for FCFS, SJF, SRTF, priority and RR; preemptive vs non-preemptive; starvation and ageing; effect of quantum size.
Common mistake: forgetting to subtract the arrival time when computing waiting and turnaround times.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick OSTEP: Scheduling: Introduction
Free chapter PDF from the authors' site (115 KB).
(opens pages.cs.wisc.edu in a new tab)
-
OSTEP: Scheduling: The Multi-Level Feedback Queue
Free chapter PDF from the authors' site (112 KB).
(opens pages.cs.wisc.edu in a new tab)
-
Scheduling (computing)
(opens en.wikipedia.org in a new tab)
Wikipedia, Scheduling (computing), CC BY-SA 4.0 · licence
Watch
-
Introduction to CPU Scheduling
10 min · ≈ 45 MB · about ₦25
(opens youtube.com in a new tab)
-
Scheduling Algorithms - First Come First Served (FCFS)
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
Scheduling Algorithms - Shortest Job First (SJF)
29 min · ≈ 131 MB · about ₦74
(opens youtube.com in a new tab)
-
Round Robin Scheduling - Solved Problem (Part 1)
20 min · ≈ 90 MB · about ₦51
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.