The mechanisms are in place; now the intelligence. This chapter develops the first scheduling policies (sometimes disciplines) โ ideas that predate computers, borrowed from operations management: assembly lines needed scheduling first, with the same laser-like desire for efficiency.
The Crux: How To Develop Scheduling Policy
How should we develop a basic framework for thinking about scheduling policies? What are the key assumptions? What metrics are important? What basic approaches were used in the earliest computer systems?7.1 Workload Assumptions
First, simplifying assumptions about the running processes โ collectively, the workload workload The collective description of the processes/jobs running in a system; the more you know about it, the better your policy. defined in ch. 7 โ open in glossary . Knowing your workload is critical to building policies; ours starts out cartoonishly simple, and the chapter relaxes the assumptions one at a time until we reach (dramatic pause) a fully-operational scheduling discipline. The jobs job Scheduling's name for a unit of work (a process, or a CPU burst treated independently). defined in ch. 7 โ open in glossary in the system:
THE FIVE ASSUMPTIONS (each will fall)
- Each job runs for the same amount of time (dropped in ยง7.3)
- All jobs arrive at the same time (dropped in ยง7.4)
- Once started, each job runs to completion (dropped in ยง7.5)
- Jobs use only the CPU โ no I/O (dropped in ยง7.8)
- The run-time of each job is known (dropped in ยง7.9 โ the most unrealistic: an omniscient scheduler!)
7.2 Scheduling Metrics
To compare policies we need a metric. For now, exactly one โ the turnaround time turnaround time T_completion โ T_arrival โ the performance metric batch systems care about. defined in ch. 7 โ open in glossary :
Since (for now) all jobs arrive at , turnaround equals completion time. Turnaround is a performance metric โ the other axis is fairness fairness Evenly dividing the resource among contenders (e.g., Jain's index); often at odds with turnaround performance. defined in ch. 7 โ open in glossary (measured, e.g., by Jainโs Fairness Index), and the two are often at odds: a scheduler may optimize performance precisely by not running some jobs for a while. Life isnโt always perfect.
7.3 First In, First Out (FIFO)
The most basic algorithm: First In, First Out fifo First In, First Out (a.k.a. FCFS) โ run jobs in arrival order to completion; simple, but suffers the convoy effect. defined in ch. 7 โ open in glossary (a.k.a. First Come, First Served) โ simple, easy to implement, and under our assumptions, pretty good. Three jobs, 10 seconds each (A a hair before B before C):
| job | arrival | runtime | turnaround | response |
|---|---|---|---|---|
| A | 0 | 10 | 10 | 0 |
| B | 0 | 10 | 20 | 10 |
| C | 0 | 10 | 30 | 20 |
| average | 20.00 | 10.00 | ||
A finishes at 10, B at 20, C at 30 โ average turnaround (10+20+30)/3 = 20. Under the five assumptions, FIFO is perfectly fine.
Now relax assumption 1 โ jobs may have different lengths. The book asks: what workload makes FIFO look terrible? (Think first.) Answer: put the long job first. A needs 100 seconds; B and C need 10 each:
| job | arrival | runtime | turnaround | response |
|---|---|---|---|---|
| A | 0 | 100 | 100 | 0 |
| B | 0 | 10 | 110 | 100 |
| C | 0 | 10 | 120 | 110 |
| average | 110.00 | 70.00 | ||
A hogs the CPU for 100 seconds while B and C wait: (100+110+120)/3 = 110 average turnaround. Now flip the policy to SJF and watch the average collapse to 50.
This is the convoy effect convoy effect Short jobs queued behind a heavyweight resource consumer โ the three-cart grocery line. defined in ch. 7 โ open in glossary : relatively-short consumers of a resource queued behind a heavyweight consumer โ the grocery-store line behind a person with three full carts and a checkbook. (Recommended action: switch lines, or breathe. It will be OK.)
7.4 Shortest Job First (SJF)
The fix is an idea stolen from operations research: Shortest Job First sjf Shortest Job First โ run the shortest job to completion first; optimal turnaround when all jobs arrive together. defined in ch. 7 โ open in glossary โ run the shortest job, then the next shortest, and so on. The name is the complete policy description. Try it on the convoy workload above (flip the policy selector in Figure 7.2): B and C go first, and average turnaround falls from 110 to 50 โ more than 2ร better. In fact, with all jobs arriving together, SJF is provably optimal. (This is a systems class, not theory: no proofs allowed.)
Tip: The principle of SJF
SJF applies to any system where perceived per-customer turnaround matters. Any line youโve ever waited in: the grocery storeโs ten-items-or-less lane exists so that shoppers with a few things donโt get stuck behind the family provisioning for nuclear winter.Now relax assumption 2: jobs arrive over time. A (100s) arrives at t=0; B and C (10s each) arrive at t=10 โ just after A started:
| job | arrival | runtime | turnaround | response |
|---|---|---|---|---|
| A | 0 | 100 | 100 | 0 |
| B | 10 | 10 | 100 | 90 |
| C | 10 | 10 | 110 | 100 |
| average | 103.33 | 63.33 | ||
B and C arrive at t=10 โ but SJF is non-preemptive, and A is already running. They wait 90+ seconds anyway: (100 + 100 + 110)/3 โ 103.33. Fixing this needs preemption (next section).
Even under SJF, B and C suffer the convoy: SJF as defined is non-preemptive โ once A starts, it runs to completion. The average climbs back to 103.33 seconds. What can a scheduler do?
Aside: Preemptive schedulers
Old batch systems used non-preemptive schedulers: each job ran to completion before anything else was considered. Virtually all modern schedulers are preemptive preemptive A scheduler willing to stop a running process to run another (via the context switch); all modern schedulers are. defined in ch. 7 โ open in glossary โ perfectly willing to stop one process to run another, using exactly the machinery of chapter 6: the timer interrupt and the context switch context switch The mechanism that stops one program and starts another on a CPU by saving and restoring register state. defined in ch. 4 โ open in glossary . Which is the cliff this section ends on: give SJF the power to preempt, and you get the next sectionโs scheduler.Check yourself
1.Jobs A, B, C each run 10 seconds and arrive together under FIFO. Compute the average turnaround time.
2.Which of the five workload assumptions does the book call the MOST unrealistic?
3.In Figure 7.2 (A=100s first, then B and C at 10s each), what is the convoy effect?
4.Why is SJF provably optimal for average turnaround when all jobs arrive simultaneously?
5.Predict from Figure 7.4: A (100s) starts at t=0; B and C (10s each) arrive at t=10. Why doesn't SJF run them right away?