chapter 5: cpu scheduling joe mccarthy css 430: operating systems - cpu scheduling1

94
Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling 1

Upload: irma-oneal

Post on 19-Dec-2015

236 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Chapter 5:CPU Scheduling

Joe McCarthy

CSS 430: Operating Systems - CPU Scheduling 1

Page 2: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Chapter 5: CPU Scheduling

• Basic Concepts• Scheduling Criteria • Scheduling Algorithms• Thread Scheduling• Multiple-Processor Scheduling• Operating Systems Examples• Algorithm Evaluation

2CSS 430: Operating Systems - CPU Scheduling

Material derived, in part, fromOperating Systems Concepts with Java, 8th Ed.

© 2009 Silberschatz, Galvin & Gagne

Page 3: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU

CSS 430: Operating Systems - CPU Scheduling 3

[from Chapter 1: Introduction]

Page 4: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 4

[from Chapter 1: Introduction]

Page 5: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 5

[from Chapter 1: Introduction]

Page 6: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 6

[from Chapter 1: Introduction]

Multiprogramming

Page 7: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 7

[from Chapter 3: Processes]

Page 8: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 8

Process Control Blocks

[from Chapter 3: Processes]

Page 9: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 9

Can all PCBs have access to CPU simultaneously?

[from Chapter 3: Processes]

Page 10: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduling

CSS 430: Operating Systems - CPU Scheduling 10

[from Chapter 3: Processes]

Page 11: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Queues

CSS 430: Operating Systems - CPU Scheduling 11

[from Chapter 3: Processes]

Page 12: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Process States

CSS 430: Operating Systems - CPU Scheduling 12

[from Chapter 3: Processes]

Page 13: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Basic Concepts

• Goal: Maximize CPU utilization via optimized multiprogramming

• CPU–I/O Burst Cycle:– Process execution consists of cycles

• CPU execution • I/O wait

• CPU burst durations vary– Within a process– across different processes

CSS 430: Operating Systems - CPU Scheduling 13

Page 14: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Histogram of CPU-burst Durations

CSS 430: Operating Systems - CPU Scheduling 14

Page 15: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduler

• Selects next process to run (i.e., to get CPU time)• Dispatches that process (allocates the CPU to it)• CPU scheduler called when a process …

CSS 430: Operating Systems - CPU Scheduling 15

Page 16: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CPU Scheduler

• Selects next process to run (i.e., to get CPU time)• Dispatches that process (allocates the CPU to it)• CPU scheduler called when a process:

– Running terminated– Running waiting– Running ready– Waiting ready

• Preemptive vs. non-preemptive

CSS 430: Operating Systems - CPU Scheduling 16

Page 17: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Dispatcher

• Activates a process (gives control of the CPU to the process) selected by the short-term scheduler

• Three steps:

CSS 430: Operating Systems - CPU Scheduling 17

Page 18: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Dispatcher

• Activates a process (gives control of the CPU to the process) selected by the short-term scheduler

• Three steps:

CSS 430: Operating Systems - CPU Scheduling 18

Page 19: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Dispatcher

• Activates a process (gives control of the CPU to the process) selected by the short-term scheduler

• Three steps:– Switch context– Switch to user mode– Jump to the proper location

in user program to start/resume

• Dispatch latency: time it takes for the dispatcher to stop one user process and start another one

CSS 430: Operating Systems - CPU Scheduling 19

Page 20: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Scheduling Milestones• Submission Time

– Time when job is submitted– E.g., when command is entered in shell

• Admission Time– Time when job is admitted to Ready queue– PCB creation & other housekeeping has to take place

• Activation Time– Time when job is first activated (Ready Running)– E.g., when first output starts appearing on console– NB: may have several CPU-I/O burst cycles

• Completion Time– Time when job finishes executing

CSS 430: Operating Systems - CPU Scheduling 20

Page 21: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Job (Process/Thread) Times• Response Time

– Time before job is first activated– Activation Time – Submission Time– E.g., time between entering command & first response

• Wait Time– Total amount of time job spends in Ready queue

• Execution Time– Total amount of time job spends Running– NB: may have several CPU-I/O burst cycles

