Processes P1–P4 arrive at 0, 1, 2 and 3 ms with bursts of 5, 3, 8 and 6 ms. Draw the FCFS Gantt chart and find the average turnaround and waiting times.
CPU scheduling decides which ready process runs next. Exams test the criteria, the TAT/WT formulas and Gantt-chart numericals for FCFS, SJF, SRTF, priority and round robin, plus multilevel queues, MLFQ and HRRN.
Processes alternate between CPU bursts (computing) and I/O bursts (waiting for a device) — the CPU–I/O burst cycle. Most CPU bursts are short and a few are long: an I/O-bound process has many short bursts, a CPU-bound process a few long ones. Whenever the CPU falls idle, the short-term (CPU) scheduler selects a process from the ready queue and the dispatcher hands it the CPU. A scheduling decision may be needed at four moments — when:
If scheduling happens only in cases 1 and 4, the scheme is non-preemptive (cooperative): once a process has the CPU, it keeps it until it blocks or exits. If the OS can also reschedule in cases 2 and 3, it is preemptive. Windows (since Windows 95), macOS and Linux are preemptive. Preemption needs a hardware timer and creates a new danger — a process can be interrupted half-way through updating shared data, which is why synchronization is needed.
The dispatcher performs the context switch, switches to user mode and jumps to the right place in the chosen program. The time it needs to stop one process and start another is the dispatch latency; because the dispatcher runs on every switch, it must be as fast as possible.
| Criterion | Meaning | Goal |
|---|---|---|
| CPU utilisation | Percentage of time the CPU is busy (real systems range from about 40% when lightly loaded to 90% when heavily loaded) | Maximise |
| Throughput | Number of processes completed per unit time | Maximise |
| Turnaround time (TAT) | Time from a process's arrival (submission) to its completion | Minimise |
| Waiting time (WT) | Total time a process spends waiting in the ready queue | Minimise |
| Response time (RT) | Time from arrival until the process first gets the CPU (its first response) | Minimise — vital for interactive systems |
A scheduling algorithm cannot change how long a process computes or does I/O — only how long it waits in the ready queue. That is why average waiting time is the favourite single yardstick for comparing algorithms.
Every numerical follows one routine: (1) draw the Gantt chart, a timeline of which process holds the CPU when; (2) read off each completion time; (3) compute TAT and WT with the formulas; (4) average them. Try any process table of your own in the CPU scheduling Gantt tool on this page and check your hand calculations against it.
FCFS keeps the ready queue as a FIFO: the process that requests the CPU first gets it first and keeps it until it finishes or blocks. It is simple, never starves anyone and is fair in the queueing sense. But it is non-preemptive, so one long job holds up everybody behind it, and its average waiting time is often poor.
Its waiting time depends heavily on arrival order. Suppose three processes arrive together with bursts of 20, 2 and 2 ms. In the order (20, 2, 2) the waits are 0, 20 and 22 — an average of 14 ms. In the order (2, 2, 20) the waits are 0, 2 and 4 — an average of 2 ms. Same work, seven times the waiting.
This is the convoy effect: a CPU-bound process hogs the CPU while many I/O-bound processes pile up behind it and their devices sit idle; when it finally blocks, they all rush through the CPU and queue at the devices while the CPU idles. Both CPU and device utilisation suffer. FCFS is therefore unsuitable for time-sharing systems, where every user needs the CPU at regular intervals.
Shortest-job-first (SJF) gives the CPU to the process with the smallest next CPU burst; ties are broken FCFS. A more accurate name is shortest-next-CPU-burst. Moving a short job ahead of a long one reduces the short job's wait more than it increases the long job's, so SJF is provably optimal: no other algorithm gives a smaller average waiting time for a given set of processes.
SJF has two forms. Non-preemptive SJF lets the running process finish its burst. Preemptive SJF, called shortest-remaining-time-first (SRTF), compares a newly arrived process's burst with the remaining time of the running process and preempts if the newcomer is shorter. SRTF gives the minimum average waiting time when processes arrive at different times.
The catch is that the OS cannot know the length of the next CPU burst in advance. It predicts it from the process's history using an exponential average of the measured bursts:
Example with α = ½ and an initial guess τ₀ = 10 ms. Actual bursts of 6, 4, 6 and 4 ms give predictions τ₁ = ½·6 + ½·10 = 8, τ₂ = ½·4 + ½·8 = 6, τ₃ = ½·6 + ½·6 = 6 and τ₄ = ½·4 + ½·6 = 5. Each older burst's weight halves at every step, so the estimate follows recent behaviour.
Priority scheduling gives each process a priority number and runs the highest-priority ready process; equal priorities are served FCFS. Priorities can be internal (computed from measurable quantities such as time limits, memory needs or the ratio of I/O to CPU burst) or external (importance of the user, fees paid, department). SJF is simply priority scheduling in which the priority is the inverse of the predicted next CPU burst.
Priority scheduling may be preemptive — a newly arrived higher-priority process immediately takes the CPU — or non-preemptive, in which case the newcomer just goes to the head of the ready queue.
Its major problem is starvation (indefinite blocking): a steady stream of high-priority work can keep a low-priority process waiting for ever. Folklore says that when the IBM 7094 at MIT was shut down in 1973, a low-priority job submitted in 1967 had still not run. The cure is aging: gradually raise the priority of processes that have waited a long time — say by 1 every 15 minutes — so that even the lowest eventually reaches the top.
Round robin (RR) is FCFS plus preemption, designed for time-sharing. Each process receives a small unit of CPU time, the time quantum or time slice q (typically 10–100 ms). The ready queue is treated as circular: the scheduler dispatches the process at the head and sets a timer for q. If the process is still running when the timer fires, it is preempted and put at the tail of the queue; if its burst ends sooner, it releases the CPU voluntarily.
With n processes in the ready queue, each gets 1/n of the CPU in chunks of at most q, and no process waits more than (n − 1)·q before its next quantum. That guaranteed bound on response time is why RR is the textbook answer for interactive and time-sharing systems.
Average turnaround time does not necessarily improve as q grows or shrinks; it improves when most processes finish their burst within a single quantum. RR usually has a higher average turnaround time than SJF, but a much better response time.
A multilevel queue scheduler partitions the ready queue into several separate queues and assigns each process permanently to one of them according to its type — for example system processes, interactive processes, interactive editing processes, batch processes and student processes. Each queue has its own algorithm (say RR for the foreground queue and FCFS for the background queue). Between queues there is either fixed-priority preemptive scheduling (no batch process runs while an interactive one is ready) or time slicing among the queues (for example 80% of CPU time to foreground RR and 20% to background FCFS). Fixed priority between queues can starve the lower queues.
A multilevel feedback queue (MLFQ) lets processes move between queues according to their behaviour; it was first used in MIT's CTSS (Corbató, 1962). A typical set-up: queue 0 with q = 8 ms, queue 1 with q = 16 ms, queue 2 FCFS. A new process enters queue 0; if it uses up its whole quantum it is demoted to queue 1, and later to queue 2. Short interactive (I/O-bound) processes therefore stay at high priority while long CPU-bound ones sink. A process that has waited too long in a low queue is promoted — aging, which prevents starvation. An MLFQ is defined by five parameters:
That flexibility makes MLFQ the most general — and the hardest to tune — of the classic schemes. Highest response ratio next (HRRN), proposed by Per Brinch Hansen in 1971, is a non-preemptive policy that, whenever the CPU becomes free, chooses the process with the largest response ratio:
Real systems refine these ideas. Linux's Completely Fair Scheduler (CFS, kernel 2.6.23, 2007) keeps runnable tasks in a red-black tree ordered by virtual runtime and runs the one that has had the least; it was succeeded by the EEVDF scheduler in kernel 6.6 (2023). Windows uses preemptive priority scheduling with 32 levels (16–31 form the real-time class). Multiprocessor schedulers add processor affinity (keep a process on the core whose cache it has warmed) and load balancing by push and pull migration.
| Algorithm | Preemptive? | Starvation? | Strength | Weakness |
|---|---|---|---|---|
| FCFS | No | No | Simplest; strict arrival order | Convoy effect; poor average waiting time |
| SJF | No | Yes, for long jobs | Minimum average waiting time when all jobs are present | Needs the next burst length (prediction) |
| SRTF | Yes | Yes, for long jobs | Minimum average waiting time with arrivals | Prediction plus extra context switches |
| Priority | Either | Yes, for low priority (cure: aging) | Expresses importance | Starvation; priority inversion |
| Round robin | Yes | No | Bounded response time; ideal for time-sharing | Overhead if q is small; FCFS if q is large |
| Multilevel queue | Usually | Yes, for lower queues | A different policy per class of process | Inflexible — processes never change queue |
| MLFQ | Yes | No, if aging is used | Adapts to behaviour; favours interactive jobs | Many parameters to tune |
| HRRN | No | No | Balances short jobs against waiting time | Needs burst estimates |
To evaluate algorithms, deterministic modelling takes one fixed workload and computes each algorithm's performance — exactly what Gantt-chart numericals do. Queueing models use results such as Little's formula, n = λ × W (average queue length = arrival rate × average waiting time). Simulations and real implementations give the most realistic picture, at the highest cost.
Try the problem first, then reveal the working one step at a time.
Processes P1–P4 arrive at 0, 1, 2 and 3 ms with bursts of 5, 3, 8 and 6 ms. Draw the FCFS Gantt chart and find the average turnaround and waiting times.
P1 (AT 0, BT 8), P2 (AT 1, BT 4), P3 (AT 2, BT 2), P4 (AT 3, BT 5). Find the average waiting time under non-preemptive SJF and under SRTF.
Lower number = higher priority. P1 (AT 0, BT 4, priority 3), P2 (AT 1, BT 3, priority 1), P3 (AT 2, BT 5, priority 4), P4 (AT 3, BT 2, priority 2). Find the average waiting time for preemptive and for non-preemptive priority scheduling.
P1 (AT 0, BT 5), P2 (AT 1, BT 4), P3 (AT 2, BT 2), P4 (AT 4, BT 1); q = 2 ms. A process that arrives when a quantum expires joins the queue before the preempted process. Find the average turnaround, waiting and response times.
Using HRRN, schedule P1 (AT 0, BT 3), P2 (AT 1, BT 6), P3 (AT 3, BT 4), P4 (AT 5, BT 2) and P5 (AT 6, BT 5). Give the execution order and the average waiting time.
With α = 0.5 and τ₀ = 12 ms, a process's first three CPU bursts are 8, 6 and 10 ms. Find τ₁, τ₂ and τ₃. What would τ₁ be with α = 0.25?
P1, P2 and P3 arrive at time 0 (in that order) with bursts of 10, 4 and 2 ms. Find the average waiting time for round robin with q = 1, q = 3 and q = 10, and count the context switches in each case.
Under SRTF, P1 (AT 0, BT 12), P2 (AT 2, BT 4), P3 (AT 3, BT 6) and P4 (AT 8, BT 5) are scheduled. How many context switches occur (ignore the first dispatch and the final exit), and what is the average waiting time?
An MLFQ has Q0 (RR, q = 8 ms), Q1 (RR, q = 16 ms) and Q2 (FCFS). A CPU-bound process needing 30 ms arrives when the system is otherwise idle. Trace its path. What changes for a process that needs only 5 ms?
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.