Chapter 5 CPU Scheduling Joe Mc Carthy CSS

  • Slides: 94
Download presentation
Chapter 5: CPU Scheduling Joe Mc. Carthy CSS 430: Operating Systems - CPU Scheduling

Chapter 5: CPU Scheduling Joe Mc. Carthy CSS 430: Operating Systems - CPU Scheduling 1

Chapter 5: CPU Scheduling • • Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling

Chapter 5: CPU Scheduling • • Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation Material derived, in part, from Operating Systems Concepts with Java, 8 th Ed. © 2009 Silberschatz, Galvin & Gagne CSS 430: Operating Systems - CPU Scheduling 2

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 3

CPU [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 3

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 4

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 4

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 5

CPU Scheduling [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 5

CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling

CPU Scheduling Multiprogramming [from Chapter 1: Introduction] CSS 430: Operating Systems - CPU Scheduling 6

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 7

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 7

CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems -

CPU Scheduling Process Control Blocks [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 8

CPU Scheduling Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes]

CPU Scheduling Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 9

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 10

CPU Scheduling [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 10

CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 11

CPU Queues [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 11

Process States [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 12

Process States [from Chapter 3: Processes] CSS 430: Operating Systems - CPU Scheduling 12

Basic Concepts • Goal: Maximize CPU utilization via optimized multiprogramming • CPU–I/O Burst Cycle:

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

Histogram of CPU-burst Durations CSS 430: Operating Systems - CPU Scheduling 14

Histogram of CPU-burst Durations CSS 430: Operating Systems - CPU Scheduling 14

CPU Scheduler • Selects next process to run (i. e. , to get CPU

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

CPU Scheduler • Selects next process to run (i. e. , to get CPU

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

Dispatcher • Activates a process (gives control of the CPU to the process) selected

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

Dispatcher • Activates a process (gives control of the CPU to the process) selected

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

Dispatcher • Activates a process (gives control of the CPU to the process) selected

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

Scheduling Milestones • Submission Time – Time when job is submitted – E. g.

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

Job (Process/Thread) Times • Response Time – Time before job is first activated –

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

Scheduling Criteria • CPU utilization – % of time CPU is executing user processes

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

Scheduling Optimization Criteria • CPU utilization: maximize – % of time CPU is executing

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

First-Come, First-Served (FCFS) • FCFS policy often implemented with FIFO queue • Example: 3

First-Come, First-Served (FCFS) • FCFS policy often implemented with FIFO queue • Example: 3 processes with the following burst times: P 1 = 24; P 2 = 3; P 3 = 3 • Suppose that the processes arrive at t 0 in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: P 1 0 P 2 24 P 3 27 30 • Waiting times • Average waiting time: CSS 430: Operating Systems - CPU Scheduling 24

First-Come, First-Served (FCFS) • FCFS policy often implemented with FIFO queue • Example: 3

First-Come, First-Served (FCFS) • FCFS policy often implemented with FIFO queue • Example: 3 processes with the following burst times: P 1 = 24; P 2 = 3; P 3 = 3 • Suppose that the processes arrive at t 0 in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: P 1 0 P 2 24 P 3 27 30 • Waiting times: P 1 = 0; P 2 = 24; P 3 = 27 • Average waiting time: (0 + 24 + 27) / 3 = 17. 0 CSS 430: Operating Systems - CPU Scheduling 25

FCFS Scheduling Suppose that the processes arrive in the order: P 2 , P

FCFS Scheduling Suppose that the processes arrive in the order: P 2 , P 3 , P 1 • The Gantt chart for the schedule is: P 2 0 P 3 3 P 1 6 30 • Waiting times: • Average waiting time: CSS 430: Operating Systems - CPU Scheduling 26

FCFS Scheduling Suppose that the processes arrive in the order: P 2 , P

FCFS Scheduling Suppose that the processes arrive in the order: P 2 , P 3 , P 1 • The Gantt chart for the schedule is: P 2 0 P 3 3 P 1 6 30 • Waiting times: P 1 = 6; P 2 = 0; ; P 3 = 3 • Average waiting time: (6 + 0 + 3) / 3 = 3. 0 CSS 430: Operating Systems - CPU Scheduling 27

FCFS Scheduling P 1 0 P 2 24 P 3 27 Order: P 1,

FCFS Scheduling P 1 0 P 2 24 P 3 27 Order: P 1, P 2, P 3 Average wait: 17. 0 P 2 30 0 P 3 3 P 1 6 30 Order: P 2, P 3, P 1 Average wait: 3. 0 • Convoy effect: short process[es] stuck behind long process • How could we achieve minimal average waiting time? CSS 430: Operating Systems - CPU Scheduling 28

Shortest-Job-First (SJF) Scheduling • Always schedule the shortest job first – Select process with

Shortest-Job-First (SJF) Scheduling • Always schedule the shortest job first – Select process with shortest next CPU burst. P 2 0 P 3 3 P 1 6 • Optimal average waiting time 30 – For any set of processes CSS 430: Operating Systems - CPU Scheduling 29

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 • SJF scheduling chart Burst Time 6 8 7 3 CSS 430: Operating Systems - CPU Scheduling 30

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 • SJF scheduling chart P 1 P 4 0 P 3 9 3 Burst Time 6 8 7 3 P 2 16 24 • Average waiting time: CSS 430: Operating Systems - CPU Scheduling 31

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0

Example of SJF Process Arrival Time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 • SJF scheduling chart P 1 P 4 0 3 Burst Time 6 8 7 3 P 3 9 P 2 16 24 • Average waiting time: (3 + 16 + 9 + 0) / 4 = 7. 0 CSS 430: Operating Systems - CPU Scheduling 32

Another SJF example • • Example: Process P 1 P 2 P 3 P

Another SJF example • • Example: Process P 1 P 2 P 3 P 4 Non preemptive SJF Arrival Time Burst Time 0 7 2 4 4 1 5 4 P 1 0 P 3 4 5 2 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 7 P 2 8 P 4 12 P 1(7) 16 P 1‘s wating time = 0 P 2‘s wating time = 6 P 2(4) P 3‘s wating time = 3 P 3(1) P 4‘s wating time = 7 P 4(4) CSS 430 CPU Scheduling 33

Another SJF example (cont’d) • • Example: Process P 1 P 2 P 3

Another SJF example (cont’d) • • Example: Process P 1 P 2 P 3 P 4 Preemptive SJF P 1(7) P 1 P 2 P 3 0 2 P 1(5) 4 Arrival Time Burst Time 0 7 2 4 4 1 5 4 Average waiting time = (9 + 1 + 0 +2)/4 = 3 P 2 5 P 4 7 P 1 11 16 P 1‘s wating time = 9 P 2‘s wating time = 1 P 2(4) P 2(2) P 3‘s wating time = 0 P 3(1) P 4‘s wating time = 2 P 4(4) CSS 430 CPU Scheduling 34

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest next CPU burst. P 2 0 P 3 3 P 1 6 • Optimal average waiting time 30 – For any set of processes • Only one problem CSS 430: Operating Systems - CPU Scheduling 35

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest next CPU burst. P 2 0 P 3 3 P 1 6 • Optimal average waiting time 30 – For any set of processes • Only one problem http: //www. hedgeworld. com/blog/? p=3570 CSS 430: Operating Systems - CPU Scheduling 36

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest

Shortest-Job-First Scheduling • Always schedule the shortest job first – Select process with shortest next CPU burst. P 2 0 P 3 3 P 1 6 • Optimal average waiting time 30 – For any set of processes • Only one problem http: //www. hedgeworld. com/blog/? p=3570 – How to know the length of the next CPU burst? CSS 430: Operating Systems - CPU Scheduling 37

Determining Length of Next CPU Burst • Can only estimate length of future bursts

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

Examples of Exponential Averaging • =0 – n+1 = n – Recent history does

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

Predicting Length of Next CPU Burst CSS 430: Operating Systems - CPU Scheduling 40

Predicting Length of Next CPU Burst CSS 430: Operating Systems - CPU Scheduling 40

Shortest Remaining Time First • Pre-emptive version of SJF CSS 430: Operating Systems -

Shortest Remaining Time First • Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling 41

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time? CSS 430:

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time? CSS 430: Operating Systems - CPU Scheduling 42

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 –

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 – 1) + (17 – 2) + (5 – 3)] / 4 = 6. 5 CSS 430: Operating Systems - CPU Scheduling 43

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 –

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 – 1) + (17 – 2) + (5 – 3)] / 4 = 6. 5 Non pre-emptive SJF? CSS 430: Operating Systems - CPU Scheduling 44

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 –

Shortest Remaining Time First • Pre-emptive version of SJF Average wait time: [(10 – 1) + (17 – 2) + (5 – 3)] / 4 = 6. 5 Non pre-emptive SJF: [0 + (8 – 1) + (17 – 2) + (12 – 3)] / 4 = 7. 75 CSS 430: Operating Systems - CPU Scheduling 45

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

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 Thread. OS] • SJF priority: • FCFS priority: CSS 430: Operating Systems - CPU Scheduling 46

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

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 Thread. OS) • SJF priority: predicted next CPU burst time • FCFS priority: arrival time CSS 430: Operating Systems - CPU Scheduling 47

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

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 Thread. OS) SJF priority: predicted next CPU burst time FCFS priority: arrival time Problem? CSS 430: Operating Systems - CPU Scheduling 48

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

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 Thread. OS) 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

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

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 Thread. OS) 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