• Turnaround Time– Time a job takes to complete after it is submitted– Completion Time – Submission Time

CSS 430: Operating Systems - CPU Scheduling 21

Page 22: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Scheduling Criteria• CPU utilization

– % of time CPU is executing user processes• Throughput

– # of processes that complete their execution per time unit• Average response time

– average amount of between when a process is submitted & first response (for time-sharing environment)

• Average waiting time– average amount of time a process waits in Ready queue

• Average turnaround time– average amount of time to execute a process

CSS 430: Operating Systems - CPU Scheduling 22

Page 23: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Scheduling Optimization Criteria• CPU utilization: maximize

– % of time CPU is executing user processes• Throughput: maximize

– # of processes that complete their execution per time unit• Average response time: minimize

– average amount of between when a process is submitted & first response (for time-sharing environment)

• Average waiting time: minimize– average amount of time a process waits in Ready queue

• Average turnaround time: minimize– average amount of time to execute a process

CSS 430: Operating Systems - CPU Scheduling 23

Page 24: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

First-Come, First-Served (FCFS)• FCFS policy often implemented with FIFO queue• Example: 3 processes with the following burst times:

P1 = 24; P2 = 3; P3 = 3 • Suppose that the processes arrive at t0 in the order:

P1, P2, P3 The Gantt Chart for the schedule is:

• Waiting times• Average waiting time:

CSS 430: Operating Systems - CPU Scheduling 24

P1 P2 P3

24 27 300

Page 25: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

First-Come, First-Served (FCFS)• FCFS policy often implemented with FIFO queue• Example: 3 processes with the following burst times:

P1 = 24; P2 = 3; P3 = 3 • Suppose that the processes arrive at t0 in the order:

P1, P2, P3 The Gantt Chart for the schedule is:

• Waiting times: P1 = 0; P2 = 24; P3 = 27• Average waiting time: (0 + 24 + 27) / 3 = 17.0

CSS 430: Operating Systems - CPU Scheduling 25

P1 P2 P3

24 27 300

Page 26: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

FCFS Scheduling

Suppose that the processes arrive in the order: P2 , P3 , P1

• The Gantt chart for the schedule is:

• Waiting times:• Average waiting time:

CSS 430: Operating Systems - CPU Scheduling 26

P1P3P2

63 300

Page 27: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

FCFS Scheduling

Suppose that the processes arrive in the order: P2 , P3 , P1

• The Gantt chart for the schedule is:

• Waiting times: P1 = 6; P2 = 0;; P3 = 3• Average waiting time: (6 + 0 + 3) / 3 = 3.0

CSS 430: Operating Systems - CPU Scheduling 27

P1P3P2

63 300

Page 28: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

FCFS Scheduling

• Convoy effect: short process[es] stuck behind long process• How could we achieve minimal average waiting time?

CSS 430: Operating Systems - CPU Scheduling 28

P1P3P2

63 300

P1 P2 P3

24 27 300

Order: P1, P2, P3

Average wait: 17.0Order: P2, P3, P1

Average wait: 3.0

Page 29: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest-Job-First (SJF) Scheduling

• Always schedule the shortest job first– Select process with shortest next CPU burst.

• Optimal average waiting time– For any set of processes

CSS 430: Operating Systems - CPU Scheduling 29

P1P3P2

63 300

Page 30: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Example of SJFProcess Arrival Time Burst Time

P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3

• SJF scheduling chart

CSS 430: Operating Systems - CPU Scheduling 30

Page 31: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Example of SJFProcess Arrival Time Burst Time

P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3

• SJF scheduling chart

• Average waiting time:

CSS 430: Operating Systems - CPU Scheduling 31

P4 P3P1

3 160 9

P2

24

Page 32: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Example of SJFProcess Arrival Time Burst Time

P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3

• SJF scheduling chart

• Average waiting time: (3 + 16 + 9 + 0) / 4 = 7.0

CSS 430: Operating Systems - CPU Scheduling 32

P4 P3P1

3 160 9

P2

24

Page 33: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS430 CPU Scheduling 33

Another SJF example

• Example: Process Arrival Time Burst TimeP1 0 7

