OS_Ch05

34
CPU Scheduling CPU Scheduling

Transcript of OS_Ch05

Page 1: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 1/34

CPU SchedulingCPU Scheduling

Page 2: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 2/34

5.2Operating System Conce pts

CPU SchedulingCPU Scheduling

Basic ConceptsScheduling Criteria

Scheduling Algorithms

Multiple-Processor Scheduling

Real-Time Scheduling

Thread Scheduling

Operating Systems Examples

Java Thread Scheduling

Algorithm Evaluation

Page 3: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 3/34

5.3Operating System Conce pts

Basic Conce ptsBasic Conce pts

Maximum CPU utilization obtained with multiprogrammingCPU±I/O Burst Cycle ± Process execution consists of acycle of CPU executionand I/O wait

CPU burst distribution

Page 4: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 4/34

5.4Operating System Conce pts

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

Page 5: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 5/34

5.5Operating System Conce pts

CPU Scheduler CPU Scheduler

Selects from among the processes in memory that are ready to execute, andallocates 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 isnonpr eem ptiv e

All other scheduling is pr eem ptiv e

Page 6: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 6/34

5.6Operating System Conce pts

Dispatcher Dispatcher

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 programDispat ch l at ency ± time it takes for the dispatcher to stop one process and startanother running

Page 7: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 7/34

5.7Operating System Conce pts

Scheduling CriteriaScheduling Criteria

CPU utilization ± keep the CPU as busy as possibleThroughput ± # of processes that complete their execution per time unit

Turnaround time ± amount of time to execute a particular process

Waiting time ± amount of time a process has been waiting in the readyqueue

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

Page 8: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 8/34

5.8Operating System Conce pts

Optimization CriteriaOptimization Criteria

Max CPU utilization

Max throughput

Min turnaround time

Min waiting time

Min response time

Page 9: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 9/34

5.9Operating System Conce pts

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

Process Burst TimeP 1 24P 2 3P 3 3

Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is:

Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3= 17

P1 P2 P3

24 27 300

Page 10: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 10/34

5.10Operating System Conce pts

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

Suppose that the processes arrive in the order P 2 , P 3 , P 1

The Gantt chart for the schedule is:

Waiting time for P 1 = 6 ; P 2 = 0; P 3 = 3

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

Much better than previous caseC onvoy effec t short process behind long process

P1P3P2

63 300

Page 11: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 11/34

5.11Operating System Conce pts

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

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

Two schemes:

nonpreemptive ± once CPU given to the process it cannot be preempted untilcompletes its CPU burst

preemptive ± if a new process arrives with CPU burst length less thanremaining time of current executing process, preempt. This scheme is knowas theShortest-Remaining-Time-First (SRTF)

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

Page 12: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 12/34

5.12Operating System Conce pts

Process Arrival Time Burst TimeP 1 0.0 7P 2 2.0 4P 3 4.0 1P 4 5.0 4

SJF (non-preemptive)

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

Ex ample of NonEx ample of Non--Preemptive SJFPreemptive SJF

P1 P3 P2

73 160

P4

8 12

Page 13: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 13/34

5.13Operating System Conce pts

Ex ample of Preemptive SJFEx ample of Preemptive SJF

Process Arrival Time Burst TimeP 1 0.0 7P 2 2.0 4P 3 4.0 1P 4 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 14: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 14/34

5.14Operating System Conce pts

Determining Length of Nex t CPU BurstDetermining Length of Nex t CPU Burst

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

:Define 4.10, 3.

burst CPUnextthefor valuepredicted 2.

burst CPUof lenghtactual 1.

1

ee

!

!

EE

X n

th

nnt

.11 nnn

t XEEX !!

Page 15: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 15/34

5.15Operating System Conce pts

Prediction of the Length of the Ne x t CPU BurstPrediction of the Length of the Ne x t CPU Burst

Page 16: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 16/34

5.16Operating System Conce pts

Ex amples of Ex ponential AveragingEx amples of Ex ponential Averaging

E =0Xn+1 = Xn

Recent history does not countE =1

Xn+1 = E t nOnly the actual last CPU burst counts

If we expand the formula, we get:Xn+1 = E tn+(1 - E )E t n -1 + «

+( 1 - E ) j E t n - j + «+( 1 - E )n +1 X0

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

Page 17: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 17/34

5.17Operating System Conce pts

Priority SchedulingPriority Scheduling

A priority number (integer) is associated with each processThe CPU is allocated to the process with the highest priority (smallest integer |

highest priority)

Preemptive

