Basic Concepts Preemptive Vs. Non-preemptive CPU...

17
1 CPU Scheduling Outline What is scheduling in the OS ? What are common scheduling criteria ? How to evaluate scheduling algorithms ? What are common scheduling algorithms ? How is thread scheduling different from process scheduling ? What are the issues in multiple-processor scheduling ? Operating systems case studies. 2 Basic Concepts Multiprogramming most processes alternate between CPU bursts and I/O bursts CPU free and idle during I/O burst schedule another process on the CPU maximizes CPU utilization CPU bound process spends most of its time in the CPU at least a few long CPU bursts I/O bound process spends most its time performing I/O several short CPU bursts 3 CPU Scheduler Responsible for the selection of the next running process part of the OS dispatcher selects from among the processes in memory that are ready to execute based on a particular strategy When does CPU scheduling happen ? process switches from running to waiting state process switches from running to ready state process switches from waiting to ready state process terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive 4 Preemptive Vs. Non-preemptive CPU Scheduling Non-preemptive scheduling process voluntarily releases the CPU (conditions 1 and 4) easy, requires no special hardware poor response time for interactive and real-time systems Preemptive scheduling OS can force a running process involuntarily relinquish the CPU arrival of a higher priority process running process exceeds its time-slot may require special hardware, eg., timer may require synchronization mechanisms to maintain data consistency complicates design of the kernel favored by most OSes

Transcript of Basic Concepts Preemptive Vs. Non-preemptive CPU...

Page 1: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

1

CP

U S

ch

ed

ulin

g

Ou

tlin

e

Wha

t is

sched

ulin

g in the

OS

?

Wha

t are

co

mm

on s

ched

ulin

g c

rite

ria

?

How

to e

valu

ate

sched

ulin

g a

lgori

thm

s ?

Wh

at

are

co

mm

on

sch

ed

ulin

g a

lgo

rith

ms ?

How

is thre

ad s

ched

ulin

g d

iffe

rent fr

om

pro

ce

ss s

ch

edu

ling ?

Wha

t are

the

issu

es in

multip

le-p

roce

ssor

sched

ulin

g ?

Opera

tin

g s

yste

ms c

ase

stu

die

s.

2

Basic

Co

ncep

ts

Multip

rogra

mm

ing

mo

st

pro

ce

sses a

lte

rna

te b

etw

een

CP

U b

urs

tsand I

/O b

urs

ts

CP

U f

ree a

nd

idle

during

I/O

bu

rst

sch

ed

ule

ano

the

r p

roce

ss o

n t

he C

PU

ma

xim

ize

s C

PU

utiliz

ation

CP

U b

ou

nd p

roce

ss

sp

en

ds m

ost

of its t

ime in

th

e C

PU

at

lea

st

a f

ew

lon

gC

PU

burs

ts

I/O

bou

nd p

roce

ss

sp

en

ds m

ost

its t

ime p

erf

orm

ing

I/O

se

vera

l sh

ort

CP

U b

urs

ts

3

CP

U S

ch

ed

ule

r

Respon

sib

le for

the

se

lectio

n o

f th

e n

ext

run

nin

g p

roce

ss

part

of

the O

S d

isp

atc

her

se

lects

fro

m a

mon

g t

he p

roce

sses in m

em

ory

th

at

are

re

ad

y t

o e

xe

cute

base

d o

n a

part

icu

lar

str

ate

gy

Whe

n d

oes C

PU

sched

ulin

g h

ap

pen

?

pro

ce

ss s

witch

es f

rom

ru

nn

ing t

o w

aitin

g s

tate

pro

ce

ss s

witch

es f

rom

ru

nn

ing t

o r

ead

y s

tate

pro

ce

ss s

witch

es f

rom

wa

itin

g t

o r

ead

y s

tate

pro

ce

ss t

erm

ina

tes

Sch

edu

ling u

nde

r 1 a

nd 4

is n

on

pre

em

pti

ve

All

oth

er

sched

ulin

g is p

reem

pti

ve

4

Pre

em

pti

ve V

s. N

on

-pre

em

pti

ve

CP

U S

ch

ed

ulin

g

Non

-pre

em

ptive s

ched

ulin

g

pro

ce

ss v

olu

nta

rily

re

lease

s t

he C

PU

(co

nd

itio

ns 1

an

d 4

)

easy,

requires n

o s

pecia

l hard

ware

poo

r re

spo

nse

tim

e f

or

inte

ractive

and

rea

l-tim

e s

yste

ms

Pre

em

ptive s

ched

ulin

g

OS

ca

n f

orc

e a

ru

nn

ing p

roce

ss invo

lunta

rily

re

linq

uis

h t

he C

PU

arr

ival of

a h

igher

priority

pro

cess

runn

ing p

roce

ss e

xce

ed

s its

tim

e-s

lot

ma

y r

equ

ire

sp

ecia

l h

ard

wa

re,

eg.,

tim

er

ma

y r

equ

ire

syn

chro

niz

atio

n m

echa

nis

ms t

o m

ain

tain

data

co

nsis

tency

co

mplic

ate

s d

esig

n o

f th

e k

ern

el

favore

d b

y m

ost O

Se

s

Page 2: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

5

Dis

patc

her

Sch

edu

ler

is a

part

of

the

dis

patc

her

module

in

the

OS

Fun

ctio

ns o

f th

e d

isp

atc

her

get

the n

ew

pro

ce

ss f

rom

th

