CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471...

90
CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1

Transcript of CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471...

Page 1: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

CS 471 Operating Systems

Yue ChengGeorge Mason University

Fall 2019

1

Page 2: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

o CPU scheduling worksheet posted on BB

2

Page 3: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Review: FIFO, SJF

3

Page 4: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

4

Page 5: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

FIFOo First-In-First-Out: Run jobs in arrival (time) order

5

Page 6: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

First-In-First-Out: Run jobs in arrival (time) orderDef: waiting_time = start_time – arrival_time

Process Burst TimeP1 5P2 5P3 5

o Suppose that the processes arrive in order: P1 , P2 , P3 The Gantt Chart for the schedule:

o Waiting time for P1 = 0; P2 = 5; P3 = 10o Average waiting time: 5

FIFO

6

P1 P2 P3

5 10 150

Page 7: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

First-In-First-Out: Run jobs in arrival (time) orderWhat is the average turnaround time? (Q2)?Def: turnaround_time = completion_time – arrival_time

Process Burst TimeP1 5P2 5P3 5

o Suppose that the processes arrive in order: P1 , P2 , P3 The Gantt Chart for the schedule:

o Waiting time for P1 = 0; P2 = 5; P3 = 10o Average waiting time: 5

FIFO

7

P1 P2 P3

5 10 150

Page 8: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

First-In-First-Out: Run jobs in arrival (time) orderWhat is the average turnaround time? (Q2)?Def: turnaround_time = completion_time – arrival_time

Process Burst TimeP1 5P2 5P3 5

o Suppose that the processes arrive in order: P1 , P2 , P3 The Gantt Chart for the schedule:

FIFO

8

P1 P2 P3

5 10 150

Page 9: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

First-In-First-Out: Run jobs in arrival (time) orderWhat is the average turnaround time? (Q2)?Def: turnaround_time = completion_time – arrival_time

Process Burst TimeP1 5P2 5P3 5

o Suppose that the processes arrive in order: P1 , P2 , P3 The Gantt Chart for the schedule:

Average turnaround time: (5+10+15)/3 = 10

FIFO

9

P1 P2 P3

5 10 150

Page 10: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

10

Page 11: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

11

Page 12: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: Big First JobJOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

12

What is the average turnaround time? (Q3)

Page 13: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: Big First JobJOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

13

P1 P2 P3

80 85 900

Page 14: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: Big First JobJOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

14

P1 P2 P3

80 85 900

Average turnaround time: (80+85+90) / 3 = 85

Page 15: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Convoy Effect

15

Page 16: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Better Schedule?

16

P1P2 P3

Page 17: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Passing the Tractoro New scheduler: SJF (Shortest Job First)

o Policy: When deciding which job to run, choose the one with the smallest run_time

17

Page 18: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: SJF

18

JOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

What is the average turnaround time with SJF? (Q4)

Page 19: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: SJF

19

JOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

P1P2 P3

5 10 900

Page 20: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example: SJF

20

JOB arrival_time run_time

P1 ~0 80

P2 ~0 5

P3 ~0 5

P1P2 P3

5 10 900

Average turnaround time: (5+10+90) / 3 = 35

Page 21: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

21

Page 22: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

22

Page 23: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Shortest Job First (Arrival Time)

23

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

What is the average turnaround time with SJF? (Q5)

Page 24: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Shortest Job First (Arrival Time)

24

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

P1 P3 P2

80 90 1100[P2, P3 arrive at 15]

Page 25: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Shortest Job First (Arrival Time)

25

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

Average turnaround time: (80+75+95) / 3 = ~83.3

P1: 80P2: 75P3: 95

P1 P3 P2

80 90 1100

Page 26: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

A Preemptive Schedulero Previous schedulers: FIFO and SJF are non-

preemptive

o New scheduler: SRTF (Shortest Remaining Time First)

o Policy: Switch jobs so we always run the one that will complete the quickest

26

Page 27: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

SJF

27

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

P1 P3 P2

80 90 1100[P2, P3 arrive at 15]

Page 28: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

SRTF

28

P1 P2P3

15 25 1100

[P2, P3 arrive]

JOB arrival_time run_time

P1 ~0 80P2 ~15 20P3 ~15 10

45

P1

What is the average turnaround time with SRTF? (Q6)

Page 29: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

SRTF

29

P1 P2P3

15 25 1100

[P2, P3 arrive]

JOB arrival_time run_time

P1 ~0 80P2 ~15 20P3 ~15 10

45

P1

P1: 110P3: 10P2: 30

Average turnaround time: (110+30+10) / 3 = 50

Page 30: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

SRTF

30

P1 P2P3

15 25 1100

[P2, P3 arrive]

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

45

P1

What is the average waiting time with SRTF? (Q7)

Page 31: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

SRTF

31

P1 P2P3

15 25 1100

[P2, P3 arrive]

JOB arrival_time run_time

P1 ~0 80

P2 ~15 20

P3 ~15 10

45

P1

P1: 30P3: 0P2: 10

Average waiting time: (30+10+0) / 3 = ~13.3

