CS 332 w22 — Uniprocessor Scheduling

Table of Contents

1 Overview

  • Scheduling policy: what to do next, when there are multiple threads ready to run
    • Or multiple packets to send, or web requests to serve, or …
  • Definitions
    • Response time, throughput, predictability
  • Fundamentals: Unicore policies
    • FIFO, SJF, round robin, max-min fairness
    • Multilevel feedback queue as approximation to optimal
  • Next time:
    • Energy-aware scheduling
    • Multicore policies

2 Definitions

  • Workload
    • Set of tasks (also called jobs) for system to perform
    • Tasks can be any size, a thread or process can generate multiple tasks
    • Tasks can be compute-bound or I/O bound
  • Preemptive scheduler
    • If we can take resources away from a running task
  • Work-conserving
    • Resource is used whenever there is a task to run
    • For non-preemptive schedulers, work-conserving is not always better
  • Scheduling algorithm
    • Takes a workload as input, decides which tasks to do first
    • Performance metrics (response time, predictability, throughput, overhead, fairness) as output
    • Only preemptive, work-conserving schedulers to be considered

3 Performance Metrics

  • Throughput
    • Average tasks completed per time unit
  • Response Time
    • Average time required to complete a task
  • Predictability
    • Low variance in response times for repeated requests
  • Fairness
    • ???
  • Unfairness
    • Higher priority tasks get special treatment

4 Scheduling Policies

4.1 FIFO

  • Schedule tasks in the order they arrive
  • Continue running them until they complete or give up the processor
  • Seems completely fair, right?
    • If you've ever waited in line, this is how they tend to work
  • On what workloads is FIFO particularly bad?

4.2 Shortest Job First (SJF)

  • Always do the task that has the shortest (remaining) amount of work to do
    • Often called Shortest Remaining Time First (SRTF)
  • Suppose we have five tasks arrive one right after each other, but the first one is much longer than the others
    • Which completes first in FIFO? Next?
    • Which completes first in SJF? Next?

badFIFO.png

4.2.1 SJF is Optimal for Average Response Time

  • Why?
    • Interchange argument
      • If a longer task precedes a shorter one in the schedule, swap them
      • The longer one's response time in the new schedule equals the shorter one's in the old schedule
      • The shorter one's response time in the new schedule is less than the longer one's in the old schedule
      • So, the average response time has decreased
  • Does SJF have any downsides?
    • Fairness—should shorter tasks always go first?
    • Starvation—what if a longer task never gets a turn?
    • Worst for variance in response time (longer tasks scheduled as slowly as possible)
    • Frequent context switches
    • Requires knowledge of the future! (Have to approximate in practice)

4.3 Round Robin

  • Each task gets resource for a fixed period of time (called a time slice or a time quantum)
    • If task doesn't complete, it goes back in line
  • Need to pick a time slice
    • What if time slice is too long?
      • Infinite? We're back to FIFO
    • What if time quantum is too short?
      • One instruction? Death by overhead

badFIFORR.png



equalLength.png

4.4 Multi-Level Feedback Queue (MFQ)

  • Goals:
    • Responsiveness
    • Low overhead
    • Starvation freedom
    • Some tasks are high/low priority
    • Fairness (among equal priority tasks)
  • Not optimal at any of them!
    • Linux, Windows, and MacOS all use a form of MFQ scheduling
  • Set of Round Robin queues
    • Each queue has a separate priority
  • High priority queues have short time slices
  • Low priority queues have long time slices
  • Scheduler picks first thread in highest priority queue
  • Tasks start in highest priority queue
  • If time slice expires, task drops one level

mfq.png

  • Rule 1: If Priority(A) > Priority(B), A runs (B doesn't).
  • Rule 2: If Priority(A) = Priority(B), A & B run in round-robin fashion using the time slice (quantum length) of the given queue.
  • Rule 3: When a job enters the system, it is placed at the highest priority (the topmost queue).
  • Rule 4: Once a job uses up its time allotment at a given level (regardless of how many times it has given up the CPU), its priority is reduced (i.e., it moves down one queue).
  • Rule 5: After some time period S, move all the jobs in the system to the topmost queue.

5 Reading: Multi-level Feedback Queue

Read OSTEP chapter 8, (p. 85–94) on the multi-level feedback queues. It develops this classic scheduling technique in more detail.