nonpreemptive

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

Problem | Starvation ± low priority processes may never execute

Solution | Aging ± as time progresses increase the priority of the process

Page 18: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 18/34

5.18Operating System Conce pts

Round Robin (RR)Round Robin (RR)

Each process gets a small unit of CPU time (ti me quant um), usually 10-100milliseconds. After this time has elapsed, the process is preempted andadded to the end of the ready queue.

If there aren processes in the ready queue and the time quantum is q, theneach process gets 1/ n of the CPU time in chunks of at mostq time units at

once. No process waits more than (n-1)q time units.Performance

q large FIFO

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

Page 19: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 19/34

5.19Operating System Conce pts

Ex ample of RR with Time Quantum = 20Ex ample of RR with Time Quantum = 20

Process Burst TimeP 1 53P 2 17P 3 68P

4 24The Gantt chart is:

Typically, higher average turnaround than SJF, but better r esponse

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 20: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 20/34

5.20Operating System Conce pts

Time Quantum and Conte x t Switch TimeTime Quantum and Conte x t Switch Time

Page 21: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 21/34

5.21Operating System Conce pts

Turnaround Time Varies With The Time QuantumTurnaround Time Varies With The Time Quantum

Page 22: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 22/34

5.22Operating System Conce pts

Multilevel QueueMultilevel Queue

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

Each queue has its own scheduling algorithm

foreground ± RR

background ± FCFSScheduling must be done between the queues

Fixed priority scheduling; (i.e., serve all from foreground then frombackground). 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 RR20% to background in FCFS

Page 23: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 23/34

5.23Operating System Conce pts

Multilevel Queue SchedulingMultilevel Queue Scheduling

Page 24: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 24/34

5.24Operating System Conce pts

Multilevel Feedback QueueMultilevel Feedback Queue

A process can move between the various queues; aging can be implemented thisway

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

number of queues

scheduling algorithms for each queuemethod 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 thatprocess needs service

Page 25: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 25/34

5.25Operating System Conce pts

Ex ample of Multilevel Feedback QueueEx ample of Multilevel Feedback Queue

Three queues:Q0 ± RR with time quantum 8 millisecondsQ1 ± RR time quantum 16 millisecondsQ2 ± FCFS

Scheduling

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

AtQ1 job is again served FCFS and receives 16 additional milliseconds. If itstill does not complete, it is preempted and moved to queueQ2.

Page 26: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 26/34

5.26Operating System Conce pts

Multilevel Feedback QueuesMultilevel Feedback Queues

Page 27: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 27/34

5.27Operating System Conce pts

MultipleMultiple--Processor SchedulingProcessor Scheduling

CPU scheduling more complex when multiple CPUs are availableH omog eneous processors within a multiprocessor Load sharing

Asymmetri c mul tiprocessing ± only one processor accesses thesystem data structures, alleviating the need for data sharing

Page 28: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 28/34

5.28Operating System Conce pts

RealReal--Time SchedulingTime Scheduling

H ard r eal -ti me systems ± required to complete a critical taskwithin a guaranteed amount of timeS of t r eal -ti me computing ± requires that critical processesreceive priority over less fortunate ones

Page 29: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 29/34

5.29Operating System Conce pts

Thread SchedulingThread Scheduling

Local Scheduling ± How the threads library decides which thread to putonto an availableLWP

Global Scheduling ± How the kernel decides which kernel thread to run next

Page 30: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 30/34

5.30Operating System Conce pts

Operating System Ex amplesOperating System Ex amples

Solaris scheduling

Windows XP schedulingLinux scheduling

Page 31: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 31/34

5.31Operating System Conce pts

Linux SchedulingLinux Scheduling

Two algorithms: time-sharing and real-timeTime-sharing

Prioritized credit-based ± process with most credits is scheduled nextCredit subtracted when timer interrupt occursWhen credit= 0, another process chosenWhen all processes have credit = 0, recrediting occurs

Based on factors including priority and historyReal-time

Soft real-timePosix.1b compliant ± two classes

FCFS and RRHighest priority process always runs first

Page 32: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 32/34

5.32Operating System Conce pts

Algorithm E valuationAlgorithm E valuation

Deterministic modeling ± takes a particular predetermined workloadand defines the performance of each algorithm for that workload

Queueing models

Simulations

Implementation

Page 33: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 33/34

5.33Operating System Conce pts

5.155.15

Page 34: OS_Ch05

8/3/2019 OS_Ch05

http://slidepdf.com/reader/full/osch05 34/34

Questions ?Questions ?