e s

ch

ed

ule

r

sw

itch

out

the c

onte

xt

of

the c

urr

ent

pro

ce

ss

giv

e C

PU

co

ntr

ol to

th

e n

ew

pro

ce

ss

jum

p t

o th

e p

rop

er

loca

tion

in t

he n

ew

pro

gra

m t

o r

esta

rt t

hat

pro

gra

m

Tim

e t

aken b

y the d

isp

atc

her

to s

top o

ne

pro

ce

ss a

nd s

tart

ano

the

r ru

nnin

g is c

alle

d the d

isp

atc

h late

ncy

6

Sch

ed

ulin

g Q

ueu

es

Jo

b q

ueu

e: consis

ts o

f a

ll pro

ce

sses

all

job

s (

pro

cesse

s),

once

su

bm

itte

d,

are

in

th

e jo

b q

ue

ue

sch

ed

ule

d b

y t

he lo

ng-t

erm

sch

ed

ule

r

Read

y q

ue

ue:

co

nsis

ts o

f p

rocesse

s in

mem

ory

pro

ce

sses r

ead

y a

nd

wa

itin

g f

or

exe

cutio

n

sch

ed

ule

d b

y t

he s

hort

-te

rm o

r C

PU

sche

du

ler

Devic

e q

ueue

: pro

ce

sses w

aitin

g fo

r a d

evic

e

mu

ltip

le p

roce

sses c

an b

e b

locke

d f

or

the s

am

e d

evic

e

I/O

com

ple

tion

mo

ves p

roce

ss b

ack t

o r

ead

y q

ue

ue

7

Sch

ed

ulin

g Q

ueu

es (

2)

8

Perf

orm

an

ce M

etr

ics f

or

CP

U S

ch

ed

ulin

g

CP

U u

tiliz

atio

n:

perc

enta

ge o

f tim

e t

hat

the

CP

U is b

usy

Thro

ugh

put:

num

be

r of p

rocesse

s t

ha

t com

ple

te t

he

ir

execu

tio

n p

er

tim

e u

nit

Turn

aro

und

tim

e:

am

oun

t of tim

e t

o e

xecute

a p

art

icu

lar

pro

ce

ss (

subm

issio

n t

ime t

o c

om

ple

tio

n t

ime)

Waitin

g tim

e:

am

oun

t of tim

e a

pro

cess h

as b

ee

n w

aitin

g in the

re

ady q

ue

ue

Respon

se

tim

e:

am

oun

t of tim

e it

takes fro

m w

hen

a r

eque

st

wa

s s

ub

mitte

d u

ntil th

e f

irst

resp

on

se

is p

rod

uce

d

Sch

edu

ling

goa

ls

ma

xim

ize

CP

U u

tiliz

ation

and

th

rou

gh

pu

t

min

imiz

e t

urn

aro

und

tim

e,

wa

itin

g t

ime

an

d r

espo

nse

tim

e

be f

air t

o a

ll pro

cesses a

nd a

ll users

Page 3: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

9

Meth

od

fo

r E

valu

ati

ng

C

PU

Sch

ed

ulin

g A

lgo

rith

ms

Evalu

atio

n c

rite

ria

defin

e r

ela

tive im

po

rtan

ce o

f th

e p

erf

orm

ance

me

tric

s

inclu

de o

ther

syste

m-s

pecific

measure

s

Dete

rmin

istic m

odelin

g

takes a

part

icu

lar

pre

dete

rmin

ed w

ork

load

and

defin

es t

he p

erf

orm

ance

o

f e

ach

alg

orith

m f

or

that

wo

rklo

ad

sim

ple

and

fa

st,

giv

es e

xa

ct

num

bers

difficu

lt t

o g

en

era

lize r

esults

ca

n r

ecog

niz

e a

lgorith

m p

erf

orm

ance

tre

nd

s o

ve

r seve

ral in

puts

used

fo

r e

xp

lain

ing s

ch

ed

ulin

g a

lgorith

ms

used

in t

he r

est

of th

is c

ha

pte

r !

10

Wo

rklo

ad

Mo

dels

an

d G

an

tt C

hart

s

Work

load m

odel:

Gantt c

hart

s:

bar

ch

art

to

illu

str

ate

a p

art

icula

r schedule

fig

ure

sh

ow

s b

atc

h s

ch

ed

ule

Process

Arriv

al Tim

eBurst T

ime

P1

08

P2

14

P3

110

P4

62

P1

P2

P3

P4

08

012

22

24

11

Dete

rmin

isti

c M

od

elin

g E

xam

ple

Supp

ose w

e h

ave p

roce

sses A

, B

, a

nd C

, subm

itte

d a

t tim

e 0

.

We w

an

t to

know

the

respon

se

tim

e,

waitin

g tim

e,

and

tu

rnaro

und

tim

e o

f p

rocess A

AB

CA

BC

AC

AC

Tim

e

respo

nse

tim

e =

0+

+w

ait t

ime

turn

aro

und

tim

e

12

Dete

rmin

isti

c M

od

elin

g E

xam

ple

Supp

ose w

e h

ave p

roce

sses A

, B

, a

nd C

, subm

itte

d a

t tim

e 0

.

We w

an

t to

know

the

respon

se

tim

e,

waitin

g tim

e,

and

tu

rnaro

und

tim

e o

f p

rocess B

.

AB

CA

BC

AC

AC

Tim

e

respo

nse

tim

e+

wa

it t