P2 2 4 P3 4 1

P4 5 4

• Non preemptive SJFP1 P3 P2

7

P1(7)

160

P4

8 12

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

2 4 5

P2(4)

P3(1)

P4(4)

P1‘s wating time = 0

P2‘s wating time = 6

P3‘s wating time = 3

P4‘s wating time = 7

Page 34: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS430 CPU Scheduling 34

Another SJF example (cont’d)• Example: Process Arrival Time Burst Time

P1 0 7 P2 2 4 P3 4 1

P4 5 4

• Preemptive SJFP1 P3P2

42 110

P4

5 7

P2 P1

16

Average waiting time = (9 + 1 + 0 +2)/4 = 3

P1(7)

P2(4)

P3(1)

P4(4)

P1‘s wating time = 9

P2‘s wating time = 1

P3‘s wating time = 0

P4‘s wating time = 2

P1(5)

P2(2)

Page 35: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest-Job-First Scheduling

• Always schedule the shortest job first– Select process with shortest next CPU burst.

• Optimal average waiting time– For any set of processes

• Only one problem

CSS 430: Operating Systems - CPU Scheduling 35

P1P3P2

63 300

Page 36: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest-Job-First Scheduling

• Always schedule the shortest job first– Select process with shortest next CPU burst.

• Optimal average waiting time– For any set of processes

• Only one problem

CSS 430: Operating Systems - CPU Scheduling 36

P1P3P2

63 300

http://www.hedgeworld.com/blog/?p=3570

Page 37: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest-Job-First Scheduling

• Always schedule the shortest job first– Select process with shortest next CPU burst.

• Optimal average waiting time– For any set of processes

• Only one problem– How to know the length of the next CPU burst?

CSS 430: Operating Systems - CPU Scheduling 37

P1P3P2

63 300

http://www.hedgeworld.com/blog/?p=3570

Page 38: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Determining Length of Next CPU Burst

• Can only estimate length of future bursts

• Use exponential averaging of previous bursts

CSS 430: Operating Systems - CPU Scheduling 38

:Define 4.

10 , 3.

burst CPU next the for value predicted 2.

burst CPU of length actual 1.

1n

thn nt

.1 1 nnn t

Page 39: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Examples of Exponential Averaging• = 0

– n+1 = n

– Recent history does not count

• = 1– n+1 = tn

– Only the most recent CPU burst counts

• = 0.5 (typically)• If we expand the formula, we get:

n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -j + … +(1 - )n +1 0

• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor

CSS 430: Operating Systems - CPU Scheduling 39

Page 40: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Predicting Length of Next CPU Burst

CSS 430: Operating Systems - CPU Scheduling 40

Page 41: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest Remaining Time First

• Pre-emptive version of SJF

CSS 430: Operating Systems - CPU Scheduling 41

Page 42: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest Remaining Time First

• Pre-emptive version of SJF

CSS 430: Operating Systems - CPU Scheduling 42

Average wait time?

Page 43: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest Remaining Time First

• Pre-emptive version of SJF

CSS 430: Operating Systems - CPU Scheduling 43

Average wait time: [(10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)] / 4 = 6.5

Page 44: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest Remaining Time First

• Pre-emptive version of SJF

CSS 430: Operating Systems - CPU Scheduling 44

Average wait time: [(10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)] / 4 = 6.5

Non pre-emptive SJF?

Page 45: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Shortest Remaining Time First

• Pre-emptive version of SJF

CSS 430: Operating Systems - CPU Scheduling 45

Average wait time: [(10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)] / 4 = 6.5

Non pre-emptive SJF: [0 + (8 – 1) + (17 – 2) + (12 – 3)] / 4 = 7.75

Page 46: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling• A priority number (integer) is associated with each process

• Based on internal and/or external factors• The CPU is allocated to the process with the highest priority

– Preemptive: new, higher priority processes take precedence– Non-preemptive: new processes wait for current process

• Textbook: low # high priority [opposite of ThreadOS]• SJF priority: • FCFS priority:

CSS 430: Operating Systems - CPU Scheduling 46

Page 47: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling• A priority number (integer) is associated with each process

