ยง7.1โ€“7.4Workload Assumptions โ€ฆ Shortest Job First (SJF)

Part I OSTEP pp. 65โ€“68 ยท ~5 min read

  • workload
  • job
  • turnaround time
  • fairness
  • fifo
  • convoy effect
  • sjf
  • preemptive

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 . 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 in the system:

THE FIVE ASSUMPTIONS (each will fall)

  1. Each job runs for the same amount of time (dropped in ยง7.3)
  2. All jobs arrive at the same time (dropped in ยง7.4)
  3. Once started, each job runs to completion (dropped in ยง7.5)
  4. Jobs use only the CPU โ€” no I/O (dropped in ยง7.8)
  5. 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 :

Tturnaround=Tcompletionโˆ’TarrivalT_{turnaround} = T_{completion} - T_{arrival}

Since (for now) all jobs arrive at Tarrival=0T_{arrival} = 0, turnaround equals completion time. Turnaround is a performance metric โ€” the other axis is fairness (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 (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):

Figure 7.1: FIFO, three equal jobs โ€” the happy case (switch the policy: nothing changes much)
AarrrunBarrrunCarrrun
CPUA: 0โ€“10AB: 10โ€“20BC: 20โ€“30C0102030
jobarrivalruntimeturnaroundresponse
A010100
B0102010
C0103020
average20.0010.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:

Figure 7.2: Why FIFO is not that great โ€” relax assumption 1 and meet the convoy
AarrrunBarrrunCarrrun
CPUA: 0โ€“100AB: 100โ€“110BC: 110โ€“120C020406080100120
jobarrivalruntimeturnaroundresponse
A01001000
B010110100
C010120110
average110.0070.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 : 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 โ€” 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:

Figure 7.4: SJF with late arrivals โ€” the convoy sneaks back
AarrrunBarrrunCarrrun
CPUA: 0โ€“100AB: 100โ€“110BC: 110โ€“120C020406080100120
jobarrivalruntimeturnaroundresponse
A01001000
B101010090
C1010110100
average103.3363.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 โ€” perfectly willing to stop one process to run another, using exactly the machinery of chapter 6: the timer interrupt and the context switch . 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?

5 questions