Docsity

Process Scheduling: Goals, Classes, and Algorithms, Slides of Computer Science

Partial preview of the text

Download Process Scheduling: Goals, Classes, and Algorithms and more Slides Computer Science in PDF only on Docsity!

CSE 410Computer Systems

L^

t^

P^

S h d li

Lecture 17 – Process Scheduling

SchedulingScheduling •^

In discussing processes and threads, we talked about

g p

context switching– an interrupt occurs (device completion, timer, …)– a thread causes an exception (a

trap

or a

fault

•^

We glossed over the choice of which thread ischosen to be run nextchosen to be run next– “some thread from the ready queue”

-^

This decision is called scheduling

g

  • context switching is a mechanism inside the OS• scheduling is a policy

3

Classes of SchedulersClasses

of Schedulers

-^

Batch

Throughput / utilization oriented

-^

Throughput / utilization oriented

-^

Example: audit inter-bank funds transfers each night, Pixar rendering

-^

Interactive–

Response time oriented

-^

Hard Real Time–

Deadline driven– Example: embedded systems (cars, airplanes, etc.)

-^

Soft Real Time–

Video, TIVO, etc.

-^

Parallel–

Speedup driven– Example: “space-shared” use of a 1000-processor machine for largeExample:

space shared

use of a 1000 processor machine for large

simulations

-^

Others…

-^

We’ll be talking primarily about interactive schedulers (as does the text)We ll be talking primarily about interactive schedulers (as does the text).

5

Multiple levels of scheduling decisionsMultiple

levels of scheduling decisions

-^

Long term

Sh

ld^

“j b” b

“i iti t d ”

h^

ld it b

h ld?

  • Should a new “job” be “initiated,” or should it be held?– typical of batch systems– what might cause you to make a “hold” decision?M di

t

-^

Medium term– Should a running program be temporarily marked as non-

runnable (e.g., swapped out)? Short term

-^

Short term– Which thread should get the CPU next? For how long?– Which I/O operation should be sent to the disk next?

O^

lti

  • On a multiprocessor:
    • should we attempt to coordinate the running of threads from the same address space in some way? - should we worry about cache state (processor affinity)?• should we worry about cache state (processor affinity)?

6

Scheduling Goals II: FairnessScheduling

Goals II: Fairness

•^

No single, compelling definition of “fair”

g^

p^

g

  • How to measure fairness?
    • Equal CPU consumption? (over what time scale?)scale?) - Fair per-user? per-process? per-thread?– What if one thread is CPU bound and one is IO

bound?

-^

Sometimes the goal is to be unfair:– Explicitly favor some particular class of requests– Explicitly favor some particular class of requests

(priority system), but…

  • avoid starvation (be sure everyone gets at least

i^

some service)

8

The basic situationThe

basic situation

Scheduling:- Who to assign each resource to

h^

l

•••

•••

  • When to re-evaluate your decisions

Schedulable units

Resources

9

Algorithm #1: FCFS/FIFOAlgorithm

#1: FCFS/FIFO

-^

First-come first-served / First-in first-out (FCFS/FIFO)

h d l

i^

th

d^

th t th

i

  • schedule in the order that they arrive– “real-world” scheduling of people in (single) lines
    • supermarkets, bank tellers, McD’s, Starbucks …
      • (sometimes we separate job classes – DMV)– (sometimes we separate job classes – DMV)
        • typically non-preemptive
          • no context switching at supermarket!
            • jobs treated equally, no starvation

j^

q^

y,

  • In what sense is this “fair”? -^

Sounds perfect!– in the real world, when does FCFS/FIFO work well?

  • even then, what’s it’s limitation?
    • and when does it work badly?

11

FCFS/FIFO exampleFCFS/FIFO

example

Job A

B^

C time

1

C B^

Job A

2

-^

Suppose the duration of A is 5, and the durations of B and Care each 1– average response time for schedule 1 (assuming A, B,

and C all arrive at about time 0) is (5+6+7)/3 = 18/3 = 6and C all arrive at about time 0) is (5+6+7)/

18/

6

  • average response time for schedule 2 is (1+2+7)/3 =

10/3 = 3.

  • consider also “elongation factor” – a “perceptual”

measure:measure:• Schedule 1: A is 5/5, B is 6/1, C is 7/1 (worst is 7,

ave is 4.7)

  • Schedule 2: A is 7/5, B is 1/1, C is 2/1 (worst is 2, Schedule 2: A is 7/5, B is 1/1, C is 2/1 (worst is 2,ave is 1.5)

12

Algorithm #2: SPT/SJFAlgorithm

#2: SPT/SJF

•^

Shortest processing time first / Shortest job first

p^

g^

j

(SPT/SJF)– choose the request with the smallest service

i^

t

requirement

-^

Provably optimal

with respect to average response

timetime

14

SPT/SJF optimalitySPT/SJF

optimality

s f^

s g

t k^

t+s k

f^

t+s k

+s f g

•^

In any schedule that is not SPT/SJF, there is somedj

t^

i^

f^

t^

f^

d^

h^

th

i^

ti

adjacent pair of requests f and g where the service time(duration) of f, s

, exceeds that of g, sf

g

•^

The total contribution to average response time of f andThe total contribution to average response time of f andg is 2t

+2sk^

+sgf

•^

If you interchange f and g, their total contribution will be2t

+2sk

+sfg

, which is smaller because s

< sg^

f

15

Algorithm #3: RRAlgorithm

#3: RR

-^

Round Robin scheduling (RR)

d^

i^

d^

i^

l^

FIFO

  • ready queue is treated as a circular FIFO queue– each request is given a time slice, called a quantum
    • request executes for duration of quantum, or until it blocksblocks - what signifies the end of a quantum? - time-division multiplexing (time-slicing) - great for timesharing - no starvation -^

Sounds perfect!

C^

S?

  • how is RR an improvement over FCFS?– how is RR an improvement over SPT?– how is RR an approximation to SPT?

what are the warts?

  • what are the warts?

17

RR drawbacksRR

drawbacks

•^

What if all jobs are exactly the same length?

j^

y^

g

  • What would the pessimal schedule be? -^

What do you set the quantum to be?– no value is “correct”

if small then context switch often incurring

  • if small, then context switch often, incurring high overhead - if large, then response time degrades

g ,

p^

g

  • treats all jobs equally
    • how might we fix this?

18

Priority drawbacksPriority

drawbacks

-^

How are you going to assign priorities?

y^

g^

g^

g^

p

-^

Starvation

if th

i^

dl^

l^

f hi h

i^

it^

j b

  • if there is an endless supply of high priority jobs, no

low-priority job will ever run

-^

Solution: “age” threads over time– increase priority as a function of accumulated wait time

d^

i^

it^

f^

ti^

f^

l t d

  • decrease priority as a function of accumulated

processing time

  • many ugly heuristics have been explored in this space

20

Combining algorithmsCombining

algorithms

-^

In practice, any real system uses some sort of hybrid

p^

y^

y^

y

approach, with elements of FCFS, SPT, RR, and Priority

-^

Example: multi level feedback queues (MLFQ)

-^

Example: multi-level feedback queues (MLFQ)– there is a hierarchy of queues– there is a priority ordering among the queues

p^

y^

g^

g^

q

  • new requests enter the highest priority queue– each queue is scheduled RR

h^

diff

t^

t

  • queues have different quanta– requests move between queues based on execution

history

21