Page 32: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Outlineo Scheduling Algorithms– First-In-First-Out– Shortest-Job-First, Shortest-Remaining-Time-First– Round Robin (RR) – Priority Scheduling– Multi-Level Feedback Queue (MLFQ)– Lottery Scheduling

32

Page 33: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Optimality of SJF and SRTFo Non-preemptive SJF is optimal if all the

processes are ready simultaneouslyo Gives minimum average waiting time for a given set

of processes

33

Page 34: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Optimality of SJF and SRTFo Non-preemptive SJF is optimal if all the

processes are ready simultaneouslyo Gives minimum average waiting time for a given set

of processes

o What is the intuition behind the optimality of SRTF?

34

Page 35: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Optimality of SJF and SRTFo Non-preemptive SJF is optimal if all the

processes are ready simultaneouslyo Gives minimum average waiting time for a given set

of processes

o What is the intuition behind the optimality of SRTF?– A: SRTF is optimal, considering a more realistic

scenario where all the processes may be arriving at different times

35

Page 36: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Optimality of SJF and SRTFo Non-preemptive SJF is optimal if all the

processes are ready simultaneouslyo Gives minimum average waiting time for a given set

of processes

o What is the intuition behind the optimality of SRTF?– A: SRTF is optimal, considering a more realistic

scenario where all the processes may be arriving at different times

36

Q: What’s the problem?We don’t exactly know how long a job would run!

Page 37: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Estimating the Length of Next CPU Burst

o Idea: Based on the observations in the recent past, we can try to predict

o Techniques such as exponential averaging are based on combining the observations in the past and our predictions using different weights

o Exponential averaging– tn: actual length of the nth CPU burst– zn+1: predicted value for the next CPU burst– zn+1 = k.tn + (1-k).zn– Commonly, k is set to ½

37

Page 38: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Response Timeo Response time definition

Tresponse = Tfirst_run – Tarrival

o SJF’s average response time (all 3 jobs arrive at same time)–(0 + 5 + 10)/3 = 5

38

Page 39: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Waiting, Turnaround, Response

39

P1 P2P3

[P2, P3 arrive at 15]

P1

P1’s waiting time:

P2’s turnaround time:

P3’s response time:

0 25 35 45 120

Page 40: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Waiting, Turnaround, Response

40

P1 P2P3 P1

P1’s waiting time: 0+20=20

P2’s turnaround time: 45-15=30

P3’s response time: 25-15=10

Q: What is P1’s response time?

[P2, P3 arrive at 15]

0 25 35 45 120

Page 41: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Round Robin (RR)

41

Page 42: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

42

Page 43: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

43

Page 44: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Extension to Multiple CPU & I/O Bursts

o When the process arrives, it will try to execute its first CPU burst– It will join the ready queue– The priority will be determined according to the underlying scheduling

algorithm and considering only that specific (i.e. first) burst

o When it completes its first CPU burst, it will try to perform its first I/O operation (burst)– It will join the device queue– When that device is available, it will use the device for a time period

indicated by the length of the first I/O burst.

o Then, it will re-join the ready queue and try to execute its second CPU burst– Its new priority may now change (as defined by its second CPU burst)!

44

Page 45: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Round Robin (RR)o Each process gets a small unit of CPU time

(time quantum). After this time has elapsed, the process is preempted and added to the end of the ready queue

o Newly-arriving processes (and processes that complete their I/O bursts) are added to the end of the ready queue

o If there are n processes in the ready queue and the time quantum is q, then no process waits more than (n-1)q time units

o Performance– q large Þ FIFO– q small Þ Processor Sharing (The system appears to

the users as though each of the n processes has its own processor running at the (1/n)th of the speed of the real processor) 45

Page 46: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Not I/O Aware

46

Poor use of resources

Page 47: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

I/O Aware (Overlap)

47

Overlap allows better use of resources!

Page 48: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

RRo SJF’s average response time– (0 + 5 + 10) / 3 = 5

o RR’s average response time (time quantum = 1)– (0 + 1 + 2) / 3 = 1

48

Process Burst TimeA 5B 5C 5

Page 49: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Tradeoff Considerationo Typically, RR achieves higher average

turnaround time than SJF, but better response time– Turnaround time only cares about when processes finish

o RR is one of the worst policies – -IF- turnaround time is the metric

49

Page 50: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

§ The effect of quantum size on context-switching time must be carefully considered

§ The time quantum must be large with respect to the context-switch time

§ Turnaround time also depends on the size of the time quantum

Choosing a Time Quantum

50

Page 51: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Time Quantum vs. Turnaround Time

51

Page 52: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Time Quantum vs. Turnaround Time

52

Q: What’s the takeaway?

Page 53: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

53

Page 54: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Workload Assumptions1. Each job runs for the same amount of time

2. All jobs arrive at the same time

3. All jobs only use the CPU (no I/O)

4. The run-time of each job is known

54

Page 55: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Priority-Based Scheduling

55

Page 56: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Priority-Based Schedulingo A priority number (integer) is associated with each

process

o The CPU is allocated to the process with the highest priority o (smallest integer º highest priority)o Preemptiveo Non-preemptive

