chapter 5: cpu schedulingcontents.kocw.net/kocw/document/2014/yeungnam/... · 2016-09-09 · cpu...

22
Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling

Upload: others

Post on 18-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Chapter 5: CPU SchedulingChapter 5: CPU Scheduling

Basic ConceptsBasic Concepts

Maximum CPU utilization is obtained by multiprogramming

CPU–I/O Burst Cycle Process execution consists of a cycle of CPU execution and yI/O wait

Burst accessBurst accessCPU burst I/O burst When I/O burst stage, the CPU is idle

5.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

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

5.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Histogram of CPUHistogram of CPU--burst Timesburst TimesThe large number of short CPU bursts. I/O bound program

The small number of long CPU bursts CPU bound programThe small number of long CPU bursts. CPU bound program

5.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

CPU SchedulerCPU Scheduler

Selects from among the processes in memory that are ready to execute and allocates the CPU to one of themexecute, and allocates the CPU to one of themCPU scheduling decisions may take place when a process:1. Switches from running to waiting stateg g2. Switches from running to ready state3. Switches from waiting to ready4. Terminates

Scheduling under 1 and 4 is nonpreemptiveAll th h d li ld b tiAll other scheduling could be preemptive

5.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

DispatcherDispatcher

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

switching process contextMode change into user modeMode change into user modejumping to the proper location in the user program to restart that program

Dispatch latency – time to take for the dispatcher to stop one process and start another runningprocess and start another running

5.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Scheduling CriteriaScheduling Criteria

CPU utilization – keep the CPU as busy as possible

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

Turnaround time amount of time to execute a particular processInterval from process submission to completionThe policy which has the view of specific processThe policy which has the view of specific process

5.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Scheduling CriteriaScheduling Criteria

Waiting time amount of time a process has been waiting in the ready queueOptimal criteria in case of shortest job first policy

Response time Interactive system, turnaround time is not best solutiony

Turnaround time is limited by the speed of the output device amount of time it takes from when a request was submitted until the first response is producedthe first response is produced

i.e., The time to start responding, not the time to completed output of response

5.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Scheduling Criteria ConflictionScheduling Criteria Confliction

The confliction example

In interactive system, response time is important criteria

To enhance response time many context switching throughput is reduced.

5.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Optimization CriteriaOptimization Criteria

Max CPU utilizationMax throughputMin turnaround time Min waiting time Min response time

5.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

FirstFirst--Come, FirstCome, First--Served (FCFS) SchedulingServed (FCFS) Scheduling

FIFO schedulinggSimplest schemeProcesses dispatched according to arrival timeNon-preemption policyRarely used as primary scheduling algorithm

5.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

FirstFirst--Come, FirstCome, First--Served (FCFS) SchedulingServed (FCFS) Scheduling

Process Burst TimeP1 24P2 3P 3P3 3

Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:

P1 P2 P3

Waiting time for P1 = 0; P2 = 24; P3 = 27

24 27 300

Average waiting time: (0 + 24 + 27)/3 = 17

5.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

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

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

The Gantt chart for the schedule is:

P1P3P2

Waiting time for P1 = 6; P2 = 0; P3 = 3A iti ti (6 + 0 + 3)/3 3

63 300

Average waiting time: (6 + 0 + 3)/3 = 3Much better than previous caseConvoy effectConvoy effect

short process behind long process

5.13 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

ShortestShortest--JobJob--First (SJF) SchedulingFirst (SJF) Scheduling

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

Two variations:Two variations: nonpreemptive – once CPU is given to the process, it cannot be preempted until completes its CPU burstpreemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, then preempt it.

This scheme is known as the Shortest-Remaining-Time-FirstThis scheme is known as the Shortest Remaining Time First (SRTF)

5.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Example of NonExample of Non--Preemptive SJFPreemptive SJF

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

SJF (non-preemptive)( p p )

P1 P3 P2 P4

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

73 160 8 12

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

5.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Example of Preemptive SJF(SRTF)Example of Preemptive SJF(SRTF)

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

SJF (preemptive)(p p )

P1 P3P2 P4P2 P1

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

42 110 5 7 16

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

5.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

ShortestShortest--JobJob--First (SJR) SchedulingFirst (SJR) Scheduling

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

Real difficultyReal difficultyHow do we know next burst of CPU?Exponential Averagep g

Next CPU burst is predicted as an exponential average of the measured lengths of previous CPU bursts.

5.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Determining Length of Next CPU Burst Determining Length of Next CPU Burst

Can only estimate the lengthCan be done by using the length of previous CPU bursts, using exponential averaging

burst CPU oflength actual 1. = thn nt

10 , 3.burst CPUnext for the valuepredicted 2. 1

≤≤=+

αατ n

:Define 4. ( ) .1 1 nnn t ταατ −+=+

5.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Prediction of the Length of Next CPU BurstPrediction of the Length of Next CPU Burst

5.19 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Examples of Exponential Averaging Examples of Exponential Averaging

If α is ½ then recent history and past( )1t ταατ −+= If α is ½, then recent history and past history are equally weighted.

So α controls relative weight of recent d t hi t

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

and past history

5.20 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Priority SchedulingPriority Scheduling

A priority number (integer) is associated with each process

The CPU is allocated to the process with the highest priorityPreemptivePreemptivenonpreemptive

SJF can be treated as a priority scheduling where priority is the predicted next CPU burst time

Problem ≡ Starvation low priority processes may never executelow priority processes may never execute

5.21 Silberschatz, Galvin and Gagne ©2009Operating System Concepts

Priority SchedulingPriority Scheduling

Solution ≡ Aging As time progresses, system increases the priority of the process

HRRN (Highest Response Ratio Next) schedulingResponse ratio = (waiting time + required CPU time) /

i d CPU tirequired CPU time

The problem of non-preemptive priority schedulingThe problem of non preemptive priority schedulingThe possibility of Priority Inversion

5.22 Silberschatz, Galvin and Gagne ©2009Operating System Concepts