chapter 6: cpu scheduling (调度)

41
CHAPTER 6: CPU SCHEDULIN G 调调调 () Scheduling Concepts Scheduling Concepts Scheduling Criteria Scheduling Criteria Scheduling Algorithms Scheduling Algorithms Multiple-Processor Scheduling Multiple-Processor Scheduling Real-Time Scheduling Real-Time Scheduling Scheduling Algorithm Evaluation Scheduling Algorithm Evaluation

Upload: daquan-neal

Post on 03-Jan-2016

100 views

Category:

Documents


0 download

DESCRIPTION

CHAPTER 6: CPU SCHEDULING (调度). Scheduling Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Scheduling Algorithm Evaluation. SCHEDULING CONCEPTS. To maximize CPU utilization with multiprogramming - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER 6:  CPU SCHEDULING  (调度)

CHAPTER 6: CPU SCHEDULING (调度) Scheduling ConceptsScheduling Concepts Scheduling Criteria Scheduling Criteria Scheduling AlgorithmsScheduling Algorithms Multiple-Processor SchedulingMultiple-Processor Scheduling Real-Time SchedulingReal-Time Scheduling Scheduling Algorithm EvaluationScheduling Algorithm Evaluation

Page 2: CHAPTER 6:  CPU SCHEDULING  (调度)

SCHEDULING CONCEPTS

To maximize CPU utilization with To maximize CPU utilization with multiprogrammingmultiprogramming

CPU–I/O Burst Cycle: CPU–I/O Burst Cycle: Process execution consists of a Process execution consists of a sequencesequence of CPU execution and of CPU execution and I/O wait.I/O wait. CPU burstCPU burst I/O waitI/O wait CPU burstCPU burst I/O waitI/O wait ……..

Page 3: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling concepts: Histogram

Page 4: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling concepts: CPU Scheduler ( 调度器 )

CPU scheduler selects a process from the ready processes CPU scheduler selects a process from the ready processes and allocates the CPU to it.and allocates the CPU to it.

When to use CPU scheduler?When to use CPU scheduler?

1.1. When a process terminates.When a process terminates.

2.2. When a process switches from running to waiting state.When a process switches from running to waiting state.

3.3. When a process switches from running to ready state.When a process switches from running to ready state.

4.4. When a process switches from waiting to ready.When a process switches from waiting to ready. Scheduling under 1 and 2 only is Scheduling under 1 and 2 only is non-preemptive(non-preemptive( 非抢非抢

占占 )).. Win 3.xWin 3.x

Scheduling under all conditions is Scheduling under all conditions is preemptive(preemptive( 抢占抢占 ).). Win 9x, Win NT/2K/XP, LinuxWin 9x, Win NT/2K/XP, Linux

Page 5: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling concepts: Process dispatcher ( 派遣器 )

Dispatcher module gives the CPU control to the Dispatcher module gives the CPU control to the process selected by the short-term scheduler; this process selected by the short-term scheduler; this involves:involves: switching context (saving context and restoring switching context (saving context and restoring

context)context) switching to user mode (from monitor mode switching to user mode (from monitor mode

user mode)user mode) jumping to the proper location in the user program jumping to the proper location in the user program

to restart that program (PC counter)to restart that program (PC counter) Dispatch latencyDispatch latency – the time it takes for the dispatcher – the time it takes for the dispatcher

to stop one process and start another running.to stop one process and start another running.

Page 6: CHAPTER 6:  CPU SCHEDULING  (调度)

SCHEDULING CRITERIA ( 标准 ) CPU CPU utilization (utilization ( 使用率使用率 ):): keep the CPU as busy as keep the CPU as busy as

possible.possible. CPU CPU throughput (throughput ( 吞吐量吞吐量 ):): number of processes that number of processes that

complete their execution per time unit.complete their execution per time unit. Process Process turnaround time (turnaround time ( 周转时间周转时间 ):): amount of tim amount of tim

e to execute a particular process.e to execute a particular process. ProcessProcess waiting time (waiting time ( 等时间等时间 ):): amount of time a pro amount of time a pro