ime

turn

aro

und

tim

e

Page 4: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

13

Dete

rmin

isti

c M

od

elin

g E

xam

ple

Supp

ose w

e h

ave p

roce

sses A

, B

, a

nd C

, subm

itte

d a

t tim

e 0

.

We w

an

t to

know

the

respon

se

tim

e,

waitin

g tim

e,

and

tu

rnaro

und

tim

e o

f p

rocess C

AB

CA

BC

AC

AC

Tim

e

respo

nse

tim

e+

++

wa

it t

ime

turn

aro

und t

ime

14

Meth

od

fo

r E

valu

ati

ng

C

PU

Sch

ed

ulin

g A

lgo

rith

ms (

2)

Queue

ing m

odels

ana

lytica

lly m

ode

l th

e q

ue

ue

beh

avio

r (u

nd

er

so

me a

ssum

ption

s)

involv

es a

lot

of

com

plic

ate

d m

ath

ca

n o

nly

han

dle

a lim

ite

d n

um

ber

of d

istr

ibutio

ns a

nd

alg

orith

ms

ma

y n

ot

be v

ery

accura

te b

eca

use

of

unre

alis

tic a

ssum

ption

s

Sim

ula

tio

ns

get

a w

ork

load info

rmation f

rom

a s

yste

m

sim

ula

te t

he s

ch

ed

ulin

g a

lgorith

m

co

mpu

te t

he p

erf

orm

ance

me

tric

s

tim

e a

nd s

pace

inte

nsiv

e

is p

ractica

lly t

he b

est

evalu

ation

me

tho

d

15

Sim

ula

tio

n Illu

str

ati

on

16

Sch

ed

ulin

g A

lgo

rith

ms

Fir

st C

om

e,

Fir

st S

erv

ed (

FC

FS

)

Short

est Job F

irst

(SJF

)

Prio

rity

Based

Ro

un

d R

ob

in (

RR

)

Multile

vel Q

ueue

Sch

edu

ling

Multile

vel F

ee

dba

ck Q

ueue

Sch

edu

ling

Page 5: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

17

Fir

st

Co

me, F

irst

Serv

ed

Sch

ed

ulin

g

Assig

ns the

CP

U b

ased o

n t

he

ord

er

of th

e r

eq

uests

.

Imple

mente

d u

sin

g a

FIF

O q

ue

ue.

No p

reem

ption

Ad

va

nta

ge

s

str

aig

htf

orw

ard

, sim

ple

to

write

and

und

ers

tand

Dis

advanta

ge

s

avera

ge

wa

itin

g t

ime

ma

y b

e t

oo lo

ng

hug

e v

aria

tion

base

d o

n w

hen

pro

ce

sses a

rriv

e

ca

nn

ot

bala

nce C

PU

-bou

nd

and

I/O

-bou

nd

pro

cesse

s

co

nvo

y e

ffe

ct,

sh

ort

pro

ce

ss b

ehin

d lon

g p

roce

ss

ca

nn

ot

be u

sed

fo

r tim

e-s

haring

syste

ms

18

FC

FS

Sch

ed

ulin

g E

xam

ple

Supp

ose th

at th

e p

roce

sses a

rriv

e in

the

ord

er:

P1

, P

2,

P3

Waitin

g tim

e f

or

P1

= 0

; P

2=

24

; P

3 =

27

Avera

ge

waitin

g tim

e:

(0 +

24

+ 2

7)/

3 =

17

P1

P2

P3

24

27

30

0

Process

Arriv

al Tim

eBurst T

ime

P1

024

P2

03

P3

03

19

FC

FS

Sch

ed

ulin

g (

2)

Supp

ose th

at th

e p

roce

sses a

rriv

e in

the

ord

er

P2

, P

3,

P1

The

Gantt c

hart

for

the

sch

edu

le is:

Wa

itin

g t

ime

fo

r P

1 =

6;P

2=

0; P

3 =

3

Avera

ge

waitin

g tim

e:

(6

+ 0

+ 3

)/3 =

3

Much

bett

er

tha

n p

revio

us c

ase

but

larg

e v

aria

tion

P1

P3

P2

63

30

0

20

Sh

ort

est

Jo

b F

irst

(SJF

) S

ch

ed

ulin

g

Ord

er

each p

rocess b

ased o

n t

he

le

ngth

of its n

ext

CP

U b

urs

t

Allo

cate

CP

U t

o t

he p

roce

ss fro

m t

he

fro

nt

of th

e lis

t

sh

ort

est

next

CP

U b

urs

t

Ad

va

nta

ge

s

SJF

is o

ptim

al

achie

ve

s m

inim

um

avera

ge

wa

itin

g t

ime

fo

r a

giv

en s

et o

f p

roce

sses

Dis

advanta

ge

s

difficu

lt t

o k

now

th

e le

ng

th o

f th

e n

ext

CP

U r

equ

est

ca

n a

sk t

he u

ser,

wh

o m

ay n

ot

kn

ow

any b

ett

er

!

mo

de

l-b

ase

d p

red

ictio

n

ca

n le

ad

to

pro

ce

ss s

tarv

atio

n

Page 6: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

21

Exam

ple

of

SJF

SJF

sch

edu

ling c

ha

rt

Avera

ge

waitin

g tim

e =

(3

+ 1

6 +

9 +

0)

/ 4 =

7

P4

P3

P1

316

09

P2

24

Process

Arriv

al Tim

eBurst T

ime

