chapter 5: cpu scheduling. 5.2/42 chapter 5: cpu scheduling basic concepts scheduling criteria...

42
Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling

Upload: charity-allen

Post on 11-Jan-2016

282 views

Category:

Documents


32 download

TRANSCRIPT

Page 1: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

Chapter 5: CPU SchedulingChapter 5: CPU Scheduling

Page 2: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.2/42

Chapter 5: CPU SchedulingChapter 5: CPU Scheduling

Basic Concepts

Scheduling Criteria

Scheduling Algorithms

Operating Systems Examples

Page 3: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.3/42

Basic ConceptsBasic Concepts

Process execution consists of a cycle of CPU execution and I/O wait.

Processes alternate between these two states.

Process execution begins with a CPU burst. That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. Eventually, the final CPU burst ends with a system request to terminate execution

CPU burst 概念:一个进程在 CPU 上的一次连续执行过程称为该进程的一个 CPU 周期。一个 CPU 周期由进程自我终止。当进程需等待某个事件而进入等待状态时,便终止了它的当前 CPU 周期。

P153

Page 4: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.4/42

Alternating Sequence of CPU And I/O BurstsAlternating Sequence of CPU And I/O Bursts

Page 5: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.5/42

CPU BurstCPU Burst

Page 6: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.6/42

CPU BurstCPU Burst with a large number of short CPU bursts and a

small number of long CPU bursts.

An I/O-bound program typically has many short CPU bursts.

A CPU-bound program might have a few long CPU bursts.

This distribution can be important in the selection of an appropriate CPU-scheduling algorithm.

P154

Page 7: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.7/42

CPU SchedulerCPU Scheduler

Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them

CPU scheduling decisions may take place when a process:

1.Switches from running to waiting state

2.Switches from running to ready state

3.Switches from waiting to ready

4.Terminates

Scheduling under 1 and 4 is non-preemptive

All other scheduling is preemptive

P156

Page 8: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.8/42

调度方式调度方式 非占先式调度( non preemptive )

一个进程一旦获得 CPU 便一直执行下去,直到完成它的当前 CPU Burst ,系统才被重新调度,换言之, OS 无权分割进程的任一 CPU Burst 。

占先式调度( preemptive ) 当现行进程正在执行它的一个 CPU 周期期间, OS 有

权强行分割该进程的当前 CPU Burst ,即强行剥夺现行进程正占用的 CPU ,并把 CPU 分配给另一进程,换言之,一个进程的一个 CPU Burst 可能被分割成两个或更多个 CPU Burst 。

Which is better ?P156

Page 9: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.9/42

调度方式调度方式 非占先式调度( non preemptive )

如果一个进程陷入死循环,怎么办? 占先式调度( preemptive )

避免了上述问题 Unfortunately, preemptive scheduling incurs a cost

associated with access to shared data.----inconsistent state.

Preemption also affects the design of the operating-system kernel.

ensures that the kernel structure is simple, since the kernel will not preempt a process while the kernel data structures are in an inconsistent state.

Page 10: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.10/42

DispatcherDispatcher

Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:

switching context

switching to user mode

jumping to the proper location in the user program to restart that program

Dispatch latency – time it takes for the dispatcher to stop one process and start another running

The dispatcher should be as fast as possible, since it is invoked during every process switch.

P157

Page 11: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.11/42

Scheduling CriteriaScheduling Criteria

CPU utilization – keep the CPU as busy as possible

Throughput – # of processes that complete their execution per time unit

Turnaround time

The interval from the time of submission of a process to the time of completion.

Turnaround time is the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.

Waiting time – amount of time a process has been waiting in the ready queue

调度算法不影响进程的总的执行时间和 I/O操作时间。只影响进程在就绪队列等待被调度的时间。P157

Page 12: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.12/42

Scheduling CriteriaScheduling Criteria

Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)

----------------------------------

Max CPU utilization

Max throughput

Min turnaround time

Min waiting time

Min response time

Page 13: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.13/42

Optimization CriteriaOptimization Criteria

each being a sequence of several hundred CPU bursts and I/O bursts.

For simplicity, though, we consider only one CPU burst per process in our examples.

Our measure of comparison is the average waiting time.

Page 14: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.14/42

First-Come, First-Served (FCFS) SchedulingFirst-Come, First-Served (FCFS) Scheduling

Process Burst Time

P1 24

P2 3

P3 3

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

The Gantt Chart for the schedule is:

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

P158