cess has been waiting in the ready queue.cess has been waiting in the ready queue. Process Process response time (response time ( 响应时间响应时间 ):): amount of time it amount of time it

takes from when a request was submitted until the first takes from when a request was submitted until the first response is produced, response is produced, notnot output (for time-sharing envi output (for time-sharing environment).ronment).

Page 7: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling criteria To maximize or minimize some average measures:To maximize or minimize some average measures:

Maximize CPU utilization.Maximize CPU utilization. Maximize CPU throughput.Maximize CPU throughput. Minimize process turnaround time.Minimize process turnaround time. Minimize process waiting time.Minimize process waiting time. Minimize process response time.Minimize process response time.

To maximize or minimize more average measures:To maximize or minimize more average measures: Peak value (Peak value ( 峰值峰值 )) Expectation (Expectation ( 数学期望数学期望 ) (Average)) (Average) Variance (Variance ( 方差方差 ))

Page 8: CHAPTER 6:  CPU SCHEDULING  (调度)

SCHEDULING ALGORITHMS

Scheduling algorithmsScheduling algorithms First come first served (FCFS) First come first served (FCFS) (先到先行调(先到先行调

度)度) Shortest job first (SJF) Shortest job first (SJF) (最短作业优先调度)(最短作业优先调度) Priority scheduling Priority scheduling (优先权调度)(优先权调度) Round robin (RR) Round robin (RR) (轮转法调度)(轮转法调度) Multilevel queue algorithm Multilevel queue algorithm (多级队列调度)(多级队列调度) Multilevel feedback queue algorithm Multilevel feedback queue algorithm (多级反馈(多级反馈

队列调度)队列调度)

Page 9: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: FCFS( 最先先行 )ProcessProcess Burst TimeBurst Time (( 区间时间区间时间 , ms), ms)

PP11 2424

PP22 33

PP33 33

Suppose that the processes arrive in the order: Suppose that the processes arrive in the order: PP11 , , PP22 , , PP

3 3 The Gantt chart for the schedule is:The Gantt chart for the schedule is:

Waiting time for Waiting time for PP11 = 0; = 0; PP22 = 24; = 24; PP3 3 = 27= 27 Average waiting time: (0 + 24 + 27)/3 = 17(ms)Average waiting time: (0 + 24 + 27)/3 = 17(ms)

P1 P2 P3

24 27 300

Page 10: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: FCFS

Suppose that the processes arrive in the orderSuppose that the processes arrive in the order

PP22 , , PP33 , , PP11 . .

The Gantt chart for the schedule is:The Gantt chart for the schedule is:

Waiting time for Waiting time for PP1 1 == 6 6;; PP22 = 0 = 0;; PP3 3 = = 3.3.

Average waiting time: (6 + 0 + 3)/3 = 3 (ms)Average waiting time: (6 + 0 + 3)/3 = 3 (ms) Much better than previous case.Much better than previous case.

P1P3P2

63 300

Page 11: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: FCFS

Convoy effectConvoy effect ( 护航效果 ) : short process behind long : short process behind long process.process.

The FCFS scheduling algorithm is nonpreemptive. The FCFS scheduling algorithm is nonpreemptive. The FCFS algorithm is particularly troublesome for timThe FCFS algorithm is particularly troublesome for tim

e-sharing systems. e-sharing systems. It would be disastrous to allow one process to keep tIt would be disastrous to allow one process to keep t

he CPU for an extended period. he CPU for an extended period.

Page 12: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF( 最短先行 )

ProcessProcess BurstTime(ms)BurstTime(ms)

PP11 66

PP22 88

PP33 77

PP44 33

SJFSJF

Waiting time.Waiting time. Average waiting time = (3+16+9+0)/4 = 7(ms)Average waiting time = (3+16+9+0)/4 = 7(ms)

Page 13: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF

Process Arrival time Burst Time Process Arrival time Burst Time

PP11 00 88

PP22 11 44

PP33 22 99

PP44 33 55

SJF (preemptive)SJF (preemptive)

Average waiting time = (9+0+15+2)/4 = 26/4=6.5Average waiting time = (9+0+15+2)/4 = 26/4=6.5