P1

06

P2

08

P3

07

P4

03

22

Esti

mate

Len

gth

of

Next

CP

U B

urs

t

Can o

nly

estim

ate

the

le

ngth

next

CP

U b

urs

t sim

ilar

to p

revio

us C

PU

burs

ts ?

how

re

levan

t is

th

e h

isto

ry o

f p

revio

us C

PU

burs

ts ?

Calc

ula

ted a

s a

n e

xpone

ntial avera

ge o

f th

e p

revio

us C

PU

burs

ts f

or

the

pro

ce

ss

Form

ula

:

10 ,

3

.

burs

t

CP

Un

ex

t

for

the

v

alu

ep

redic

ted

2.

burs

t

CP

U

o

fle

ngth

actu

al

1

.

1n

th

nn

t

nn

nt

1

1

23

Esti

mate

Len

gth

of

the N

ext

CP

U B

urs

t (2

)

=0 n

+1

=

n

Rece

nt

his

tory

doe

s n

ot

co

un

t

=1

n+

1=

t n

On

ly t

he a

ctu

al la

st

CP

U b

urs

t co

un

ts

If w

e e

xpand

the

form

ula

, w

e g

et:

n+

1=

t n

+(1

-)

t n-1

+(1

-)j

t n-j

+(1

-)n

+1

0

Sin

ce

both

and

(1 -

) are

le

ss tha

n o

r equ

al to

1, e

ach

su

ccessiv

e term

has le

ss w

eig

ht th

an

its

pre

decesso

r

24

Esti

mate

Len

gth

of

the N

ext

CP

U B

urs

t (3

)

Page 7: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

25

Pre

em

pti

ve S

JF

Sch

ed

ulin

g

New

sh

ort

er

pro

ce

ss c

an p

ree

mpt

longe

r cu

rren

t ru

nnin

g

pro

ce

ss

Sh

ort

est

Re

ma

inin

g T

ime

First

sch

ed

ulin

g c

ha

rt:

Ave

rag

e w

aitin

g t

ime

= 6

.5

Process

Arriv

al Tim

eBurst T

ime

P1

08

P2

14

P3

29

P4

35

P 1

P2

P4

P1

P3

01

510

17

26

26

Pri

ori

ty S

ch

ed

ulin

g

A p

riori

ty n

um

be

r asso

cia

ted w

ith

ea

ch

pro

ce

ss

CP

U a

llocate

d to

the

pro

ce

ss w

ith

the

hig

he

st

pri

ori

ty

equ

al p

rio

rity

pro

ce

sses s

ch

ed

ule

d in F

CF

S o

rde

r

Inte

rna

lly d

ete

rmin

ed

prio

ritie

s

tim

e lim

it,

me

mory

re

qu

ire

men

ts,

etc

SJF

use

s n

ext

CP

U b

urs

t fo

r its p

rio

rity

(h

ow

?)

Exte

rnally

sp

ecifie

d p

rio

ritie

s

pro

ce

ss im

po

rtan

ce,

user-

leve

l, e

tc

Can b

e p

reem

ptive o

r non

-pre

em

ptive

Text

uses lo

w n

um

be

rs f

or

hig

h p

riori

tie

s

27

Pri

ori

ty S

ch

ed

ulin

g (

2)

Advanta

ges

prio

ritie

s c

an b

e m

ade

as g

ene

ral a

s n

eed

ed

Dis

advanta

ge

low

prio

rity

pro

ce

ss m

ay n

eve

r e

xe

cute

(in

defin

ite

blo

ckin

g o

r sta

rvatio

n)

Agin

g

techn

ique

to

pre

ve

nt

sta

rvatio

n

incre

ase

prio

rity

of

pro

ce

sses w

ith

tim

e

28

Ro

un

d R

ob

in S

ch

ed

ulin

g (

RR

)

Roun

d r

ob

in a

lgori

thm

arr

an

ge

job

s in F

CF

S o

rde

r

allo

ca

te C

PU

to

th

e f

irst

job in

th

e q

ue

ue

fo

r o

ne

tim

e-s

lice

pre

em

pt

job

, a

dd

it to

th

e e

nd

of th

e q

ue

ue

allo

ca

te C

PU

to

th

e n

ext

job

and

co

ntin

ue

...

One t

ime s

lice is c

alle

d a

tim

e q

ua

ntu

m

Is b

y d

efin

itio

n p

reem

ptive

ca

n b

e c

onsid

ere

d a

s F

CF

S w

ith

pre

em

ptio

n

Advanta

ges

sim

ple

, a

void

s s

tarv

atio

n

Dis

advanta

ge

s

ma

y invo

lve

a larg

e c

onte

xt

sw

itch

overh

ea

d

hig

her

avera

ge w

aitin

g t

ime t

han S

JF

an I

/O b

ou

nd

pro

ce

ss o

n a

hea

vily

loa

de

d s

yste

m w

ill r

un s

low

er

Page 8: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

29

Exam

ple

of

RR

wit

h T

ime Q

uan

tum

= 4

The

Gantt s

ched

ulin

g c

hart

is:

Avera

ge

waitin

g tim

e =

5.6

6

P1

P2

P3

P1

P1

P1

P1

P1

04

710

14

18

22

26

30

Process

Burst T

imes

P1

24

P2

3

P3

3

30

Ro

un

d R

ob

in S

ch

ed

ulin

g (

2)

Perf

orm

ance

dep

en

ds o

n t