Priority Scheduling Example Average wait time: (6 + 0 + 16 + 18 +

Priority Scheduling Example Average wait time: (6 + 0 + 16 + 18 + 1) / 5 = 8. 2 CSS 430: Operating Systems - CPU Scheduling 51

Priority Scheduling Example Average wait time: (6 + 0 + 16 + 18 +

Priority Scheduling Example 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: CSS 430: Operating Systems - CPU Scheduling 52

Priority Scheduling Example Average wait time: (6 + 0 + 16 + 18 +

Priority Scheduling Example 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 CSS 430: Operating Systems - CPU Scheduling 53

Round Robin (RR) • Each process gets time quantum (q) of CPU time (typically

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

Example of RR with Time Quantum = 4 Process P 1 P 2 P

Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 Burst Time 24 3 3 • The Gantt chart is: CSS 430: Operating Systems - CPU Scheduling 55

Example of RR with Time Quantum = 4 Process P 1 P 2 P

Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 Burst Time 24 3 3 • The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 22 26 P 1 30 • Compare to SJF: P 2 0 P 3 3 P 1 6 CSS 430: Operating Systems - CPU Scheduling 30 56

Another RR example • • Each process is given CPU time in turn, (i.

Another RR example • • 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 = 20 Process Burst Time Wait Time P 1 53 57 +24 = 81 P 2 17 20 P 3 68 37 + 40 + 17= 94 P 4 24 57 + 40 = 97 P 1 P 2 P 3 P 4 P 1 P 3 0 20 37 57 77 97 117 121 134 154 162 P 1(33) 24 P 1(13) P 1(53) 57 P 2(17) 20 P 3(68) P 4(24) 37 57 40 P 3(48) 40 17 P 3(28) P 3(8) P 4(4) Average wait time = (81+20+94+97)/4 = 73 CSS 430 CPU Scheduling 57

Time Quantum & Context Switch Time • Performance issues: – q large – q

Time Quantum & Context Switch Time • Performance issues: – q large – q small CSS 430: Operating Systems - CPU Scheduling 58

Time Quantum & Context Switch Time • Performance issues: – q large FCFS –

Time Quantum & Context Switch Time • Performance issues: – q large FCFS – q small context switch overhead too high CSS 430: Operating Systems - CPU Scheduling 59

Time Quantum Turnaround Time CSS 430: Operating Systems - CPU Scheduling 60

Time Quantum Turnaround Time CSS 430: Operating Systems - CPU Scheduling 60

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

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

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

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

Multilevel Queue • Ready queue can be partitioned into separate queues – E. g.

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

Multilevel Queue Scheduling CSS 430: Operating Systems - CPU Scheduling 64

Multilevel Queue Scheduling CSS 430: Operating Systems - CPU Scheduling 64

Multilevel Feedback Queue • Process can migrate among queues – Queue placement based on

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

Example of Multilevel Feedback Queue • Three queues: Q 0, Q 1, Q 2;

Example of Multilevel Feedback Queue • Three queues: Q 0, Q 1, Q 2; all use FCFS • If blocked for I/O, job returned to Q 0 when I/O done • New jobs enter queue Q 0 – When activated (given CPU), job receives 8 ms – If not done, job is preempted, demoted to Q 1 • Jobs in Q 1 – When activated, job receives 16 ms – If not done, job is preempted, demoted to Q 2 • Jobs in Q 2 – When activated, job runs until terminated or interrupted – If not done, job is preempted, returned to Q 2 CSS 430: Operating Systems - CPU Scheduling 66

Java Thread Scheduling • Priority-based • FCFS within same priority level • May be

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 run() method Yields to another thread via yield() • “cooperative multitasking” CSS 430: Operating Systems - CPU Scheduling 67

Java Thread Priorities • Values: 1. . 10 – Thread. MAX_PRIORITY – Thread. NORM_PRIORITY

Java Thread Priorities • Values: 1. . 10 – Thread. MAX_PRIORITY – Thread. NORM_PRIORITY – Thread. MIN_PRIORITY = 10 = 5 = 1 • Methods: – int Thread. get. Priority() – Thread. set. Priority( int new. Priority ) CSS 430: Operating Systems - CPU Scheduling 68

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

Mapping Java Thread Priorities • JVM runs on host • Java Thread priorities (user) host kernel priorities • Example: Win 32 • Thread. OS 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

Program 2 http: //courses. washington. edu/css 430/joem cc/prog 2 b. html CSS 430: Operating

Program 2 http: //courses. washington. edu/css 430/joem cc/prog 2 b. html CSS 430: Operating Systems - CPU Scheduling 70

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

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 selfscheduling, 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

http: //hadoop. apache. org/common/docs/current/hdfs_design. html CSS 430: Operating Systems - CPU Scheduling 72

http: //hadoop. apache. org/common/docs/current/hdfs_design. html CSS 430: Operating Systems - CPU Scheduling 72

NUMA and CPU Scheduling NUMA = non-uniform memory access CSS 430: Operating Systems -

NUMA and CPU Scheduling NUMA = non-uniform memory access CSS 430: Operating Systems - CPU Scheduling 73

Multicore Processors • Multiple threads per core – Memory stall: CPU awaits instruction/data fetch

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

Multithreaded Multicore System CSS 430: Operating Systems - CPU Scheduling 75

Multithreaded Multicore System CSS 430: Operating Systems - CPU Scheduling 75

Operating System Examples • Solaris scheduling • Windows XP scheduling • Linux scheduling CSS

Operating System Examples • Solaris scheduling • Windows XP scheduling • Linux scheduling CSS 430: Operating Systems - CPU Scheduling 76

Solaris Scheduling Dispatch Table (for TS, IA) CSS 430: Operating Systems - CPU Scheduling

Solaris Scheduling Dispatch Table (for TS, IA) CSS 430: Operating Systems - CPU Scheduling 77

Windows XP Scheduling Relative priorities Priority classes Processes within a variable class (not real-time):

Windows XP Scheduling Relative priorities Priority classes Processes within a variable class (not real-time): • Expired time quantum reduce priority • Released from wait increase priority Foreground process gets extra boost CSS 430: Operating Systems - CPU Scheduling 78

Linux priorities & time quanta CSS 430: Operating Systems - CPU Scheduling 79

Linux priorities & time quanta CSS 430: Operating Systems - CPU Scheduling 79

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

runqueue: active & expired When all task lists in active array are empty, exchange active array & expired array CSS 430: Operating Systems - CPU Scheduling 80

Algorithm Evaluation • How can we compare scheduling algorithms? CSS 430: Operating Systems -

Algorithm Evaluation • How can we compare scheduling algorithms? CSS 430: Operating Systems - CPU Scheduling 81

Algorithm Evaluation • How can we compare scheduling algorithms? FCFS, SJF & RR average

Algorithm Evaluation • How can we compare scheduling algorithms? FCFS, SJF & RR average wait times? CSS 430: Operating Systems - CPU Scheduling 82

FCFS, SJF & RR: average wait times? CSS 430: Operating Systems - CPU Scheduling

FCFS, SJF & RR: average wait times? CSS 430: Operating Systems - CPU Scheduling 83

FCFS, SJF & RR: average wait times? CSS 430: Operating Systems - CPU Scheduling

FCFS, SJF & RR: average wait times? CSS 430: Operating Systems - CPU Scheduling 84

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 +

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 CSS 430: Operating Systems - CPU Scheduling 85

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 +

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 CSS 430: Operating Systems - CPU Scheduling 86

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 +

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13 CSS 430: Operating Systems - CPU Scheduling 87

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 +

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13 CSS 430: Operating Systems - CPU Scheduling 88

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 +

FCFS, SJF & RR: average wait times? FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13 RR: (0 + 32 + 20 + 23 + 40) / 5 = 23 CSS 430: Operating Systems - CPU Scheduling 89

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models:

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models: predetermined workload CSS 430: Operating Systems - CPU Scheduling 90

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models:

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

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models:

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models: predetermined workload • Queueing models: use probability distribution – Arrival rate – Service rate – Simulation • Thread. OS CSS 430: Operating Systems - CPU Scheduling 92

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models:

Algorithm Evaluation • How can we compare scheduling algorithms? – Analysis • Deterministic models: predetermined workload • Queueing models: use probability distribution – Arrival rate – Service rate – Simulation • Thread. OS – Implementation CSS 430: Operating Systems - CPU Scheduling 93

For next time • Readings – Chapter 6: Process Synchronization CSS 430: Operating Systems

For next time • Readings – Chapter 6: Process Synchronization CSS 430: Operating Systems - CPU Scheduling 94