Page 15: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.15/42

FCFS Scheduling (Cont.)FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order

P2 , P3 , P1

The Gantt chart for the schedule is:

Waiting time for P1 = 6; P2 = 0; P3 = 3

Average waiting time: (6 + 0 + 3)/3 = 3

Much better than previous case

Convoy effect :all the other processes wait for the one big process to get off the CPU.

P1P3P2

63 300

Page 16: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.16/42

FCFS Scheduling FCFS Scheduling 特点特点

实现简单,自然; 属于非占先调度; 平均等待时间一般比较长; 有利于长( CPU burst )进程,不利于短( CPU

burst )进程。

Page 17: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.17/42

Shortest-Job-First (SJF) SchedulingShortest-Job-First (SJF) Scheduling

Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time

Two schemes:

nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst

preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)

P160

Page 18: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.18/42

Shortest-Job-First (SJF) SchedulingShortest-Job-First (SJF) Scheduling

SJF is optimal – gives minimum average waiting time for a given set of processes

The real difficulty with the SJF algorithm is knowing the length of the next CPU request.

Page 19: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.19/42

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

SJF (non-preemptive)

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

Example of Non-Preemptive SJFExample of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Page 20: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.20/42

Example of Preemptive SJFExample of Preemptive SJF

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

SJF (preemptive)

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 21: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.21/42

SJF SchedulingSJF Scheduling 特点特点 有效地降低作业的平均等待时间,提高了系统的吞吐量; 对长作业十分不利。饿死现象。 未完全考虑作业的紧迫程度; 估算作业(进程)的执行时间非常困难。

Page 22: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.22/42

Priority SchedulingPriority Scheduling

A priority number (integer) is associated with each process, the CPU is allocated to the process with the highest priority, Equal-priority processes are scheduled in FCFS order.

P162

Page 23: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.23/42

Priority SchedulingPriority Scheduling

Page 24: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.24/42

Priority SchedulingPriority Scheduling Equal-priority processes are scheduled in FCFS order.

An SJF algorithm is simply a priority algorithm where the priority is the inverse of the (predicted) next CPU burst.

Priority scheduling can be either preemptive or non-preemptive.

A major problem with priority scheduling algorithms is indefinite blocking, or starvation.

(Rumor has it that, when they shut down the IBM 7094 at MIT in 1973, they found a low-priority process that had been submitted in 1967 and had not yet been run.)

Aging: gradually increasing the priority of processes that wait in the system for a long time.

P163

Page 25: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.25/42

Priority SchedulingPriority Scheduling

Priorities can be defined either internally or externally.

Internally defined priorities use some measurable quantity or quantities to compute the priority of a process. For example, time limits, memory requirements, the number of open files, and the ratio of average I/O burst to average CPU burst have been used in computing priorities.

External priorities are set by criteria outside the operating system, such as the importance of the process, the type and amount of funds being paid for computer use, the department sponsoring the work, and other, often political, factors.

Page 26: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.26/42

Round Robin (RR)Round Robin (RR)

Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

The process may have a CPU burst of less than 1 time quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue

If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.

P164

Page 27: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.27/42

Example of RR with Time Quantum = 20Example of RR with Time Quantum = 20

Process Burst Time

P1 53

P2 17

P3 68

P4 24 The Gantt chart is:

Typically, higher average turnaround than SJF, but better response

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 28: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.28/42

Round Robin (RR)Round Robin (RR)

The performance of the RR algorithm depends heavily on the size of the time quantum.

q large FIFO

q small q must be large with respect to context switch, otherwise overhead is too high

Although the time quantum should be large compared with the context switch time, it should not be too large.

Page 29: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.29/42

Time Quantum and Context Switch TimeTime Quantum and Context Switch Time

Page 30: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.30/42

Multilevel QueueMultilevel Queue

Ready queue is partitioned into separate queues

Each queue has its own scheduling algorithm

Scheduling must be done between the queues

Fixed priority scheduling; (i.e., serve all from foreground then from background). -Possibility of starvation.

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

P166-167

Page 31: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.31/42

Multilevel Queue SchedulingMultilevel Queue Scheduling

Page 32: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.32/42

Multilevel Feedback QueueMultilevel Feedback Queue

设置多个就绪队列,并为各个队列赋予不同的优先权。第一个队列的优先权最高,第二队列次之,其余队列的优先权逐个降低。

赋予各个队列中进程执行时间片的大小也各不相同,优先权越高,时间片越小。