he le

ng

th o

f th

e t

ime q

ua

ntu

m

Genera

lly,

tim

e q

uan

ta r

ang

e f

rom

10

100

mill

ise

con

ds

co

nte

xt

sw

itch

tim

e is less t

han

10

mic

rose

con

ds

RR

has la

rge

r w

aitin

g tim

e,

but

pro

vid

es b

ett

er

respon

se

tim

e f

or

inte

ractive

syste

ms

Turn

aro

und

tim

e d

ep

end

s o

n t

he

siz

e o

f th

e t

ime q

ua

ntu

m

31

Tu

rnaro

un

d T

ime V

ari

es W

ith

Th

e T

ime Q

uan

tum

32

Mu

ltilevel Q

ueu

e

Read

y q

ue

ue is p

art

itio

ned

in

to s

epa

rate

que

ues:

fore

gro

und

(in

tera

ctive)

backg

rou

nd (

ba

tch)

Each q

ueu

e h

as its

ow

n s

ched

ulin

g a

lgori

thm

fore

gro

und

R

R

backg

rou

nd

F

CF

S

Sch

edu

ling m

ust

be d

on

e b

etw

ee

n t

he

qu

eue

s

Fix

ed p

rio

rity

sch

ed

ulin

g;

(i.e

., s

erv

e a

ll fr

om

fo

reg

rou

nd

th

en

fro

m

backgro

und).

P

ossib

ility

of sta

rvation.

Tim

e s

lice

e

ach

que

ue

gets

a c

ert

ain

am

ou

nt

of

CP

U t

ime w

hic

h it

ca

n

sch

ed

ule

am

on

gst

its p

roce

sses;

i.e

., 8

0%

to

fo

reg

rou

nd

in R

R,

20%

to

b

ackg

rou

nd

in F

CF

S

Page 9: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

33

Mu

ltilevel Q

ueu

e S

ch

ed

ulin

g

Use

d w

he

n

pro

ce

sses c

an b

e e

asily

cla

ssifie

d into

gro

ups

each

gro

up h

as a

diffe

ren

t sch

ed

ulin

g r

equ

ire

men

t

Alg

ori

thm

part

itio

n r

ead

y q

ue

ue

into

mu

ltip

le q

ue

ue

s

dete

rmin

e s

om

e s

ch

ed

ulin

g a

lgorith

m f

or

each

pro

ce

sses in e

ach

que

ue

FC

FS

, S

JF,

RR

, e

tc.

dete

rmin

e inte

r-q

ue

ue

sch

ed

ulin

g

fixe

d p

rio

rity

, fixe

d C

PU

utiliz

ation

per

que

ue

, e

tc.

perm

ane

ntly a

ssig

n a

pro

cess t

o a

part

icu

lar

que

ue

34

Mu

ltilevel Q

ueu

e S

ch

ed

ulin

g E

xam

ple

Exam

ple

: fo

regro

und

Vs.

backg

rou

nd

pro

ce

sses

fore

gro

und

are

in

tera

ctive,

backg

rou

nd

are

batc

h p

roce

sses

fore

gro

und

have

priority

over

backg

rou

nd

intr

a-q

ue

ue

sch

ed

ulin

g

fore

gro

und

re

spo

nse

tim

e,

backg

rou

nd

lo

w o

verh

ea

d,

no s

tarv

atio

n

fore

gro

und

R

R,

backg

rou

nd

F

CF

S

sch

ed

ulin

g b

etw

een

th

e q

ue

ue

s

fixe

d p

rio

rity

sch

ed

ulin

g;

fore

gro

und

h

ighe

r p

rio

rity

tim

e s

lice;

80%

to

fo

regro

und in R

R,

20%

to b

ackgro

und in F

CF

S

35

Mu

ltilevel Q

ueu

e S

ch

ed

ulin

g E

xam

ple

(2)

36

Mu

ltilevel F

eed

back Q

ueu

e S

ch

ed

ulin

g

Allo

ws p

roce

ss to m

ove b

etw

een

que

ues

used

to

dyn

am

ically

so

rt p

roce

ss b

ase

d o

n t

heir t

yp

ica

l C

PU

burs

ts

Alg

ori

thm

mu

ltip

le q

ue

ue

s w

ith

diffe

ren

t fixe

d p

rio

ritie

s

roun

d r

obin

at

each

priority

leve

l

run h

igh

est

prio

rity

job

s f

irst,

once

th

ose

fin

ish

, ru

n n

ext

hig

he

st

prio

rity

, e

tc

job

s s

tart

in t

he h

igh

est

prio

rity

que

ue

if t

ime s

lice e

xpires,

dro

p t

he job b

y o

ne level

if t

ime s

lice d

oe

s n

ot

exp

ire

, p

ush

th

e jo

b u

p b

y o

ne

leve

l

Page 10: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

37

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

0

Tim

e

AB

C

02

59

38

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

1

Tim

e

A

BC

003

7

A

1

39

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

2

Tim

e

ABC

004

3

A

1

B

40

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

3

Tim

e

AB

C

06

3

A

1

BC

Page 11: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

41

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

3

Tim

e

AB

C

06

3

A

1

BC

Suppose A

is b

locked o

n I

/O

42

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

3

Tim

e

A

BC

0

52

A

1

BC

Suppose A

is b

locked o

n I

/O

0

43

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

5

Tim

e

A

B

C

0

3

A

1

BC

Suppose A

now

retu

rns fro

