Sub topic 11
Scheduling Criteria: Turnaround, Waiting, Response Time
This chapter assumes you know what the CPU scheduler and dispatcher do, what the ready queue is, and the difference between preemptive and non-preemptive scheduling. It also assumes you can read a Gantt chart, which is a timeline showing which process holds the CPU during each interval. Here you learn the measurements used to compare scheduling algorithms. The later chapters on FCFS, SJF, Round Robin and priority scheduling all use them.
Why We Need Criteria
No scheduling algorithm is best on every measure. An algorithm that keeps the CPU busy may make interactive users wait. An algorithm that answers users quickly may waste time on switching. To compare algorithms, we need numbers, and these are the usual ones.
| Criterion | Meaning | Goal |
|---|---|---|
| CPU utilization | Fraction of time the CPU does useful work | Maximize |
| Throughput | Processes completed per unit time | Maximize |
| Turnaround time | Arrival until completion | Minimize |
| Waiting time | Time spent in the ready queue | Minimize |
| Response time | Arrival until first CPU time | Minimize |
CPU utilization and throughput describe the whole system. Turnaround, waiting and response time describe each process, and we usually report their averages. GATE questions almost always ask for these three.
The Per-Process Quantities
For each process, we need four inputs and outputs:
- Arrival time (AT) is when the process enters the ready queue.
- Burst time (BT) is the total CPU time the process needs.
- Completion time (CT) is when the process finishes.
- First-run time (FR) is when the process first gets the CPU.
From these we derive the three criteria.
- Turnaround time:
TAT = CT - AT - Waiting time:
WT = TAT - BT - Response time:
RT = FR - AT
Turnaround time is the whole stay in the system. Waiting time is that stay minus the time spent running. Response time covers only the wait before the first run.
A Worked Example with FCFS
Take four processes under First-Come First-Served, which runs them in arrival order without preemption.
| Process | AT | BT |
|---|---|---|
| P1 | 0 | 5 |
| P2 | 1 | 3 |
| P3 | 2 | 8 |
| P4 | 3 | 2 |
The Gantt chart is P1 from 0 to 5, P2 from 5 to 8, P3 from 8 to 16, and P4 from 16 to 18.
| Process | CT | TAT = CT - AT | WT = TAT - BT | RT = FR - AT |
|---|---|---|---|---|
| P1 | 5 | 5 | 0 | 0 |
| P2 | 8 | 7 | 4 | 4 |
| P3 | 16 | 14 | 6 | 6 |
| P4 | 18 | 15 | 13 | 13 |
The averages are as follows.
- Average TAT is (5 + 7 + 14 + 15) / 4 = 10.25.
- Average WT is (0 + 4 + 6 + 13) / 4 = 5.75.
- Average RT is also 5.75.
Waiting time equals response time here because FCFS never preempts. Once a process starts, it runs to completion, so its only wait comes before the first run. With preemption the two diverge.
Computing the Metrics in Code
The script below reads a Gantt chart, given as (process, start, end) segments, and computes every metric. It works for preemptive schedules too, because it takes the first start and the last end of each process. The second schedule is Round Robin with quantum 2, where P1 has burst 5 and P2 has burst 3, both arriving at time 0.
def metrics(procs, gantt):
"""procs: {name: (arrival, burst)}; gantt: [(name, start, end), ...]"""
first, last = {}, {}
for name, start, end in gantt:
first.setdefault(name, start)
last[name] = end
rows = {}
for name, (at, bt) in procs.items():
tat = last[name] - at
rows[name] = {"CT": last[name], "TAT": tat,
"WT": tat - bt, "RT": first[name] - at}
return rows
def report(procs, gantt):
rows = metrics(procs, gantt)
for name, r in rows.items():
print(name, r)
for key in ("TAT", "WT", "RT"):
avg = sum(r[key] for r in rows.values()) / len(rows)
print("average", key, avg)
print()
report({"P1": (0, 5), "P2": (1, 3), "P3": (2, 8), "P4": (3, 2)},
[("P1", 0, 5), ("P2", 5, 8), ("P3", 8, 16), ("P4", 16, 18)])
report({"P1": (0, 5), "P2": (0, 3)},
[("P1", 0, 2), ("P2", 2, 4), ("P1", 4, 6), ("P2", 6, 7), ("P1", 7, 8)])For the Round Robin schedule, P1 has CT 8, TAT 8, WT 3 and RT 0. P2 has CT 7, TAT 7, WT 4 and RT 2. P2 waited 4 units in total but only 2 before its first run, so here waiting time and response time differ.
Relationships Worth Memorizing
WT = TAT - BTalways holds under the standard definition, so you only need two of the three numbers to find the third.RT <= WTfor every process, because response time counts only the first part of the waiting.RT = WTfor non-preemptive algorithms.- The sum of all burst times is fixed. If the CPU never idles, the last completion time is the same for every non-preemptive order, so only the distribution of waiting changes.
- Average TAT and average WT differ by exactly the average burst time. Minimizing one therefore minimizes the other.
System-Wide Criteria
CPU utilization is the percentage of time the CPU is busy running processes. In a real system it ranges from about 40 percent for a lightly loaded machine to 90 percent for a heavily loaded one. Context-switch time counts as overhead, so it lowers useful utilization.
Throughput is the number of processes completed per time unit. In the FCFS example, 4 processes finish in 18 units, so throughput is 4 / 18, about 0.22 processes per unit. Long processes reduce throughput, and short ones raise it.
Some textbooks add fairness (each process gets a reasonable share) and a variance criterion. For interactive systems, minimizing the variance of response time can matter more than minimizing its average, because users prefer predictable delays.
Common Errors
- Forgetting arrival times. If P2 arrives at time 1 and first runs at time 5, its response time is 4, not 5.
- Using the last start instead of the first. In a preemptive schedule, response time uses the first segment only.
- Counting CPU idle time as waiting. If the CPU idles from time 0 to 4 because nothing has arrived, nobody is waiting during that gap. Measure from each process's own arrival time.
- Ignoring I/O. The formula
WT = TAT - BTassumes the burst is the only time the process was not waiting. If a process also does I/O, that time is subtracted nowhere, so the formula reports ready-queue plus I/O time as waiting. GATE questions usually state which convention to use. When they give I/O bursts, subtract all CPU and I/O time only if the question asks for ready-queue waiting. - Averaging the wrong way. Compute each process's value first, then average. Do not subtract average arrival from average completion unless you check that it gives the same answer. It does, because subtraction distributes over a sum, but mistakes in the intermediate steps are common.
Choosing Between Criteria
Different systems weight these measures differently.
- Batch systems care about throughput and turnaround time, since nobody waits at a terminal.
- Interactive systems care about response time, because a user is watching the screen.
- Real-time systems care about meeting deadlines, which no average captures.
This is why the next chapters compare algorithms on several criteria rather than naming a single winner.
Exercise
A process arrives at time 2 with a CPU burst of 4. It runs for the first time at time 7, is preempted, and completes at time 15. Find its turnaround time, waiting time and response time.
Show answer
TAT = CT - AT = 15 - 2 = 13. WT = TAT - BT = 13 - 4 = 9. RT = FR - AT = 7 - 2 = 5. Note that RT (5) is less than WT (9), as expected for a preemptive schedule.
Exercise
Three processes with bursts 6, 2 and 4 all arrive at time 0 and run in that order under FCFS. Find the average waiting time and the average turnaround time, and confirm that they differ by the average burst time.
Show answer
The Gantt chart is P1 0-6, P2 6-8, P3 8-12. Waiting times are 0, 6 and 8, so the average WT is 14 / 3, about 4.67. Turnaround times are 6, 8 and 12, so the average TAT is 26 / 3, about 8.67. The difference is 4, and the average burst is (6 + 2 + 4) / 3 = 4.