26
2.5 Scheduling 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 Many times when more than 1 process is waiting for the CPU process is waiting for the CPU (in the ready queue). (in the ready queue). The The scheduler scheduler (using a (using a scheduling scheduling algorithm algorithm ) decides which process ) decides which process will run next. will run next. User satisfaction is important. User satisfaction is important.

2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Embed Size (px)

Citation preview

Page 1: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

2.5 Scheduling2.5 Scheduling

Given a multiprogramming system.Given a multiprogramming system.Many times when more than 1 Many times when more than 1

process is waiting for the CPU (in the process is waiting for the CPU (in the ready queue).ready queue).

The The schedulerscheduler (using a (using a scheduling scheduling algorithmalgorithm) decides which process will ) decides which process will run next.run next.

User satisfaction is important.User satisfaction is important.

Page 2: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Context switchContext switch

When the CPU changes from one process When the CPU changes from one process to another.to another.

Expensive operation.Expensive operation.1.1. User mode to kernel modeUser mode to kernel mode

2.2. Save state of current processSave state of current process Registers, invalidate cache, MMURegisters, invalidate cache, MMU

3.3. Run schedulerRun scheduler Pick next process to runPick next process to run Load state of next processLoad state of next process

4.4. Run next processRun next process

Page 3: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Process Process behaviobehavio

rr

Types:Types:I/O bound – spend most time performing I/O bound – spend most time performing

I/OI/OCompute bound – spend most time Compute bound – spend most time

performing computationperforming computationMixtureMixture

Page 4: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

When do we need to When do we need to schedule?schedule?

creation (before/after parent?)creation (before/after parent?)exitexitblock (I/O, semaphore, sleep, wait, block (I/O, semaphore, sleep, wait,

etc.)etc.) I/O interrupt/completionI/O interrupt/completionclock interruptclock interrupt