Page 14: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF

Can only estimate the length.Can only estimate the length. Can be done by using the length of previous Can be done by using the length of previous

CPU bursts, using exponential averaging.CPU bursts, using exponential averaging.

:Define 4.

10 , 3.

burst CPU next the for value predicted 2.

burst CPU of lenght actual 1.

1n

thn nt

.t nnn 11

Page 15: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF

=0=0 n+1n+1 = = n. n. Recent history does not count.Recent history does not count.

=1=1 n+1n+1 = = ttn: n: Only the actual last CPU burst counts.Only the actual last CPU burst counts.

If we expand the formula, we get:If we expand the formula, we get:

n+1n+1 = = t tnn+(+(1 - 1 - ) ) t tnn - -1 1 + …+ …

+(1+(1 - - ))j j t tnn - -1 1 + …+ …

+(1+(1 - - ))n=1 n=1 ttnn 00

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

Page 16: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF

Page 17: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: SJF

The SJF scheduling algorithm is provably The SJF scheduling algorithm is provably optimal.optimal.

The SJF scheduling algorithm supports both The SJF scheduling algorithm supports both preemptive and non-preemptive scheduling preemptive and non-preemptive scheduling algorithms. algorithms.

Suitable for long-term scheduling.Suitable for long-term scheduling. Not very good for short-term scheduling. Not very good for short-term scheduling. Difficult to estimate the CPU bursts.Difficult to estimate the CPU bursts.

Page 18: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Priority scheduling( 最优先行 )

ProcessProcess Burst Time Burst Time PriorityPriorityP1P1 1010 33P2P2 11 11P3P3 22 44P4P4 11 55P5P5 55 22

Priority schedulingPriority scheduling

Waiting time: (6+0+16+18+1)/5 =8.2(ms).Waiting time: (6+0+16+18+1)/5 =8.2(ms).

Page 19: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Priority scheduling A priority number (integer) is associated with each prA priority number (integer) is associated with each pr

ocess.ocess. The CPU is allocated to the process with the highest pThe CPU is allocated to the process with the highest p

riority (smallest integer riority (smallest integer highest priority). highest priority). Preemptive.Preemptive. Non-preemptive.Non-preemptive.

SJF is a priority scheduling where priority is the prediSJF is a priority scheduling where priority is the predicted next CPU burst time.cted next CPU burst time.

ProblemProblem :: Starvation (Starvation ( 饥饿饥饿 ) – low priority process) – low priority processes may never execute.es may never execute.

SolutionSolution :: Aging (Aging ( 时效时效 ) – A process will increase i) – A process will increase its priority with time. ts priority with time.

Page 20: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: RR ( 循环而行 ) Each process gets a small unit of CPU time (Each process gets a small unit of CPU time (time quanttime quant

um, um, 时间片段时间片段 ), usually 10-100 milliseconds. After th), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added is time has elapsed, the process is preempted and added to the end of the ready queue.to the end of the ready queue.

If there are If there are nn processes in the ready queue and the time processes in the ready queue and the time quantum is quantum is q, q, then then Each process gets Each process gets 1/n1/n of the CPU time in chunks of a of the CPU time in chunks of a

t most t most qq time units at once. time units at once. No process waits more than No process waits more than (n-1)q (n-1)q time units.time units.

PerformancePerformance qq large large FIFO FIFO q q small small q q must be large with respect to context swimust be large with respect to context swi

tch time, otherwise overhead is too high.tch time, otherwise overhead is too high.

Page 21: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: RR (q=20ms)

ProcessProcess Burst TimeBurst Time

PP11 5353

PP22 1717

PP33 6868

PP44 2424 The Gantt chart is: The Gantt chart is:

Typically, higher average turnaround than SJF, but better Typically, higher average turnaround than SJF, but better responseresponse..

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 22: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: RR (Turnaround time)

Page 23: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: RR(Context Switches)

Page 24: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Multilevel queue

Ready queue is partitioned into separate queues:Ready queue is partitioned into separate queues:foreground (interactive) and background (batch)foreground (interactive) and background (batch)

