operating systems cmpsci 377 lecture 6: scheduling

48
UNIVERSITY OF NIVERSITY OF MASSACHUSETTS ASSACHUSETTS, A , AMHERST MHERST Department of Computer Science Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture 6: Scheduling

Upload: ashlyn

Post on 19-Mar-2016

60 views

Category:

Documents


2 download

DESCRIPTION

Operating Systems CMPSCI 377 Lecture 6: Scheduling. Emery Berger University of Massachusetts, Amherst. Last Time: Threads & Scheduling. Thread = execution stream within process User-level, kernel-level, hybrid No perfect scheduling algorithm Policy decision Goals: Minimize response time - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science

Emery BergerUniversity of Massachusetts, Amherst

Operating SystemsCMPSCI 377

Lecture 6: Scheduling

Page 2: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 2

Last Time: Threads & Scheduling Thread = execution stream within

process User-level, kernel-level, hybrid

No perfect scheduling algorithm Policy decision Goals:

Minimize response time Maximize throughput Fairness

Page 3: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 3

This Time: Scheduling Algorithms FCFS

First-Come, First-Served Round-robin SJF Multilevel Feedback Queues Lottery Scheduling

Page 4: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 4

Round-Robin Scheduling Quantum expires: move to back of

queue Variants used in most real systems

Tradeoffs: length of quantum Large: response time increases

quantum ) 1 = FCFS Small: throughput decreases

quantum ) 0 = overhead dominates context switches, cache misses

Page 5: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 5

Example: Round-Robin

waitingrunning

Page 6: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 6

Example: Round-Robin

waitingrunning

Page 7: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 7

Example: Round-Robin

waitingrunning

Page 8: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 8

Example: Round-Robin

waitingrunning

Page 9: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 9

Example: Round-Robin

waitingrunning

Page 10: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 10

Example: Round-Robin

waitingrunning

Page 11: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 11

Example: Round-Robin

waitingrunning

Page 12: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 12

Example: Round-Robin

waitingrunning

Page 13: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 13

Example: Round-Robin

waitingrunning

Page 14: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 14

Example: Round-Robin

waitingrunning

Page 15: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 15

Example: Round-Robin

waitingrunning

+ Fair- Long

average wait times

Page 16: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 16

Round-Robin vs. FCFS Example 1:

5 jobs, 100 seconds each, quantum = 1s

ignore context switch time

Page 17: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 17

Round-Robin vs. FCFS Example 1:

5 jobs, 100 seconds each, quantum = 1s

ignore context switch time

Page 18: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 18

Round-Robin vs. FCFS Example 2:

5 jobs: 50, 40, 30, 20, 10 seconds each

Page 19: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 19

Round-Robin vs. FCFS Example 2:

5 jobs: 50, 40, 30, 20, 10 seconds each

Page 20: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 20

This Time: Scheduling Algorithms FCFS

First-Come, First-Served Round-robin SJF Multilevel Feedback Queues Lottery Scheduling

Page 21: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 21

Example: SJF

5040

3020

10

Schedule job with least work until I/O or done

0 10 30 60 100 150

Page 22: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 22

Example: SJF

5040

3020

10

Schedule job with least work until I/O or done

0 10 30 60 100 150

Page 23: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 23

Example: SJF

5040

3020

10

Schedule job with least work until I/O or done

0 10 30 60 100 150

Page 24: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 24

Example: SJF

5040

3020

10

Schedule job with least work until I/O or done

0 10 30 60 100 150

Page 25: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 25

Example: SJF

5040

3020

10

Schedule job with least work until I/O or done

0 10 30 60 100 150

Page 26: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 26

Example: SJF

5040

3020

100 10 30 60 100 150

Schedule job with least work until I/O or done

Page 27: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 27

Example: SJF 5 jobs, length 50, 40, 30, 20, 10

seconds

Page 28: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 28

Example: SJF 5 jobs, length 50, 40, 30, 20, 10

seconds

Page 29: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 29

SJF/SRTF: Shortest-Job First Advantages:

Provably optimal – minimizes average waiting time

Works for preemptive & non-preemptive schedulers

Preemptive SJF = SRTF Shortest remaining time first

I/O-bound jobs get priority over CPU-bound jobs

Disadvantages: Impossible to predict CPU time job has left Long-running CPU-bound jobs can starve

Page 30: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 30

This Time: Scheduling Algorithms FCFS

First-Come, First-Served Round-robin SJF Multilevel Feedback Queues Lottery Scheduling

Page 31: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 31

Multilevel Feedback Queues (MLFQ) Use past behavior to predict future,

assign job priorities Overcome prediction problem in SJF

Assumption: I/O-bound in past, I/O-bound in future Scheduler favors jobs that used least

CPU time Adaptive:

Change in behavior ) change in scheduling decisions

Page 32: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 32

MLFQ: Approximating SJF Multiple queues, different priorities

Round-robin scheduling at each priority level

Run all at highest priority first, then next, etc.

Can lead to starvation Increase quantum exponentially at

lower priorities

Page 33: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 33

MLFQ Example

Page 34: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 34

MLFQ: Assigning Priorities Job starts in highest priority queue

Quantum expires ) CPU-bound Drop priority one level

Quantum does not expire ) I/O-bound Increase priority one level

CPU-bound jobs move down,I/O-bound jobs move up

Page 35: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 35

Improving Fairness SJF: optimal, but unfair

Increase fairness = give long jobs CPU time degrades average waiting time

Solutions: Each queue – fraction of CPU time

Fair iff even distribution of jobs among queues

Adjust priority of jobs w/o service Originally done by UNIX Avoids starvation Under load, waiting time suffers

Page 36: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 36

This Time: Scheduling Algorithms FCFS

First-Come, First-Served Round-robin SJF Multilevel Feedback Queues Lottery Scheduling

Page 37: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 37

Lottery Scheduling Every job gets lottery tickets Each quantum: randomly pick

winner On average:

CPU time proportional to # of tickets Give most tickets to short-running

jobs (approximates SJF) Give every job at least one ticket Degrades gracefully as load changes

Page 38: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 38

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 39: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 39

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 40: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 40

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 41: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 41

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 42: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 42

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 43: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 43

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 44: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 44

Example: Lottery Scheduling

Paying customers: 40%, guests: 60% 2:1 ticket ratio

2 2 1 1 1 1 1 1

Page 45: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 45

Example: Lottery Scheduling2 2 1 1 1 1 1 1

2/5=40%

Page 46: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 46

Example: Lottery Scheduling

Probabilistically achieves desired proportions

2 2 1 1 1 1 1 1

2/5=40%

3/5=60%

Page 47: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 47

Summary of Scheduling Algorithms FCFS:

unfair, average waiting time poor Round robin:

fair, average waiting time poor SJF:

unfair, minimizes average waiting time requires accurate prediction

Multilevel Feedback Queueing: approximates SJF

Lottery scheduling: fair, low average waiting time poor fit to priority

Page 48: Operating Systems CMPSCI 377 Lecture 6: Scheduling

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 48

Next Time Synchronization