scheduling - home | computer science and...

29
1 Lecture 3 Scheduling January 18, 2006 Prof. Joe Pasquale Department of Computer Science and Engineering University of California, San Diego © 2006 by Joseph Pasquale CSE 120: Principles of Operating Systems

Upload: lythuan

Post on 21-May-2018

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

1

Lecture 3

SchedulingJanuary 18, 2006

Prof. Joe PasqualeDepartment of Computer Science and Engineering

University of California, San Diego

© 2006 by Joseph Pasquale

CSE 120: Principles of Operating Systems

Page 2: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

2

Before We Begin …

Read Chapter 5 (CPU Scheduling)

Programming Assignment 2• Due Friday, January 27 (midnight)

Homework Assignments• Hwk 1 due on Sunday, January 22 (midnight)• Hwk 2 due on Sunday, January 29 (midnight)

Page 3: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

3

The CPU Scheduling Problem

We have multiple processes/threads, but only one CPU

How much time does each process/thread get on CPU?

Possibilities• Keep it till done• Each uses it a bit and passes it on• Each gets proportional to what they pay

Which is the best policy?

Page 4: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

4

There is No Single Best Policy

Depends on the goals of the system

Different for• your personal computer• large time-shared computer• computer controlling a nuclear power plant

Might even have multiple (conflicting) goals

Page 5: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

5

Scheduling: An Example

Process Arrival Time Service Time1 0 52 0 33 0 1

What order minimizes average turnaround time?

Turnaround time: time between arrival and departure• Arrive, wait for CPU (waiting time)• Use CPU (service time), depart

Page 6: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

6

P1P2P3

0 1 2 3 4 5 6 7 8 9

Longest First vs. Shortest First

Longest First

Shortest First

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 7: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

7

Shortest First Is Provably Optimal

Given n processes with service times S1, S2, S3, … , Sn

Average Turnaround Time (ATT)= [S1 + (S1 + S2) + (S1 + S2 + S3) + … + (S1 + … + Sn)] / n= [(n × S1) + ((n-1) × S2) + ((n-2) × S3) + … + Sn] / n

S1 has maximum weight (n), minimize it

S2 has next-highest weight (n-1), minimize it after S1

In general: order by shortest to longest

Page 8: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

8

P1P2P3

0 1 2 3 4 5 6 7 8 9

waiting executing

Consider Different Arrival Times

Process Arrival Time Service Time1 0 52 1 33 3 1

Page 9: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

9

First-Come First-ServedAllocate CPU in the order that processes arrive

Process Departure Time Turnaround Time1 5 52 8 73 9 6

Average Turnaround Time = (5 + 7 + 6)/3 = 6.0Simple, non-preemptive, poor for short processes

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 10: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

10

Round RobinTime-slice CPU: give each processes a quantum in turn

Process Departure Time Turnaround Time1 9 92 7 63 4 1

Average Turnaround Time = (9 + 6 + 1)/3 = 5.3Simple, preemptive, more overhead than FCFS

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 11: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

11

Shortest Process NextSelect process with shortest execution time

Process Departure Time Turnaround Time1 5 52 9 83 6 3

Average Turnaround Time = (5 + 8 + 3)/3 = 5.3Optimal for non-preemptive, must know exec times

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 12: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

12

Shortest Remaining TimeSelect process with shortest remaining time

Process Departure Time Turnaround Time1 9 92 5 43 4 1

Average Turnaround Time = (9 + 4 + 1)/3 = 4.7Optimal, must know execution times

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 13: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

13

20

21

22

2n

Multi-Level Feedback Queues

Multiple ready queues 0, 1, …, n

Always select process in lowest-

numbered queue

Run selected process for 2i

quantums (for queue i)

If process doesn’t block, place in

next higher queue (except last)

Page 14: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

14

Example using Feedback Queues

Preemptive: if a new process arrives, current process

goes back to queue it came from

Average Turnaround Time = (9 + 5 + 1)/3 = 5.0

Favors shorter processes over longer, dynamic

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 15: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

15

Priority Scheduling

Select process with highest priority

Example: P1 = medium, P2 = high, P3 = low

Allows scheduling based on arbitrary criteria• External, e.g., based on who’s willing to pay most• Internal, e.g., past CPU usage (dynamic)

P1P2P3

0 1 2 3 4 5 6 7 8 9

Page 16: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

16

Fair Share (Proportional Share)

Processes get predetermined fraction of CPU time

Example: P1 to get 25%, P2 to get 50%, P3 to get 25%

Each quantum, which process got least of its fair share?

What is fair share? Getting what you’re supposed to get

P1

P2

P30 1 2 3 4 5 6 7 8 9

