chapter 6: cpu scheduling - kent state universityfarrell/osf03/lectures/ch6-1.pdfchapter 6: cpu...

34
1 Silberschatz, Galvin and Gagne 2002 6.1 Operating System Concepts Chapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria n Scheduling Algorithms n Multiple-Processor Scheduling n Real-Time Scheduling n Algorithm Evaluation

Upload: vankhanh

Post on 21-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

1

Silberschatz, Galvin and Gagne 20026.1Operating System Concepts

Chapter 6: CPU Scheduling

n Basic Conceptsn Scheduling Criteria n Scheduling Algorithmsn Multiple-Processor Schedulingn Real-Time Schedulingn Algorithm Evaluation

Page 2: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

2

Silberschatz, Galvin and Gagne 20026.2Operating System Concepts

Basic Concepts

n Maximum CPU utilization obtained with multiprogrammingn CPU–I/O Burst Cycle – Process execution consists of a

cycle of CPU execution and I/O wait.n CPU burst distribution

Page 3: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

3

Silberschatz, Galvin and Gagne 20026.3Operating System Concepts

Alternating Sequence of CPU And I/O Bursts

Page 4: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

4

Silberschatz, Galvin and Gagne 20026.4Operating System Concepts

Histogram of CPU-burst Times

Page 5: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

5

Silberschatz, Galvin and Gagne 20026.5Operating System Concepts

CPU (Short-term) Scheduler

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

n 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.

n Scheduling under 1 and 4 is nonpreemptiveF Process retains CPU until it releases it F Windows 3.1, MAC OS

n All other scheduling is preemptive.

Page 6: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

6

Silberschatz, Galvin and Gagne 20026.6Operating System Concepts

Issues with Preemptive Scheduling

n New mechanisms needed to ensure shared data is not in an inconsistent state (partially updated)

n System calls may change important kernel parametersF What happens if process preempted

n Unix (most versions) wait for system call to complete or i/o block to take place

n Also interrupts must be guarded from simultaneous useF Interrupts disabled at entry, reenabled at exit

n These are bad features for real time or multiprocessor systems

Page 7: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

7

Silberschatz, Galvin and Gagne 20026.7Operating System Concepts

Dispatcher

n Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:F switching contextF switching to user modeF jumping to the proper location in the user program to restart

that program

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

Page 8: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

8

Silberschatz, Galvin and Gagne 20026.8Operating System Concepts

Scheduling Criteria

n CPU utilization – keep the CPU as busy as possiblen Throughput – # of processes that complete their

execution per time unitn Turnaround time – amount of time to execute a particular

processn Waiting time – amount of time a process has been waiting

in the ready queuen 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)

Page 9: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

9

Silberschatz, Galvin and Gagne 20026.9Operating System Concepts

Optimization Criteria

n Max CPU utilizationn Max throughputn Min turnaround time n Min waiting time n Min response timen In theory minimize variance in response time

Page 10: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

10

Silberschatz, Galvin and Gagne 20026.10Operating System Concepts

First-Come, First-Served (FCFS) Scheduling

Process Burst TimeP1 24P2 3P3 3

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

The Gantt Chart for the schedule is:

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

P1 P2 P3

24 27 300

Page 11: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

11

Silberschatz, Galvin and Gagne 20026.11Operating System Concepts

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the orderP2 , P3 , P1 .

n The Gantt chart for the schedule is:

n Waiting time for P1 = 6; P2 = 0; P3 = 3n Average waiting time: (6 + 0 + 3)/3 = 3n Much better than previous case.n Convoy effect short process behind long process

P1P3P2

63 300

Page 12: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

12

Silberschatz, Galvin and Gagne 20026.12Operating System Concepts

Shortest-Job-First (SJR) Scheduling

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

n Two schemes: F nonpreemptive – once CPU given to the process it cannot

be preempted until completes its CPU burst.F 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).

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

Page 13: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

13

Silberschatz, Galvin and Gagne 20026.13Operating System Concepts

Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4

n SJF (non-preemptive)

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

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Page 14: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

14

Silberschatz, Galvin and Gagne 20026.14Operating System Concepts

Example of Preemptive SJF

Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4

n SJF (preemptive)

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 15: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

15

Silberschatz, Galvin and Gagne 20026.15Operating System Concepts

Determining Length of Next CPU Burst

n Can only estimate the length.n Can be done by using the length of previous 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

( ) .1 1 nnn t ταατ −+=+

Page 16: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

16

Silberschatz, Galvin and Gagne 20026.16Operating System Concepts

Prediction of the Length of the Next CPU Burst

Page 17: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

17

Silberschatz, Galvin and Gagne 20026.17Operating System Concepts

Examples of Exponential Averaging

n α =0F τn+1 = τn

F Recent history does not count.