Each queue has its own scheduling algorithm, Each queue has its own scheduling algorithm, foreground – RR and background – FCFSforeground – RR and background – FCFS

Scheduling must be done between the queues.Scheduling must be done between the queues. Fixed priority scheduling; (i.e., serve all from Fixed priority scheduling; (i.e., serve all from

foreground then from background). Possibility of foreground then from background). Possibility of starvation.starvation.

Time slice – each queue gets a certain amount of Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR, 20% to processes; i.e., 80% to foreground in RR, 20% to background in FCFSbackground in FCFS

Page 25: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Multilevel queue

Page 26: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Multilevel feedback queue A process can move between the various A process can move between the various

queues; aging can be implemented this way.queues; aging can be implemented this way. Multilevel-feedback-queue scheduler defined Multilevel-feedback-queue scheduler defined

by the following parameters:by the following parameters: number of queues.number of queues. scheduling algorithms for each queue.scheduling algorithms for each queue. method used to determine which queue a method used to determine which queue a

process will enter when that process needs process will enter when that process needs service.service.

method used to determine when to method used to determine when to upgrade/downgrade a process.upgrade/downgrade a process.

Page 27: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Multilevel feedback queue Three queues: Three queues:

QQ00 – time quantum 8 milliseconds – time quantum 8 milliseconds QQ11 – time quantum 16 milliseconds – time quantum 16 milliseconds QQ22 – FCFS – FCFS

SchedulingScheduling A new job enters queue A new job enters queue QQ00 which is servedwhich is served FCFS. FCFS.

When it gains CPU, job receives 8 milliseconds. If When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to it does not finish in 8 milliseconds, job is moved to queue queue QQ11..

At At QQ11, job is again served FCFS and receives 16 , job is again served FCFS and receives 16 additional milliseconds. If it still does not additional milliseconds. If it still does not complete, it is preempted and moved to queue complete, it is preempted and moved to queue QQ22..

Page 28: CHAPTER 6:  CPU SCHEDULING  (调度)

Scheduling algorithms: Multilevel feedback queue

Page 29: CHAPTER 6:  CPU SCHEDULING  (调度)

MULTIPLE-PROCESSOR SCHEDULING When multiple CPUs are available, the scheduling probWhen multiple CPUs are available, the scheduling prob

lem is more complex.lem is more complex. For multiple CPUsFor multiple CPUs

Homogeneous vs heterogeneous CPUsHomogeneous vs heterogeneous CPUs Uniform memory access(UMA) vs Non-Uniform meUniform memory access(UMA) vs Non-Uniform me

mory access(NUMA).mory access(NUMA). SchedulingScheduling

Master/Slave vs peer/peerMaster/Slave vs peer/peer Separate queues vs common queue (sharing)Separate queues vs common queue (sharing)

Asymmetric multiprocessingAsymmetric multiprocessing – only one processor acces – only one processor accesses the system data structures, alleviating the need for dses the system data structures, alleviating the need for data sharing.ata sharing.

Symmetric multiprocessing.Symmetric multiprocessing.

Page 30: CHAPTER 6:  CPU SCHEDULING  (调度)

REAL-TIME SCHEDULING Hard real-timeHard real-time systems – required to complete a critical t systems – required to complete a critical t

ask within a guaranteed amount of time.ask within a guaranteed amount of time. Hard real-time systems are composed of special-purpoHard real-time systems are composed of special-purpo

se software running on hardware dedicated to their crise software running on hardware dedicated to their critical process, and lack the full functionality of modern tical process, and lack the full functionality of modern computers and operating systems. computers and operating systems.

Soft real-timeSoft real-time computing – requires that critical processe computing – requires that critical processes receive priority over less fortunate ones.s receive priority over less fortunate ones. Priority scheduling Priority scheduling Low dispatch latencyLow dispatch latency

To insert preemption points.To insert preemption points.To support priority inversion. (See the next slide)To support priority inversion. (See the next slide)To make the entire kernel preemptible.To make the entire kernel preemptible.