100% 50%

100%

33%

100% 67%

25% 40%

50%

33% 43% 50% 56%

100%

Page 17: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

17

Computing Ratios for Fair Share

Determine utilization: fraction of CPU time received

Compute ratio: utilization to fraction promised• If ratio < 1, process getting less than its FS• If ratio > 1, process getting more than its FS• If ratio == 1, process getting exactly its FS

Example P1 P2 P3

• Utilization 40% 40% 15%• Promised 25% 50% 25%• Ratio 40/25=1.6 40/50=0.8 15/25=0.6

Process with lowest ratio needs CPU most: schedule it

Page 18: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

18

Example of Fair Share Scheduling

Time P1 P2 P3 Action1 100/25 0/50 P2 smallest, schedule P2

2 50/25 100/50 tie, schedule either3 33/25 100/50 0/25 schedule P3

4 25/25 67/50 P3 done, schedule P1

5 40/25 50/50 schedule P2

P1

P2

P30 1 2 3 4 5 6 7 8 9

100% 50%

100%

33%

100% 67%

25% 40%

50%

33% 43% 50% 56%

100%

Page 19: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

19

Calculating Exponential Averages

Exponential average: Ai+1 = α Mi + (1 - α) Ai

• Ai = exponential average at time i• Mi = measurement at time i• α = weight (0 - 1) on recent vs. past history

– large α emphasizes recent history– small α emphasizes past history

Example for P1 utilization (α = 0.25, assume A0 = .5)• Mi: 1 0 0 0 1 0 1• Ai: .63 .47 .35 .26 .45 .34 .50• True: 1 .50 .33 .25 .40 .33 .45

Page 20: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

20

Real Time Scheduling

Correctness of real-time systems depend on• logical result of computations• the timing of these results

Type of real-time systems• Hard vs. soft real-time• Periodic vs. aperiodic

Scheduling• Earliest deadline• Rate monotonic scheduling

Page 21: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

21

P1

P20 1 2 3 4 5 6 7 8 9 10 11 12

Periodic Processes (or Tasks)

Periodic processes: perform computation at certain rate

C = compute time, T = period, U = C/T = utilization

Can processes be ordered so that deadlines are met?• Should P1 run before or after P2?

Page 22: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

22

P1

P20 1 2 3 4 5 6 7 8 9 10 11 12

Periodic Processes (or Tasks)

Example C T UP1 15 30 50%P2 10 20 50%

Sum of utilizations does not exceed 100%

P1 running before P2 causes P2 to miss all deadlines!

Page 23: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

23

P1

P20 1 2 3 4 5 6 7 8 9 10 11 12

Periodic Processes

Example C T UP1 15 30 50%P2 10 20 50%

Sum of utilizations does not exceed 100%

P2 before P1 causes P2 to miss three deadlines

Page 24: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

24

P1

P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Earliest Deadline

Schedule the process that has the earliest deadline

Requires sorting of deadlines, O(n log n) complexity

Also works for aperiodic processes

Page 25: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

25

P1

P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Earliest Deadline

Example: C T UP1 30 60 50%P2 10 40 25%P3 7.5 30 25%

Meets all deadlines (sum of utilizations = 100%)

Page 26: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

26

P1

P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Rate Monotonic Scheduling

Prioritize based on rates (rate = 1/period) - no sorting!

Deadlines guaranteed met if:

U1 + U2 + … + Un ≤ n (21/n - 1)

Limited to periodic processes

Page 27: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

27

P1

P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Rate Monotonic Scheduling

Example: C T U RateP1 30 60 50% 1/60 = 0.017P2 10 40 25% 1/40 = 0.025P3 7.5 30 25% 1/30 = 0.033

Fails: P3 misses two deadlines!

Page 28: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

28

P1

P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Rate Monotonic Scheduling

Example: C T U RateP1 20 60 33% 1/60 = 0.017P2 10 40 25% 1/40 = 0.025P3 5 30 16.7% 1/30 = 0.033

Success: U1 + U2 + U3 = 75% ≤ 3 (21/3 - 1) = 78%

Page 29: Scheduling - Home | Computer Science and Engineeringcseweb.ucsd.edu/classes/wi06/cse120/Lec03.pdf · What order minimizes average turnaround time? Turnaround time: time between arrival

29

P1P2

P30 1 2 3 4 5 6 7 8 9 10 11 12

Rate Monotonic Scheduling

Example: C T U RateP1 25 60 41.7% 1/60 = 0.017P2 10 40 25% 1/40 = 0.025P3 5 30 16.7% 1/30 = 0.033

Failure: U1 + U2 + U3 = 83.3% > 3 (21/3 - 1) = 78%