n α =1F τn+1 = tnF Only the actual last CPU burst counts.

n If we expand the formula, we get:τn+1 = α tn+(1 - α) α tn -1 + …

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

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

Page 18: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

18

Silberschatz, Galvin and Gagne 20026.18Operating System Concepts

Priority Scheduling

n A priority number (integer) is associated with each process

n The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority!! maybe).F PreemptiveF nonpreemptive

n SJF is a priority scheduling where priority is the inverse of the predicted next CPU burst time.

n Problem ≡ Starvation (indefinite postponement) – low priority processes may never execute.

n Solution ≡ Aging – as time progresses increase the priority of the process.

Page 19: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

19

Silberschatz, Galvin and Gagne 20026.19Operating System Concepts

Round Robin (RR)

n 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.

n 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.

n PerformanceF q large ⇒ FIFOF q small ⇒ q must be large with respect to context switch,

otherwise overhead is too high.

Page 20: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

20

Silberschatz, Galvin and Gagne 20026.20Operating System Concepts

Example of RR with Time Quantum = 20

Process Burst Time

P1 53P2 17P3 68P4 24

n The Gantt chart is:

n 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 21: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

21

Silberschatz, Galvin and Gagne 20026.21Operating System Concepts

Time Quantum and Context Switch Time

Page 22: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

22

Silberschatz, Galvin and Gagne 20026.22Operating System Concepts

Turnaround Time Varies With The Time Quantum

Page 23: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

23

Silberschatz, Galvin and Gagne 20026.23Operating System Concepts

Multilevel Queue

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

n Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS

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

then from background). Possibility of starvation.F 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

F 20% to background in FCFS

Page 24: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

24

Silberschatz, Galvin and Gagne 20026.24Operating System Concepts

Multilevel Queue Scheduling

Page 25: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

25

Silberschatz, Galvin and Gagne 20026.25Operating System Concepts

Multilevel Feedback Queue

n A process can move between the various queues; aging can be implemented this way.

n Multilevel-feedback-queue scheduler defined by the following parameters:F number of queuesF scheduling algorithms for each queueF method used to determine when to upgrade a processF method used to determine when to demote a processF method used to determine which queue a process will enter

when that process needs service

Page 26: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

26

Silberschatz, Galvin and Gagne 20026.26Operating System Concepts

Example of Multilevel Feedback Queue

n Three queues: F Q0 – time quantum 8 millisecondsF Q1 – time quantum 16 millisecondsF Q2 – FCFS

n SchedulingF 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.

F 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 27: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

27

Silberschatz, Galvin and Gagne 20026.27Operating System Concepts

Multilevel Feedback Queues

Page 28: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

28

Silberschatz, Galvin and Gagne 20026.28Operating System Concepts

Multiple-Processor Scheduling

n CPU scheduling more complex when multiple CPUs are available.

n Assume:F Homogeneous processors within a multiprocessor.F Uniform memory access (UMA)

n Load sharing - use common ready queueF Symmetric – each processor examines ready queue

n Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing protection.

Page 29: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

29

Silberschatz, Galvin and Gagne 20026.29Operating System Concepts

Real-Time Scheduling

n Hard real-time systems – required to complete a critical task within a guaranteed amount of time.F Need special purpose software on dedicated hardwareF No secondary storage or virtual memory

n Soft real-time computing – requires that critical processes receive priority over less fortunate ones.F Need priority schedulingF Need small dispatch latency –difficult F Unix: context switch only when systems calls complete or

I/O blocksF Can insert preemption points in system calls F Or make kernel preemptibleF Read more on this.

Page 30: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

30

Silberschatz, Galvin and Gagne 20026.30Operating System Concepts

Dispatch Latency

Conflict phase: preempt kernel processes/ release low priority process resources needed by higher priority processes

Page 31: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

31

Silberschatz, Galvin and Gagne 20026.31Operating System Concepts

Algorithm Evaluation

n Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload.

n Queueing models – obtain probability distribution from measured CPU and I/O bursts. Treat computer as network of queues of waiting processes with known arrival and service rates

n Simulations – represent components by software data structures. F Use random number generator to generate data.F Use trace tapes

n Implementation

Page 32: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

32

Silberschatz, Galvin and Gagne 20026.32Operating System Concepts

Evaluation of CPU Schedulers by Simulation

Page 33: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

33

Silberschatz, Galvin and Gagne 20026.33Operating System Concepts

Solaris 2 Scheduling

Page 34: Chapter 6: CPU Scheduling - Kent State Universityfarrell/osf03/lectures/ch6-1.pdfChapter 6: CPU Scheduling n Basic Concepts n Scheduling Criteria ... Multilevel Feedback Queue n A

34

Silberschatz, Galvin and Gagne 20026.34Operating System Concepts

Windows 2000 Priorities