• Based on internal and/or external factors• The CPU is allocated to the process with the highest priority

– Preemptive: new, higher priority processes take precedence– Non-preemptive: new processes wait for current process

• Textbook: low # high priority (opposite of ThreadOS)• SJF priority: predicted next CPU burst time• FCFS priority: arrival time

CSS 430: Operating Systems - CPU Scheduling 47

Page 48: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling• A priority number (integer) is associated with each process

• Based on internal and/or external factors• The CPU is allocated to the process with the highest priority

– Preemptive: new, higher priority processes take precedence– Non-preemptive: new processes wait for current process

• Textbook: low # high priority (opposite of ThreadOS)• SJF priority: predicted next CPU burst time• FCFS priority: arrival time• Problem?

CSS 430: Operating Systems - CPU Scheduling 48

Page 49: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling• A priority number (integer) is associated with each process

• Based on internal and/or external factors• The CPU is allocated to the process with the highest priority

– Preemptive: new, higher priority processes take precedence– Non-preemptive: new processes wait for current process

• Textbook: low # high priority (opposite of ThreadOS)• SJF priority: predicted next CPU burst time• FCFS priority: arrival time• Problem Starvation – low priority processes may never

execute• Solution?

CSS 430: Operating Systems - CPU Scheduling 49

Page 50: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling• A priority number (integer) is associated with each process

• Based on internal and/or external factors• The CPU is allocated to the process with the highest priority

– Preemptive: new, higher priority processes take precedence– Non-preemptive: new processes wait for current process

• Textbook: low # high priority (opposite of ThreadOS)• SJF priority: predicted next CPU burst time• FCFS priority: arrival time• Problem Starvation – low priority processes may never

execute• Solution Aging – as time progresses increase the priority

of the process

CSS 430: Operating Systems - CPU Scheduling 50

Page 51: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling Example

CSS 430: Operating Systems - CPU Scheduling 51

Average wait time: (6 + 0 + 16 + 18 + 1) / 5 = 8.2

Page 52: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling Example

CSS 430: Operating Systems - CPU Scheduling 52

Average wait time: (6 + 0 + 16 + 18 + 1) / 5 = 8.2

If pre-emptive & process arrival times are 0, 1, 2, 3, 4?Average wait time:

Page 53: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Priority Scheduling Example

CSS 430: Operating Systems - CPU Scheduling 53

Average wait time: (6 + 0 + 16 + 18 + 1) / 5 = 8.2

If pre-emptive & process arrival times are 0, 1, 2, 3, 4?Average wait time: [((2 – 1) + (9 – 4)) + 0 + (16 – 2) + (18 – 3) + 0] / 5 = 7.0

Page 54: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Round Robin (RR)

• Each process gets time quantum (q) of CPU time (typically 10-100 ms)– Selected from head of Ready queue

• If process does not block (I/O) within q ms: preempted – Added to the tail of the Ready queue (as are new

processes)• General observations:

– n processes in the Ready queue – each process gets 1/n of the CPU time (in slices of size q) – maximum waiting time: (n-1)q time units

CSS 430: Operating Systems - CPU Scheduling 54

Page 55: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Process Burst TimeP1 24 P2 3 P3 3

• The Gantt chart is:

Example of RR with Time Quantum = 4

CSS 430: Operating Systems - CPU Scheduling 55

Page 56: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Example of RR with Time Quantum = 4

Process Burst TimeP1 24 P2 3 P3 3

• The Gantt chart is:

• Compare to SJF:

CSS 430: Operating Systems - CPU Scheduling 56

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

P1P3P2

63 300

Page 57: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS430 CPU Scheduling 57

• Each process is given CPU time in turn, (i.e. time quantum: usually 10-100 milliseconds), and thus waits no longer than ( n – 1 ) * time quantum

• time quantum = 20Process Burst Time Wait TimeP1 53 57 +24 = 81 P2 17 20 P3 68 37 + 40 + 17= 94 P4 24 57 + 40 = 97

Another RR example

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Average wait time = (81+20+94+97)/4 = 73

57

20

37

57

24

40

40

17

P1(53)