56

Page 57: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example for Priority-Based Scheduling

ProcessAaiBurst TimeT PriorityP1 10 3P2 1 1P3 2 4P4 1 5P5 5 2

o Priority scheduling Gantt Chart

o Average waiting time = 8.2

57

P2 P3P5

1 180 16

P4

196

P1

Page 58: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Priority-Based Scheduling (cont.)o Priority Assignment

– Internal factors: timing constraints, memory requirements, the ratio of average I/O burst to average CPU burst …

– External factors: Importance of the process, financial considerations, hierarchy among users …

o Problem: Indefinite blocking (or starvation) – low priority processes may never execute

o One solution: Agingo As time progresses increase the priority of the processes

that wait in the system for a long time

58

Page 59: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Multi-Level Feedback Queue (MLFQ)

59

Page 60: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Multi-Level Feedback Queue (MLFQ)

o Goals of MLFQ– Optimize turnaround time

• In reality, SJF does not work since OS does not know how long a process will run

– Minimize response time • Unfortunately, RR is really bad on optimizing turnaround time

60

Page 61: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

MLFQ: Basicso MLFQ maintains a number of queues (multi-

level queue)– Each assigned a different priority level– Priority decides which process should run at a given

time

61

Page 62: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

MLFQ Example

62

How to know process type to set priority?1. nice2. history

Page 63: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

How to Check Nice Values in Linux?

o % ps ax -o pid,ni,cmd

63

Page 64: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

MLFQ Example

64

How to know process type to set priority?1. nice2. history

In this example, A and B are given high priority to run, while C and D may starve

Page 65: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

MLFQ: Basic Ruleso MLFQ maintains a number of queues (multi-

level queue)– Each assigned a different priority level– Priority decides which process should run at a given

time

65

Page 66: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Attempt #1: Change Priorityo Workload

– Interactive processes (many short-run CPU bursts)– Long-running processes (CPU-bound)

o Each time quantum = 10ms

66

Page 67: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 1: One Single Long-Running Process

o A process enters at highest priority (time quantum = 10ms)

67

Page 68: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 1: One Single Long-Running Process

o A process enters at highest priority (time quantum = 10ms)

68

Page 69: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 1: One Single Long-Running Process

o A process enters at highest priority (time quantum = 10ms)

69

Page 70: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 2: Along Came a Short-Running Process

o Process A: long-running process (start at 0)

70

Process A

Process B

Page 71: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 2: Along Came a Short-Running Process

o Process A: long-running process (start at 0)o Process B: short-running interactive process

(start at 100)

71

Process A

Process B

Page 72: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 2: Along Came a Short-Running Process

o Process A: long-running process (start at 0)o Process B: short-running interactive process

(start at 100)

72

Process A

Process B

Page 73: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 2: Along Came a Short-Running Process

o Process A: long-running process (start at 0)o Process B: short-running interactive process

(start at 100)

73

Process A

Process B

Page 74: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 3: What about I/O?o Process A: long-running process o Process B: I/O-intensive interactive process

(each CPU burst = 1ms)

74

CPU-intensive Process A

I/O-intensive Process B

Rule 4b

Page 75: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 4: What’s the Problem?

o Process A: long-running process o Process B + C: Interactive process

75

Interactive Process B

Interactive Process C

Page 76: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Example 4: What’s the Problem?

o Process A: long-running process o Process B + C: Interactive process

76

CPU-intensive Process Astarves!

Interactive Process B

Interactive Process C

Page 77: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Attempt #2: Priority Boosto Simple idea: Periodically boost the priority of all

processes

77

CPU-intensive Process Aproceeds!

Interactive Process B

Interactive Process C

Page 78: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Tuning MLFQo MLFQ scheduler is defined by many parameters:

– Number of queues– Time quantum of each queue– How often should priority be boosted?– A lot more…

o The scheduler can be configured to match the requirements of a specific system– Challenging and requires experience

78

Page 79: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling

79

Page 80: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Schedulingo Goal: Proportional share

– One of the fair-share schedulers

o Approach– Gives processes lottery tickets– Whoever wins runs– Higher priority --> more tickets

80

Page 81: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Code

81

Page 82: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

82

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

Page 83: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

83

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = random(402)

Page 84: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

84

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

Page 85: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

85

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

Is 1 > 102?

Page 86: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

86

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

Is 2 > 102?

Page 87: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

87

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

Is 102 > 102?

Page 88: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

88

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

Is 302 > 102?

Page 89: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Lottery Scheduling Example

89

Job A(1)

Job B(1)

Job C(100)

Job D(200)

Job E(100)

402 total tickets

winner = 102

302 > 102

OS picks Job D to run!

Page 90: CS 471 Operating Systems Yue Chengyuecheng/teaching/cs471_fall19/_static/lecs/lec … · CS 471 Operating Systems Yue Cheng George Mason University Fall 2019 1. oCPUschedulingworksheetposted

Other Lottery Ideaso Ticket transfers

o Ticket currencies

o Ticket inflation

o Read more in OSTEP

90