Page 31: CHAPTER 6:  CPU SCHEDULING  (调度)

Real-time scheduling: Dispatch latency

Page 32: CHAPTER 6:  CPU SCHEDULING  (调度)

ALGORITHM EVALUATION

Deterministic modelingDeterministic modeling Queueing modelsQueueing models SimulationsSimulations ImplementationImplementation

Page 33: CHAPTER 6:  CPU SCHEDULING  (调度)

Algorithm Evaluation: Deterministic modeling Deterministic modeling Deterministic modeling (确定模型法) (确定模型法) takes a partictakes a partic

ular predetermined workload and defines the performancular predetermined workload and defines the performance of each algorithm for that workload.e of each algorithm for that workload. To describe scheduling algorithms and provide exampTo describe scheduling algorithms and provide examp

les,les, Simple and fast,Simple and fast,

But, Too specific to be useful.But, Too specific to be useful.

Page 34: CHAPTER 6:  CPU SCHEDULING  (调度)

Algorithm Evaluation: Queuing models Queuing models Queuing models (排队模型) (排队模型)

Queueing-network analysisQueueing-network analysisThe computer system is described as a network of sThe computer system is described as a network of s

ervers. Each server has a queue of waiting process. ervers. Each server has a queue of waiting process. The CPU is a server with its ready queue, as is the IThe CPU is a server with its ready queue, as is the I/O system with its device queues. /O system with its device queues.

Knowing arrival rates and service rates, we can coKnowing arrival rates and service rates, we can compute utilization, average queue length, average wmpute utilization, average queue length, average wait time, and so on. ait time, and so on.

Useful for comparing scheduling algorithms.Useful for comparing scheduling algorithms. Real distributions are difficult to work with. Real distributions are difficult to work with. Some assumptions required.Some assumptions required.

Page 35: CHAPTER 6:  CPU SCHEDULING  (调度)

Algorithm Evaluation: Simulation

Simulations (Simulations ( 模拟模拟 ) involve programming a model of ) involve programming a model of the computer system. the computer system. Software data structures represent the major compSoftware data structures represent the major comp

onents of the system. onents of the system. The simulator has a variable representing a clock; The simulator has a variable representing a clock;

as this variable ‘s value is increased, the simulator as this variable ‘s value is increased, the simulator modifies the system to reflect the activities of the dmodifies the system to reflect the activities of the device, the processes, and the scheduler. evice, the processes, and the scheduler.

As the simulation executes, statistics that indicate aAs the simulation executes, statistics that indicate algorithm performance are gathered and printed. lgorithm performance are gathered and printed.

Artificial data or trace tapes.Artificial data or trace tapes. Useful but expensive.Useful but expensive.

Page 36: CHAPTER 6:  CPU SCHEDULING  (调度)

Algorithm Evaluation: Implementation

Implementation (Implementation ( 实现实现 ) ) To code the algorithm, To code the algorithm, To put it in the OS, and To put it in the OS, and To see how it works. To see how it works.

Costly.Costly.

A Perfect Scheduling Algorithm Is Not Easy To FA Perfect Scheduling Algorithm Is Not Easy To Found.ound.

In Practice, We Don’t Really Need The Perfect ScIn Practice, We Don’t Really Need The Perfect Scheduling Algorithm.heduling Algorithm.

Page 37: CHAPTER 6:  CPU SCHEDULING  (调度)

SOLARIS 2 SCHEDULING

Page 38: CHAPTER 6:  CPU SCHEDULING  (调度)

WINDOWS 2000 PRIORITIES

Page 39: CHAPTER 6:  CPU SCHEDULING  (调度)

LINUX SCHEDULING

realtime processes (1000+), nonrealtime procerealtime processes (1000+), nonrealtime processes(1000-)sses(1000-)

Credits = Credits/2 + priorityCredits = Credits/2 + priority High priority High priority interactive processes interactive processes

Page 40: CHAPTER 6:  CPU SCHEDULING  (调度)

Homework

6.3***6.3*** 6.4***6.4*** 6.86.8 6.106.10

Page 41: CHAPTER 6:  CPU SCHEDULING  (调度)