P2(17)

P3(68)

P4(24)

P1(33) P1(13)

P3(48) P3(28) P3(8)

P4(4)

Page 58: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Time Quantum & Context Switch Time

• Performance issues:– q large – q small

CSS 430: Operating Systems - CPU Scheduling 58

Page 59: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Time Quantum & Context Switch Time

• Performance issues:– q large FCFS– q small context switch overhead too high

CSS 430: Operating Systems - CPU Scheduling 59

Page 60: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Time Quantum Turnaround Time

CSS 430: Operating Systems - CPU Scheduling 60

Page 61: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multilevel Queue

• Ready queue can be partitioned into separate queues– E.g., foreground (interactive) & background (batch)

• Each queue can have its own scheduling algorithm• Scheduling must be done between the queues• Simplest: Fixed priority scheduling

– One possibility: 100% priority to foreground• Serve foreground if any; serve background only if no foreground• Problem?

CSS 430: Operating Systems - CPU Scheduling 61

Page 62: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multilevel Queue

• Ready queue can be partitioned into separate queues– E.g., foreground (interactive) & background (batch)

• Each queue can have its own scheduling algorithm• Scheduling must be done between the queues• Simplest: Fixed priority scheduling

– One possibility: 100% priority to foreground• Serve foreground if any; serve background only if no foreground• Problem: possibility of starvation

– Another possibility?

CSS 430: Operating Systems - CPU Scheduling 62

Page 63: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multilevel Queue

• Ready queue can be partitioned into separate queues– E.g., foreground (interactive) & background (batch)

• Each queue can have its own scheduling algorithm• Scheduling must be done between the queues• Simplest: Fixed priority scheduling

– One possibility: 100% priority to foreground• Serve foreground if any; serve background only if no foreground• Problem: possibility of starvation.

– Another possibility: 80/20 priority using time slices • 80% to foreground using RR• 20% to background using FCFS

CSS 430: Operating Systems - CPU Scheduling 63

Page 64: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multilevel Queue Scheduling

64CSS 430: Operating Systems - CPU Scheduling

Page 65: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multilevel Feedback Queue

• Process can migrate among queues– Queue placement based on

past behavior (feedback)• Parameters:

– number of queues– scheduling algorithms for each queue– when to upgrade a process– when to demote a process– when to select a queue for service

CSS 430: Operating Systems - CPU Scheduling 65

Page 66: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Example of Multilevel Feedback Queue

• Three queues: Q0, Q1, Q2; all use FCFS• If blocked for I/O, job returned to Q0

when I/O done• New jobs enter queue Q0

– When activated (given CPU), job receives 8 ms– If not done, job is preempted, demoted to Q1

• Jobs in Q1– When activated, job receives 16 ms– If not done, job is preempted, demoted to Q2

• Jobs in Q2– When activated, job runs until terminated or

interrupted– If not done, job is preempted, returned to Q2

CSS 430: Operating Systems - CPU Scheduling 66

Page 67: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Java Thread Scheduling

• Priority-based• FCFS within same priority level

• May be preemptive or non-preemptive• A runnable thread executes until:

• Time quantum expires• Blocks for I/O• Exits its run() method• Yields to another thread via yield()

• “cooperative multitasking”

CSS 430: Operating Systems - CPU Scheduling 67

Page 68: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Java Thread Priorities

• Values: 1 .. 10– Thread.MAX_PRIORITY = 10– Thread.NORM_PRIORITY = 5– Thread.MIN_PRIORITY = 1

• Methods:– int Thread.getPriority()– Thread.setPriority( int newPriority )

CSS 430: Operating Systems - CPU Scheduling 68

Page 69: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Mapping Java Thread Priorities

• JVM runs on host• Java Thread priorities (user)

host kernel priorities• Example: Win32• ThreadOS Scheduler:

– Sets its priority to 6– Sets user thread priorities to 4 (running) or 2 (ready)– Goes to sleep

• effectively yields to priority 4 user thread

CSS 430: Operating Systems - CPU Scheduling 69

Page 70: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Program 2

CSS 430: Operating Systems - CPU Scheduling 70