m I

/O

0

44

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

6

Tim

eA

B

C

3

AB

C

0

Page 12: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

45

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

8

Tim

eA

B

C

3

AB

C

0

C

46

Exam

ple

of

Mu

ltilevel F

eed

back Q

ueu

es

Prio

rity

0 (

tim

e s

lice =

1):

Prio

rity

1 (

tim

e s

lice

= 2

):

Prio

rity

2 (

tim

e s

lice =

4):

tim

e =

9

Tim

eA

BC

AB

CC

47

Mu

ltilevel F

eed

back Q

ueu

es

Appro

xim

ate

s S

RT

F

a C

PU

-bou

nd

job

dro

ps lik

e a

rock

I/O

-bou

nd

job

s s

tay n

ea

r th

e t

op

Still

unfa

ir fo

r lo

ng r

un

nin

g jo

bs

co

un

ter-

me

asu

re:

Ag

ing

incre

ase

th

e p

riority

of

lon

g r

unn

ing job

s if

they a

re n

ot

se

rvic

ed f

or

a

period

of

tim

e

tric

ky t

o tu

ne

agin

g

48

Lo

ttery

Sch

ed

ulin

g

Adap

tive s

ched

ulin

g a

pp

roa

ch

to a

dd

ress t

he

fair

ne

ss p

roble

m

Alg

ori

thm

each

pro

ce

ss o

wn

s s

om

e t

ickets

on e

ach

tim

e s

lice

, a

tic

ke

t is

ra

ndo

mly

pic

ke

d

on a

vera

ge

, th

e a

llocate

d C

PU

tim

e is p

rop

ort

iona

l to

th

e n

um

ber

of

tickets

g

ive

n t

o e

ach

job

To a

ppro

xim

ate

SJF, short

jobs g

et

more

tic

ke

ts

To a

void

sta

rvation,

each job g

ets

at le

ast one

tic

ke

t

Page 13: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

49

Lo

ttery

Sch

ed

ulin

g E

xam

ple

Short

jo

bs: 10 t

icke

ts e

ach

Lon

g jobs: 1 t

icke

t each

# s

ho

rt jo

bs

/#

lon

g jo

bs

% o

f C

PU

fo

r eac

h s

ho

rt jo

b%

of

CP

U f

or

eac

h lo

ng

jo

b

1/1

91

%9

%

0/2

0%

50

%

2/0

50

%0

%

10

/11

0%

1%

1/1

05

0%

5%

50

Th

read

Sch

ed

ulin

g

On s

yste

ms s

uppo

rtin

g t

hre

ads

ke

rne

lth

rea

ds a

re t

he r

eal sch

ed

ulin

g e

ntitie

s

user

thre

ads n

ee

d t

o b

e m

app

ed

to

ke

rne

l th

rea

ds f

or

exe

cutio

n

sch

ed

ulin

g a

ttribu

tes m

ay b

e s

et

at th

rea

d c

reatio

n

Conte

ntion

-scope

PT

HR

EA

D_S

CO

PE

_P

RO

CE

SS

gro

up u

ser

thre

ads t

o c

onte

nd

fo

r co

mm

on

ke

rne

l th

rea

d(s

)

PT

HR

EA

D_S

CO

PE

_S

YS

TE

M

dire

ctly a

ssig

n t

o k

ern

el th

rea

d,

co

nte

nd

s w

ith

oth

er

kern

el th

rea

ds

inheri

tsch

ed

PT

HR

EA

D_IN

HE

RIT

_S

CH

ED

inh

erit

sch

ed

ulin

g p

olic

y a

nd

prio

rity

fro

m p

are

nt

thre

ad

PT

HR

EA

D_E

XP

LIC

IT_S

CH

ED

exp

licitly

sp

ecify s

ch

ed

ulin

g p

olic

y a

nd

prio

rity

of

the n

ew

th

rea

d

51

Th

read

Sch

ed

ulin

g (

2)

sched

polic

y

SC

HE

D_O

TH

ER

regu

lar

non-r

eal-tim

e s

ch

ed

ulin

g

SC

HE

D_R

R

real-tim

e r

oun

d-r

obin

sch

ed

ulin

g

SC

HE

D_F

IFO

real-tim

e F

CF

S s

che

du

ling

sched

para

m

set/

get

the p

riority

of

the t

hre

ad

All

para

mete

rs o

nly

rele

vant

if t

hre

ad lib

rary

su

pp

ort

s n

on

-one-t

o-o

ne

user

level th

rea

ds

for

real-tim

e s

ch

ed

ulin

g

52

Pth

read

Sch

ed

ulin

g E

xam

ple

int main(int argc, char *argv[]){

int i;

pthread t tid[5];

pthread attr t attr;

pthread attr init(&attr); /* get the default attributes */

/* set the scheduling algorithm to PROCESS or SYSTEM */

pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);

/* set the scheduling policy -

FIFO, RT, or OTHER */

pthread attr setschedpolicy(&attr, SCHED OTHER);

for (i = 0; i < 5; i++)

pthread create(&tid[i],&attr,runner,NULL);

for

(i =

0;

i <

NU

M T

HR

EA

DS

; i+

+)

pth

read join

(tid

[i], N

ULL);

} void

*ru