当一个新进程进入内存后,首先将它放入第一队列的末尾,按 FCFS 原则排队等待调度。当轮到该进程执行时,如能在该时间片内完成,便可准备撤离系统;如果它在一个时间片结束时尚未完成,调度程序便将该进程转入第二队列的末尾,再同样地按 FCFS 原则等待调度执行;如果它在第二队列中运行一个时间片后仍未完成,再依法将它转入第三队列。如此下去,当一个长进程从第一队列降到第 n 队列后,在第n 队列中便采取按时间片轮转的方式运行。

P168

Page 33: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.33/42

Multilevel Feedback QueueMultilevel Feedback Queue

仅当第一队列空闲时,调度程序才调度第二队列中的进程运行;仅当第 1~ ( i-1 )队列均空时,才会调度第 i 队列中的进程运行。

如果处理机正在第 i 队列中为某进程服务时,又有新进程进入优先权较高的队列,则此时新进程将抢占正在运行进程的处理机,由调度程序把正在运行进程放回第 i 队列末尾,重新把处理机分配给新进程。

Page 34: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.34/42

Example of Multilevel Feedback QueueExample of Multilevel Feedback Queue

Three queues:

Q0 – RR with time quantum 8 milliseconds

Q1 – RR time quantum 16 milliseconds

Q2 – FCFS

Scheduling

A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.

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

Page 35: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.35/42

Multilevel Feedback QueuesMultilevel Feedback Queues

Page 36: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.36/42

Multilevel Feedback QueueMultilevel Feedback Queue

Multilevel-feedback-queue scheduler defined by the following parameters:

number of queues

scheduling algorithms for each queue

method used to determine when to upgrade a process

method used to determine when to demote a process

method used to determine which queue a process will enter when that process needs service

Page 37: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.37/42

Windows XP SchedulingWindows XP Scheduling

Windows XP schedules threads using a priority-based, preemptive scheduling algorithm.

The Windows XP scheduler ensures that the highest-priority thread will always run.

The portion of the Windows XP kernel that handles scheduling is called the dispatcher.

A thread selected to run by the dispatcher will run until it is preempted by a higher-priority thread, until it terminates, until its time quantum ends, or until it calls a blocking system call, such as for I/O.

If a higher-priority real-time thread becomes ready while a lower-priority thread is running, the lower-priority thread will be preempted.

P177-179

Page 38: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.38/42

Windows XP SchedulingWindows XP Scheduling The dispatcher uses a 32-level priority scheme to determine

the order of thread execution. Priorities are divided into two classes.

The variable class contains threads having priorities from 1 to 15,

the real-time class contains threads with priorities ranging from 16 to 31.

(There is also a thread running at priority 0 that is used for memory management.)

The dispatcher uses a queue for each scheduling priority and traverses the set of queues from highest to lowest until it finds a thread that is ready to run.

If no ready thread is found, the dispatcher will execute a special thread called the idle thread.

Page 39: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.39/42

Windows XP SchedulingWindows XP Scheduling The Win32 API identifies several priority classes to which a

process can belong. These include 6 classes (see p177).

Priorities in all classes except the REALTIME-PRIORITY-CLASS are variable, meaning that the priority of a thread belonging to one of these classes can change.

Within each of the priority classes is a relative priority. The values for relative priority include:

TIME-CRITICAL 、 HIGHEST 、 ABOVE-NORMAL 、 NORMAL 、 BELOW-NORMAL 、 LOWEST、 IDLE

Page 40: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.40/42

Windows XP PrioritiesWindows XP Priorities

Page 41: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

5.41/42

Windows XP SchedulingWindows XP Scheduling When a thread's time quantum runs out, that thread is

interrupted; if the thread is in the variable-priority class, its priority is lowered.

The priority is never lowered below the base priority, however. Lowering the thread's priority tends to limit the CPU consumption of compute-bound threads.

When a variable-priority thread is released from a wait operation, the dispatcher boosts the priority. The amount of the boost depends on what the thread was waiting for; for example, a thread that was waiting for keyboard I/O would get a large increase, whereas a thread waiting for a disk operation would get a moderate one. This strategy tends to give good response times to interactive threads that are using the mouse and windows.

Page 42: Chapter 5: CPU Scheduling. 5.2/42 Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Operating Systems Examples

HomeworkHomework ::11、、 22、、 44、、 55、、 66、、 77、、 99、、 1010

End of Chapter 5End of Chapter 5