http://courses.washington.edu/css430/joemcc/prog/prog2b.html

Page 71: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multiple-Processor Scheduling• CPU scheduling more complex when multiple CPUs are

available• Homogeneous processors within a multiprocessor• Asymmetric multiprocessing: only one processor accesses

the system data structures, alleviating the need for data sharing (master & slave processors)

• Symmetric multiprocessing (SMP): each processor is self-scheduling, all processes in common ready queue, or each has its own private queue of ready processes

• Processor affinity: process has affinity for processor on which it is currently running– soft affinity: policy– hard affinity: mechanism

CSS 430: Operating Systems - CPU Scheduling 71

Page 72: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 72

http://hadoop.apache.org/common/docs/current/hdfs_design.html

Page 73: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

NUMA and CPU Scheduling

73CSS 430: Operating Systems - CPU Scheduling

NUMA = non-uniform memory access

Page 74: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multicore Processors

• Multiple threads per core– Memory stall: CPU awaits instruction/data fetch– Multiple hardware threads: logical processors

• Each logical processor can run a software thread

– Enables progress to made on one thread while memory is updated

– Two approaches• Coarse-grained multithreading (latency event)• Fine-grained multithreading (per instruction)

CSS 430: Operating Systems - CPU Scheduling 74

Page 75: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Multithreaded Multicore System

CSS 430: Operating Systems - CPU Scheduling 75

Page 76: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Operating System Examples

• Solaris scheduling

• Windows XP scheduling

• Linux scheduling

CSS 430: Operating Systems - CPU Scheduling 76

Page 77: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Solaris Scheduling

77CSS 430: Operating Systems - CPU Scheduling

Dispatch Table (for TS, IA)

Page 78: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Windows XP Scheduling

78CSS 430: Operating Systems - CPU Scheduling

Priority classes

Rel

ativ

e pr

iorit

ies

Processes within a variable class (not real-time):• Expired time quantum reduce priority• Released from wait increase priorityForeground process gets extra boost

Page 79: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Linux priorities & time quanta

CSS 430: Operating Systems - CPU Scheduling 79

Page 80: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

runqueue: active & expired

CSS 430: Operating Systems - CPU Scheduling 80

When all task lists in active array are empty,exchange active array & expired array

Page 81: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?

CSS 430: Operating Systems - CPU Scheduling 81

Page 82: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?

CSS 430: Operating Systems - CPU Scheduling 82

FCFS, SJF & RR average wait times?

Page 83: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 83

FCFS, SJF & RR: average wait times?

Page 84: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 84

FCFS, SJF & RR: average wait times?

Page 85: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 85

FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28

FCFS, SJF & RR: average wait times?

Page 86: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 86

FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28

FCFS, SJF & RR: average wait times?

Page 87: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 87

FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28

SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13

FCFS, SJF & RR: average wait times?

Page 88: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 88

FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28

SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13

FCFS, SJF & RR: average wait times?

Page 89: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

CSS 430: Operating Systems - CPU Scheduling 89

FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28

SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13

RR: (0 + 32 + 20 + 23 + 40) / 5 = 23

FCFS, SJF & RR: average wait times?

Page 90: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?– Analysis

• Deterministic models: predetermined workload

CSS 430: Operating Systems - CPU Scheduling 90

Page 91: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?– Analysis

• Deterministic models: predetermined workload• Queueing models: use probability distribution

– Arrival rate– Service rate

CSS 430: Operating Systems - CPU Scheduling 91

Page 92: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?– Analysis

• Deterministic models: predetermined workload• Queueing models: use probability distribution

– Arrival rate– Service rate

– Simulation• ThreadOS

CSS 430: Operating Systems - CPU Scheduling 92

Page 93: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

Algorithm Evaluation

• How can we compare scheduling algorithms?– Analysis

• Deterministic models: predetermined workload• Queueing models: use probability distribution

– Arrival rate– Service rate

– Simulation• ThreadOS

– Implementation

CSS 430: Operating Systems - CPU Scheduling 93

Page 94: Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1

For next time

• Readings– Chapter 6: Process Synchronization

CSS 430: Operating Systems - CPU Scheduling 94