Download presentation
Presentation is loading. Please wait.
Published byIrma O’Neal’ Modified over 9 years ago
1
Chapter 5: CPU Scheduling Joe McCarthy CSS 430: Operating Systems - CPU Scheduling1
2
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, from Operating Systems Concepts with Java, 8 th Ed. © 2009 Silberschatz, Galvin & Gagne
3
CPU CSS 430: Operating Systems - CPU Scheduling3 [from Chapter 1: Introduction]
4
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling4 [from Chapter 1: Introduction]
5
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling5 [from Chapter 1: Introduction]
6
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling6 [from Chapter 1: Introduction] Multiprogramming
7
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling7 [from Chapter 3: Processes]
8
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling8 Process Control Blocks [from Chapter 3: Processes]
9
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling9 Can all PCBs have access to CPU simultaneously? [from Chapter 3: Processes]
10
CPU Scheduling CSS 430: Operating Systems - CPU Scheduling10 [from Chapter 3: Processes]
11
CPU Queues CSS 430: Operating Systems - CPU Scheduling11 [from Chapter 3: Processes]
12
Process States CSS 430: Operating Systems - CPU Scheduling12 [from Chapter 3: Processes]
13
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 Scheduling13
14
Histogram of CPU-burst Durations CSS 430: Operating Systems - CPU Scheduling14
15
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 Scheduling15
16
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 Scheduling16
17
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 Scheduling17
18
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 Scheduling18
19
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 Scheduling19
20
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 Scheduling20
21
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 Scheduling21
22
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 Scheduling22
23
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 Scheduling23
24
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: Waiting times Average waiting time: CSS 430: Operating Systems - CPU Scheduling24 P1P1 P2P2 P3P3 27300
25
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: 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 Scheduling25 P1P1 P2P2 P3P3 2427300
26
FCFS Scheduling Suppose that the processes arrive in the order: P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting times: Average waiting time: CSS 430: Operating Systems - CPU Scheduling26 P1P1 P3P3 P2P2 63300
27
FCFS Scheduling Suppose that the processes arrive in the order: P 2, P 3, P 1 The Gantt chart for the schedule is: 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 Scheduling27 P1P1 P3P3 P2P2 63300
28
FCFS Scheduling Convoy effect: short process[es] stuck behind long process How could we achieve minimal average waiting time? CSS 430: Operating Systems - CPU Scheduling28 P1P1 P3P3 P2P2 63300 P1P1 P2P2 P3P3 2427300 Order: P 1, P 2, P 3 Average wait: 17.0 Order: P 2, P 3, P 1 Average wait: 3.0
29
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 Scheduling29 P1P1 P3P3 P2P2 63300
30
Example of SJF ProcessArrival TimeBurst Time P 1 0.06 P 2 2.08 P 3 4.07 P 4 5.03 SJF scheduling chart CSS 430: Operating Systems - CPU Scheduling30
31
Example of SJF ProcessArrival TimeBurst Time P 1 0.06 P 2 2.08 P 3 4.07 P 4 5.03 SJF scheduling chart Average waiting time: CSS 430: Operating Systems - CPU Scheduling31 P4P4 P3P3 P1P1 3 16 0 9 P2P2 24
32
Example of SJF ProcessArrival TimeBurst Time P 1 0.06 P 2 2.08 P 3 4.07 P 4 5.03 SJF scheduling chart Average waiting time: (3 + 16 + 9 + 0) / 4 = 7.0 CSS 430: Operating Systems - CPU Scheduling32 P4P4 P3P3 P1P1 3 16 0 9 P2P2 24
33
CSS430 CPU Scheduling33 Another SJF example Example:ProcessArrival Time Burst Time P 1 07 P 2 24 P 3 41 P 4 54 Non preemptive SJF P1P1 P3P3 P2P2 7 P 1 (7) 160 P4P4 812 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 2 4 5 P 2 (4) P 3 (1) P 4 (4) P 1 ‘s wating time = 0 P 2 ‘s wating time = 6 P 3 ‘s wating time = 3 P 4 ‘s wating time = 7
34
CSS430 CPU Scheduling34 Another SJF example (cont’d) Example:ProcessArrival Time Burst Time P 1 07 P 2 24 P 3 41 P 4 54 Preemptive SJF P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 P 1 (7) P 2 (4) P 3 (1) P 4 (4) P 1 ‘s wating time = 9 P 2 ‘s wating time = 1 P 3 ‘s wating time = 0 P 4 ‘s wating time = 2 P 1 (5) P 2 (2)
35
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 Scheduling35 P1P1 P3P3 P2P2 63300
36
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 Scheduling36 P1P1 P3P3 P2P2 63300 http://www.hedgeworld.com/blog/?p=3570
37
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 Scheduling37 P1P1 P3P3 P2P2 63300 http://www.hedgeworld.com/blog/?p=3570
38
Determining Length of Next CPU Burst Can only estimate length of future bursts Use exponential averaging of previous bursts CSS 430: Operating Systems - CPU Scheduling38
39
Examples of Exponential Averaging = 0 – n+1 = n – Recent history does not count = 1 – n+1 = t n – Only the most recent CPU burst counts = 0.5 (typically) If we expand the formula, we get: n+1 = t n +(1 - ) t n -1 + … +(1 - ) j t n -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 Scheduling39
40
Predicting Length of Next CPU Burst CSS 430: Operating Systems - CPU Scheduling40
41
Shortest Remaining Time First Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling41
42
Shortest Remaining Time First Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling42 Average wait time?
43
Shortest Remaining Time First Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling43 Average wait time: [(10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)] / 4 = 6.5
44
Shortest Remaining Time First Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling44 Average wait time: [(10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)] / 4 = 6.5 Non pre-emptive SJF?
45
Shortest Remaining Time First Pre-emptive version of SJF CSS 430: Operating Systems - CPU Scheduling45 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
46
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 Scheduling46
47
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 Scheduling47
48
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 Scheduling48
49
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 Scheduling49
50
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 Scheduling50
51
Priority Scheduling Example CSS 430: Operating Systems - CPU Scheduling51 Average wait time: (6 + 0 + 16 + 18 + 1) / 5 = 8.2
52
Priority Scheduling Example CSS 430: Operating Systems - CPU Scheduling52 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:
53
Priority Scheduling Example CSS 430: Operating Systems - CPU Scheduling53 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
54
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 Scheduling54
55
ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Example of RR with Time Quantum = 4 CSS 430: Operating Systems - CPU Scheduling55
56
Example of RR with Time Quantum = 4 ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Compare to SJF: CSS 430: Operating Systems - CPU Scheduling56 P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630 P1P1 P3P3 P2P2 63 0
57
CSS430 CPU Scheduling57 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 = 20 ProcessBurst TimeWait Time P 1 5357 +24 = 81 P 2 1720 P 3 6837 + 40 + 17= 94 P 4 2457 + 40 = 97 Another RR example P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162 Average wait time = (81+20+94+97)/4 = 73 57 20 37 57 24 40 17 P 1 (53) P 2 (17) P 3 (68) P 4 (24) P 1 (33) P 1 (13) P 3 (48) P 3 (28) P 3 (8) P 4 (4)
58
Time Quantum & Context Switch Time Performance issues: – q large – q small CSS 430: Operating Systems - CPU Scheduling58
59
Time Quantum & Context Switch Time Performance issues: – q large FCFS – q small context switch overhead too high CSS 430: Operating Systems - CPU Scheduling59
60
Time Quantum Turnaround Time CSS 430: Operating Systems - CPU Scheduling60
61
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 Scheduling61
62
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 Scheduling62
63
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 Scheduling63
64
Multilevel Queue Scheduling 64CSS 430: Operating Systems - CPU Scheduling
65
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 Scheduling65
66
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 Scheduling66
67
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 Scheduling67
68
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 Scheduling68
69
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 Scheduling69
70
Program 2 CSS 430: Operating Systems - CPU Scheduling70 http://courses.washington.edu/css430/joem cc/prog/prog2b.html
71
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 Scheduling71
72
CSS 430: Operating Systems - CPU Scheduling72 http://hadoop.apache.org/common/docs/current/hdfs_design.html
73
NUMA and CPU Scheduling 73CSS 430: Operating Systems - CPU Scheduling NUMA = non-uniform memory access
74
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 Scheduling74
75
Multithreaded Multicore System CSS 430: Operating Systems - CPU Scheduling75
76
Operating System Examples Solaris scheduling Windows XP scheduling Linux scheduling CSS 430: Operating Systems - CPU Scheduling76
77
Solaris Scheduling 77CSS 430: Operating Systems - CPU Scheduling Dispatch Table (for TS, IA)
78
Windows XP Scheduling 78CSS 430: Operating Systems - CPU Scheduling Priority classes Relative priorities Processes within a variable class (not real-time): Expired time quantum reduce priority Released from wait increase priority Foreground process gets extra boost
79
Linux priorities & time quanta CSS 430: Operating Systems - CPU Scheduling79
80
runqueue: active & expired CSS 430: Operating Systems - CPU Scheduling80 When all task lists in active array are empty, exchange active array & expired array
81
Algorithm Evaluation How can we compare scheduling algorithms? CSS 430: Operating Systems - CPU Scheduling81
82
Algorithm Evaluation How can we compare scheduling algorithms? CSS 430: Operating Systems - CPU Scheduling82 FCFS, SJF & RR average wait times?
83
CSS 430: Operating Systems - CPU Scheduling83 FCFS, SJF & RR: average wait times?
84
CSS 430: Operating Systems - CPU Scheduling84 FCFS, SJF & RR: average wait times?
85
CSS 430: Operating Systems - CPU Scheduling85 FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 FCFS, SJF & RR: average wait times?
86
CSS 430: Operating Systems - CPU Scheduling86 FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 FCFS, SJF & RR: average wait times?
87
CSS 430: Operating Systems - CPU Scheduling87 FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13 FCFS, SJF & RR: average wait times?
88
CSS 430: Operating Systems - CPU Scheduling88 FCFS: (0 + 10 + 39 + 42 + 49) / 5 = 28 SJF: (10 + 32 + 0 + 3 + 20) / 5 = 13 FCFS, SJF & RR: average wait times?
89
CSS 430: Operating Systems - CPU Scheduling89 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?
90
Algorithm Evaluation How can we compare scheduling algorithms? – Analysis Deterministic models: predetermined workload CSS 430: Operating Systems - CPU Scheduling90
91
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 Scheduling91
92
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 Scheduling92
93
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 Scheduling93
94
For next time Readings – Chapter 6: Process Synchronization CSS 430: Operating Systems - CPU Scheduling94
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.