nner(

void

*para

m){

printf("

I am

a t

hre

ad

\n")

;

pth

read e

xit(0

);

}

Page 14: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

53

Mu

ltip

le-P

rocesso

r S

ch

ed

ulin

g Issu

es

Multip

roce

ssor

Sch

edu

ling

asym

me

tric

mu

ltip

roce

ssin

g

only

one

pro

cesso

r a

ccesse

s t

he s

yste

m d

ata

str

uctu

res

sim

ple

sym

me

tric

mu

ltip

roce

ssin

g

(SM

P)

each

pro

ce

ssor

is s

elf-s

ch

ed

ulin

g

nee

d t

o m

ain

tain

sch

ed

ule

r d

ata

str

uctu

res s

yn

chro

niz

atio

n

Pro

cesso

r affin

ity

pro

ce

ss h

as a

ffin

ity f

or

pro

ce

ssor

on w

hic

h it

is c

urr

ently r

unn

ing

redu

ce m

em

ory

and

ca

che

overh

ea

d

me

mory

affin

ity im

po

rtan

t fo

r N

UM

A a

rchite

ctu

res

so

ft a

nd

hard

pro

ce

ssor

affin

ity

how

str

ictly O

S m

ain

tain

s a

ffin

ity p

olic

y

54

NU

MA

an

d C

PU

Sch

ed

ulin

g

55

Mu

ltip

le-P

rocesso

r S

ch

ed

ulin

g Issu

es

Loa

d b

ala

ncin

g

ke

ep

wo

rklo

ad

even

ly d

istr

ibute

d a

cro

ss a

ll C

PU

s

impo

rtan

t if e

ach

pro

ce

ssor

has its

ow

n q

ue

ue

of

read

y p

roce

sses

push

and p

ull

mig

ration

push

or

pu

ll ta

sks to

wa

rds idle

pro

ce

ssors

Multic

ore

pro

ce

ssors

mu

ltip

le p

roce

ssor

co

res o

n s

am

e p

hysic

al ch

ip

unifo

rm m

em

ory

access,

faste

r in

terc

ore

co

mm

un

ica

tion

ma

y b

e s

imu

lta

ne

ou

sly

mu

ltith

rea

de

d (

SM

T, o

r h

ype

rthre

ad

ed

)

instr

uction

s f

rom

mu

ltip

le t

hre

ads s

imulta

ne

ou

sly

liv

e in d

iffe

ren

t p

ipelin

e s

tage

s

OS

giv

en

a v

iew

of

one

pro

cesso

r p

er

hard

wa

re t

hre

ad

ma

y r

edu

ce m

em

ory

sta

lls

ma

y incre

ase

re

sou

rce c

onte

ntio

n

56

Case S

tud

ies:

So

lari

s S

ch

ed

ulin

g

Prio

rity

-based s

ched

ulin

g

Six

cla

sse

s

real tim

e, syste

m,

fair s

hare

, fixe

d p

rio

rity

, tim

e s

ha

r, a

nd inte

ractive

(in o

rde

r o

f p

rio

rity

)

diffe

ren

t p

rio

ritie

s a

nd

sch

ed

ulin

g a

lgorith

ms in d

iffe

ren

t cla

sse

s

The

defa

ult c

lass is tim

e s

hari

ng

uses m

ultile

vel fe

ed

ba

ck q

ue

ue

with

va

ria

ble

tim

e s

lice

s

inve

rse r

ela

tion

ship

betw

een

prio

ritie

s a

nd

tim

e s

lice

s

goo

d r

espo

nse

tim

e f

or

inte

ractive

pro

ce

sses a

nd

goo

d t

hro

ugh

pu

t fo

r C

PU

-bou

nd

pro

ce

sses

se

e t

he d

ispa

tch t

able

Page 15: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

57

So

lari

s D

isp

atc

h T

ab

le

58

Case S

tud

ies:

So

lari

s S

ch

ed

ulin

g (

2)

Real-

tim

e s

ched

ulin

g c

lass

hig

he

st

prio

rity

, b

ou

nd

ed

tim

e r

espo

nse

sh

ou

ld b

e c

are

ful be

fore

putt

ing

a p

rocess in

th

is c

lass

Syste

m s

ched

ulin

g c

lass

reserv

ed f

or

ke

rne

l th

rea

ds (

sch

ed

ulin

g a

nd

pa

gin

g d

ae

mon

)

Fix

ed p

riori

ty s

ched

ulin

g c

lass

prio

ritie

s n

ot

dyn

am

ically

adju

ste

d

Fair

sh

are

sch

edu

ling c

lass

base

d o

n lo

tte

ry s

ch

ed

ulin

g

pro

ce

sses g

rou

pe

d into

pro

jects

; e

ach

pro

ject

assig

ne

d s

om

e n

ub

mer

of

lott

ery

to

ken

s

pro

ce

sses w

ith

in e

ach

pro

ject

sh

are

th

e t

oken

fa

irly

Pro

ce

ss c

lass p

rio

ritie

s c

on

ve

rte

d t

o g

lob

al p

rio

ritie

s

59

Map

to

Glo

bal P

rio

riti

es

60

Case S

tud

ies:

Lin

ux S

ch

ed

ulin

g (

v2.5

)

Lin

ux s

ch

ed

ulin

g (

ve

rsio

n 2

.5)

co

nsta

nt

ord

er

O(1

) sch

ed

ulin

g t

ime

su

pp

ort

fo

r S

MP

syste

ms

pro

vid

e f

airne

ss a

nd

su

pp

ort

fo

r in

tera

ctive

ta

sks

Prio

rity

-ba

se

d,

pre

em

ptive

, g

lob

al ro

un

d-r

ob

in s

ch

ed

ulin

g

two

prio

rity

ra

ng

es:

tim

e-s

haring

and

re

al-tim

e

Rea

l-ti

me

ra

ng

e f

rom

0 t

o 9

9 a

nd

nic

e v

alu

e f

rom

100

to

140

num

erica

lly low

er

va

lues h

ave

hig

he

r p

rio

ritie

s

hig

he

r p

rio

rity

ta

sks a

re g

ive lon

ge

r tim

e s

lice

Ta

sk r

un-a

ble

as lon

g a

s t

ime le

ft in t

ime

slic

e

(active)

If n

o t

ime le

ft (

exp

ire

d),

not

run

-able

until a

ll o

the

r ta

sks u

se t

heir

slic

es

se

e m

app

ing f

igure

Page 16: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

61

Pri

ori

ties a

nd

Tim

e-s

lice len

gth

62

Case S

tud

ies:

Lin

ux S

ch

ed

ulin

g (

v2.5

)

More

abo

ut L

inux s

ched

ulin

g

ma

inta

ins t

wo

arr

ays:

active

and e

xp

ire

d

tasks lin

ke

d in a

rra

ys a

ccord

ing t

o t

heir p

rio

ritie

s

active t

asks o

n e

xp

ira

tion

of

their t

ime

qua

nta

mo

ve t

o t

he e

xp

ire

d a

rray

real-tim

e t

asks h

ave

sta

tic

prio

ritie

s

norm

al ta

sks h

ave

dyna

mic

prio

ritie

s

63

Case S

tud

ies:

Lin

ux S

ch

ed

ulin

g (

v2.5

)

Even m

ore

abo

ut L

inux s

ched

ulin

g

Aft

er

the t

ime s

lice o

f a

pro

ce

ss is u

se

d u

p,

the p

roce

ss m

ust w

ait u

ntil a

ll re

ad

y p

roce

sses t

o u

se u

p t

heir t

ime

slic

e (

or

be b

locke

d)

roun

d-r

obin

app

roa

ch

no s

tarv

ation p

roble

m

for

a u

ser

pro

cess,

its p

riority

ma

y +

or

5 d

epe

nd

ing w

heth

er

the p

roce

ss

is I

/O-

bou

nd

or

CP

U-b

ound

I/O

bo

un

d p

roce

ss is g

ive

n h

ighe

r p

rio

rity

64

Case S

tud

ies:

Lin

ux S

ch

ed

ulin

g (

v2.6

.23+

)

Co

mp

lete

ly F

air

Sch

ed

ule

r (C

FS

)

Sch

ed

ulin

g c

lasses,

each w

ith s

pecific

priori

ty

schedule

r pic

ks h

ighest priori

ty t

ask in h

ighest schedulin

g c

lass

rath

er

tha

n q

ua

ntu

m b

ase

d o

n fix

ed

tim

e a

llotm

en

ts, b

ase

d o

n p

rop

ort

ion

of C

PU

tim

e

two

sch

ed

ulin

g c

lasse

s in

clu

de

d (

de

fau

lt, re

al-

tim

e),

oth

ers

ca

n b

e a

dd

ed

Qu

an

tum

ca

lcu

late

d b

ase

d o

n n

ice

va

lue

fro

m -

20

to

+19

low

er

valu

e is h

igher

priority

ca

lcu

late

s t

arg

et

late

nc

y

inte

rva

l o

f tim

e d

urin

g w

hic

h ta

sk s

ho

uld

ru

n a

t le

ast o

nce

targ

et la

ten

cy c

an

in

cre

ase

if s

ay n

um

be

r o

f a

ctive

ta

sks in

cre

ases

CF

S s

ch

ed

ule

r m

ain

tain

s p

er

task v

irtu

al ru

n t

ime

in

va

ria

ble

vruntime

associa

ted w

ith d

ecay f

acto

r based o

n p

riority

of

task

low

er

priori

ty is h

igher

decay r

ate

no

rma

l d

efa

ult p

rio

rity

yie

lds v

irtu

al ru

n tim

e =

actu

al ru

n tim

e

task w

ith low

est

vir

tual ru

n t

ime r

uns n

ext

Page 17: Basic Concepts Preemptive Vs. Non-preemptive CPU Schedulingittc.ku.edu/~kulkarni/teaching/EECS678/slides/chap6.handout.pdf · Basic Concepts Multiprogramming ... Preemptive Vs. Non-preemptive

65

Case S

tud

ies:

Lin

ux S

ch

ed

ulin

g (

v2.6

.23+

)

66

Case S

tud

ies:

Su

mm

ary

Basic

id

ea f

or

sched

ule

user

pro

cesse

s is the

sam

e f

or

all

syste

ms

low

er

prio

rity

fo

r C

PU

bou

nd

pro

ce

ss

incre

ase

prio

rity

fo

r I/

O b

ou

nd

pro

ce

ss

The

sched

ulin

g in

Sola

ris / L

inux is m

ore

co

ncern

ed a

bo

ut

fair

ne

ss

mo

re p

op

ula

r a

s t

he O

Ses f

or

se

rvers

The

sched

ulin

g in

Win

do

w X

P is m

ore

co

ncern

ed a

bo

ut u

se

r p

erc

eiv

ed

pe

rfo

rma

nce

mo

re p

op

ula

r a

s t

he O

S fo

r p

ers

ona

l co

mpu

ters