non preemptive (run until you block or non preemptive (run until you block or “cooperate”“cooperate”

preemptivepreemptive

Page 5: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Scheduling environmentsScheduling environments

BatchBatch InteractiveInteractiveReal timeReal time

Page 6: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Scheduling algorithm goalsScheduling algorithm goals

Page 7: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Concepts & definitionsConcepts & definitions

Throughput = # of jobs completed per hourThroughput = # of jobs completed per hour Turnaround time = avg of “start (submit) to Turnaround time = avg of “start (submit) to

completion” times; avg wait timecompletion” times; avg wait time CPU utilization = avg of CPU busynessCPU utilization = avg of CPU busyness Response time = time between issuing a Response time = time between issuing a

command and getting the resultcommand and getting the result Proportionality = user perception that Proportionality = user perception that

“complex” things take a long time and that “complex” things take a long time and that is fine but “simple” things must be quickis fine but “simple” things must be quick

Predictability = regularity, especially Predictability = regularity, especially important for audio and video streamingimportant for audio and video streaming

Page 8: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Batch schedulingBatch scheduling

First-come first-servedFirst-come first-servedShortest job firstShortest job firstShortest remaining time nextShortest remaining time nextThree-level schedulingThree-level scheduling

Page 9: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Batch schedulingBatch scheduling

First-come first-servedFirst-come first-servedSimpleSimpleNon preemptiveNon preemptiveProcess runs until it either blocks on I/O Process runs until it either blocks on I/O

or finishesor finishes

Page 10: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Batch schedulingBatch scheduling

Shortest job firstShortest job firstOptimal turnaround time (when all start Optimal turnaround time (when all start

together)together)Requires that we know run time Requires that we know run time a prioria priori

Page 11: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Batch schedulingBatch scheduling

Shortest remaining time nextShortest remaining time nextPreemptive version of shortest job firstPreemptive version of shortest job firstRequires Requires a prioria priori information informationWhen a new job arrives, its total time is When a new job arrives, its total time is

compared to the current process’ compared to the current process’ remaining time. If the new job needs remaining time. If the new job needs less time to finish, the new job is less time to finish, the new job is started.started.

New, short jobs get good service New, short jobs get good service

Page 12: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Batch schedulingBatch scheduling

Three-level schedulingThree-level schedulingAdmission scheduler – chooses next job Admission scheduler – chooses next job

beginbeginMemory scheduler – which jobs are kept in Memory scheduler – which jobs are kept in

memory and which jobs are swapped to diskmemory and which jobs are swapped to diskHow long swapped in or out?How long swapped in or out?How much CPU time recently?How much CPU time recently?How big is the process?How big is the process?How important is the process?How important is the process?

Degree of multiprogramming – number of Degree of multiprogramming – number of processes in memoryprocesses in memory

CPU scheduler – picks which runs nextCPU scheduler – picks which runs next

Page 13: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Round-robin schedulingRound-robin schedulingPriority schedulingPriority schedulingMultiple queuesMultiple queuesShortest process nextShortest process nextGuaranteed schedulingGuaranteed schedulingLottery schedulingLottery schedulingFair-share schedulingFair-share scheduling

Page 14: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Round-robin schedulingRound-robin schedulingSimple, fair, widely used, preemptiveSimple, fair, widely used, preemptiveQuantum = time intervalQuantum = time interval

Process/context switch is expensiveProcess/context switch is expensiveToo small and we waste timeToo small and we waste timeToo large and interactive system will appear Too large and interactive system will appear

sluggishsluggish~20-50 msec is good~20-50 msec is good

Every process has equal priorityEvery process has equal priority

Page 15: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Priority schedulingPriority schedulingEach process is assigned a priority; Each process is assigned a priority;

process with highest priority is next to process with highest priority is next to run.run.

Types: static or dynamic (ex. I/O bound Types: static or dynamic (ex. I/O bound jobs get a boost)jobs get a boost)

Unix/Linux Unix/Linux nicenice command command

Page 16: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Ex. 4 priorities & RR w/in a priorityEx. 4 priorities & RR w/in a priority

Page 17: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Multiple queuesMultiple queuesDifferent typesDifferent typesEx. 4 queues for:Ex. 4 queues for:

Terminal, I/O, short quantum, and long Terminal, I/O, short quantum, and long quantumquantum

Page 18: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Shortest process nextShortest process nextShortest job first always produces min avg Shortest job first always produces min avg

response time (for batch systems)response time (for batch systems)How do we estimate this?How do we estimate this?

From recent behavior (of interactive From recent behavior (of interactive commands, Tcommands, Tii))

Example of aging (or IIR filter)Example of aging (or IIR filter)alpha near 1 implies little memoryalpha near 1 implies little memoryalpha near 0 implies much memoryalpha near 0 implies much memory

11 )1(ˆ iii TTT

Page 19: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Guaranteed schedulingGuaranteed schedulingGiven n processes, each process should Given n processes, each process should

get 1/n of the CPU timeget 1/n of the CPU timeSay we keep track of the actual CPU Say we keep track of the actual CPU

used vs. what we should receive used vs. what we should receive (entitled to/deserved).(entitled to/deserved).

K = actual / entitledK = actual / entitledK = 1 K = 1 we got what we deserved we got what we deservedK < 1 K < 1 we got less than deserved we got less than deservedK > 1 K > 1 we got more than deserved we got more than deserved

Pick process w/ min K to run nextPick process w/ min K to run next

Page 20: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Lottery schedulingLottery schedulingEach process gets tickets; # of tickets Each process gets tickets; # of tickets

can vary from process to process.can vary from process to process.If you ticket is chosen, you run next.If you ticket is chosen, you run next.

Highly responsive (new process might Highly responsive (new process might run right away)run right away)

Processes can cooperate (give each Processes can cooperate (give each other their tickets)other their tickets)

Page 21: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Interactive schedulingInteractive scheduling

Fair-share schedulingFair-share schedulingConsider who (user) owns the processConsider who (user) owns the processEx.Ex.

User A has 1 processUser A has 1 processUser B has 9 processesUser B has 9 processes

Should user A get 10% and user B get 90% Should user A get 10% and user B get 90% or should A get 50% and B get 50% (5.6% or should A get 50% and B get 50% (5.6% for each of the 9 processes)? Latter is fair-for each of the 9 processes)? Latter is fair-share.share.

Page 22: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Real time schedulingReal time scheduling

Time plays an essential roleTime plays an essential role Must react w/in a fixed amount of timeMust react w/in a fixed amount of time Categories:Categories:

Hard – absolute deadlines must be metHard – absolute deadlines must be met Soft – missing an occasional deadline is Soft – missing an occasional deadline is

tolerabletolerable Event types:Event types:

Periodic = occurring at regular intervalsPeriodic = occurring at regular intervals Aperiodic = occurring unpredictablyAperiodic = occurring unpredictably

Algorithm types:Algorithm types: Static (before the system starts running)Static (before the system starts running) Dynamic (scheduling decisions at run time)Dynamic (scheduling decisions at run time)

Page 23: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Real time schedulingReal time scheduling

Given m Given m periodicperiodic events.events.

Event i occurs w/ Event i occurs w/ period Pperiod Pii and and requires Crequires Cii seconds seconds of CPU timeof CPU time

SchedulableSchedulable iff: iff:

(basically normalizing C by P)(basically normalizing C by P)1

1

m

i i

i

P

C

Page 24: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Schedulable exampleSchedulable example

Given PGiven Pii = 100, = 100, 200, and 500 msec200, and 500 msec

Given CGiven Cii = 50, 30, = 50, 30, 100 msec100 msec

Can we handle Can we handle another event w/ another event w/ PP44=1 sec?=1 sec? Yes, as long as Yes, as long as

CC44<=150 msec<=150 msec

185.02.015.05.0500

100

200

30

100

50

11000

?

500

100

200

30

100

50

11000

150

500

100

200

30

100

50

Page 25: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Thread schedulingThread scheduling

User level threadsUser level threads No clock interrupts (per thread)No clock interrupts (per thread) A compute bound thread will dominate its A compute bound thread will dominate its

process but not the CPUprocess but not the CPU A thread can yield to other threads within the A thread can yield to other threads within the

same processsame process Typically round robin or priorityTypically round robin or priority

+ Context switch from thread to thread is + Context switch from thread to thread is simplersimpler

+ App specific thread scheduler can be used+ App specific thread scheduler can be used

- If a thread blocks on I/O, the entire process (all - If a thread blocks on I/O, the entire process (all threads) blockthreads) block

Page 26: 2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready

Thread schedulingThread scheduling

Kernel level threadsKernel level threads- Context switch from thread to thread is - Context switch from thread to thread is

expensive (but scheduler can make expensive (but scheduler can make more informed choices)more informed choices)

+ A thread, blocking on I/O, doesn’t block + A thread, blocking on I/O, doesn’t block all other threads in processall other threads in process