2.5 Scheduling 2.5 Scheduling
2.5 Scheduling2.5 Scheduling
2.5 Scheduling2.5 Scheduling
Given a multiprogramming system, Given a multiprogramming system, there are many times when more than there are many times when more than 1 process is waiting for the CPU (in the 1 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.
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, MMU (Note: Pipeline Registers, invalidate cache, MMU (Note: Pipeline
becomes useless.)becomes useless.)
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
Process Process behaviobehavio
rr
Types:Types:1.1. I/O bound – spend most time I/O bound – spend most time
performing I/Operforming I/O
2.2. Compute bound – spend most time Compute bound – spend most time performing computationperforming computation
3.3. MixtureMixture
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
Scheduling environmentsScheduling environments
1.1. BatchBatch
2.2. InteractiveInteractive
3.3. Real timeReal time
Scheduling algorithm goalsScheduling algorithm goals(for all systems)(for all systems)
Scheduling algorithm goalsScheduling algorithm goals(for batch systems)(for batch systems)
Scheduling algorithm goalsScheduling algorithm goals(for interactive systems)(for interactive systems)
Scheduling algorithm goalsScheduling algorithm goals(for real-time systems)(for real-time systems)
Concepts & definitionsConcepts & definitions
Throughput = # of jobs completed per Throughput = # of jobs completed per hourhour
Concepts & definitionsConcepts & definitions
Turnaround time = avg of “start (submit) Turnaround time = avg of “start (submit) to completion” times; avg wait timeto completion” times; avg wait time
Concepts & definitionsConcepts & definitions
CPU utilization = avg of CPU busynessCPU utilization = avg of CPU busyness
Concepts & definitionsConcepts & definitions
Response time = time between issuing a Response time = time between issuing a command and getting the resultcommand and getting the result
Concepts & definitionsConcepts & definitions
Proportionality = user perception that Proportionality = user perception that “complex” things take a long time and “complex” things take a long time and that is fine, but “simple” things must be that is fine, but “simple” things must be quickquick
Concepts & definitionsConcepts & definitions
Predictability = regularity, especially Predictability = regularity, especially important for audio and video streamingimportant for audio and video streaming
BATCH SCHEDULINGBATCH SCHEDULING
Batch schedulingBatch scheduling
1.1. First-come first-servedFirst-come first-served
2.2. Shortest job firstShortest job first
3.3. Shortest remaining time nextShortest remaining time next
4.4. Three-level schedulingThree-level scheduling
Batch schedulingBatch scheduling
1. First-come first-served1. First-come first-servedSimpleSimple
Non preemptiveNon preemptive
Process runs until it either blocks on I/O Process runs until it either blocks on I/O or finishesor finishes
Batch schedulingBatch scheduling
2. Shortest job first2. Shortest job firstOptimal turnaround time (when all start Optimal turnaround time (when all start
together)together)
Requires that we know run times Requires that we know run times a prioria priori
Batch schedulingBatch scheduling
3. Shortest remaining time next3. Shortest 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
Batch schedulingBatch scheduling
4. Three-level scheduling4. Three-level scheduling1.1. Admission scheduler – chooses next job Admission scheduler – chooses next job
beginbegin2.2. Memory scheduler – which jobs are kept in Memory scheduler – which jobs are kept in
memory and which jobs are swapped to memory and which jobs are swapped to diskdiskHow 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?
3.3. CPU scheduler – picks which runs nextCPU scheduler – picks which runs next Degree of multiprogramming – number of Degree of multiprogramming – number of
processes in memoryprocesses in memory
INTERACTIVE INTERACTIVE SCHEDULINGSCHEDULING
Interactive schedulingInteractive scheduling
1.1. Round-robin schedulingRound-robin scheduling
2.2. Priority schedulingPriority scheduling
3.3. Multiple queuesMultiple queues
4.4. Shortest process nextShortest process next
5.5. Guaranteed schedulingGuaranteed scheduling
6.6. Lottery schedulingLottery scheduling
7.7. Fair-share schedulingFair-share scheduling
Interactive schedulingInteractive scheduling
1. Round-robin scheduling1. Round-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
Interactive schedulingInteractive scheduling
2. Priority scheduling2. Priority 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:Types:1.1. staticstatic
2.2. dynamic (ex. I/O bound jobs get a boost)dynamic (ex. I/O bound jobs get a boost)
Unix/Linux Unix/Linux nicenice command command
Interactive schedulingInteractive scheduling
Ex. 4 priorities & RR w/in a priorityEx. 4 priorities & RR w/in a priority
Interactive schedulingInteractive scheduling
3. Multiple queues3. Multiple queuesdifferent Q’s for different types of jobsdifferent Q’s for different types of jobs
Ex. 4 queues for:Ex. 4 queues for:Terminal, I/O, short quantum, and long Terminal, I/O, short quantum, and long
quantumquantum
Interactive schedulingInteractive scheduling
4. Shortest process next4. Shortest process nextShortest job first always produces min Shortest job first always produces min
avg response time (for batch systems)avg response time (for batch systems)How do we estimate this (for interactive How do we estimate this (for interactive
jobs)?jobs)?
Interactive schedulingInteractive scheduling
4. Shortest process next4. Shortest 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 (for interactive How do we estimate this (for interactive
jobs)?jobs)?From recent behavior (of interactive commands, TFrom recent behavior (of interactive commands, T ii))Example of aging (or IIR filter)Example of aging (or IIR filter)
alpha near 1 implies little memoryalpha near 1 implies little memory alpha near 0 implies much memoryalpha near 0 implies much memory
11 )1(ˆ iii TTT
Interactive schedulingInteractive scheduling
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
How can we “increase” the “memory?”How can we “increase” the “memory?”
11 )1(ˆ iii TTT
Interactive schedulingInteractive scheduling
5. Guaranteed scheduling5. Guaranteed 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
Interactive schedulingInteractive scheduling
6. Lottery scheduling6. Lottery 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 your ticket is chosen, you run next.If your 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)
Interactive schedulingInteractive scheduling
7. Fair-share scheduling7. Fair-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)?for each of the 9 processes)?Latter is fair-share.Latter is fair-share.
REAL TIME SCHEDULINGREAL TIME SCHEDULING
Real time schedulingReal time scheduling
Time plays an essential role.Time plays an essential role.
System must react w/in a fixed amount of System must react w/in a fixed amount of time.time.
Real time schedulingReal time scheduling
Categories:Categories:1.1. Hard – absolute deadlines must be metHard – absolute deadlines must be met
2.2. Soft – missing an occasional deadline is Soft – missing an occasional deadline is tolerabletolerable
Real time schedulingReal time scheduling
Event types:Event types:1.1. Periodic = occurring at regular intervalsPeriodic = occurring at regular intervals
2.2. Aperiodic = occurring unpredictablyAperiodic = occurring unpredictably
Real time schedulingReal time scheduling
Algorithm types:Algorithm types:1.1. Static (before the system starts running)Static (before the system starts running)
2.2. Dynamic (scheduling decisions at run time)Dynamic (scheduling decisions at run time)
Real time schedulingReal time scheduling
Given m Given m periodicperiodic events. events.
Event i occurs w/ period PEvent i occurs w/ period Pii, and requires C, and requires Cii seconds of CPU time.seconds of CPU time.
SchedulableSchedulable iff: iff:
11
m
i i
i
P
CWe’re basically normalizing C (event length) by P (event frequency).
11
m
i i
i
P
CSchedulable exampleSchedulable example
Given periods PGiven periods Pii = = 100, 200, and 500 100, 200, and 500 msec and CPU msec and CPU time requirements time requirements of Cof Cii = 50, 30, 100 = 50, 30, 100 msecmsec
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
THREADS & THREAD THREADS & THREAD SCHEDULINGSCHEDULING
Thread scheduling types:Thread scheduling types:
1.1. User levelUser level
2.2. Kernel levelKernel level
Thread schedulingThread scheduling
1. User level threads1. User 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
Tradeoffs:Tradeoffs:
+ Context switch from thread to thread is simpler+ Context switch from thread to thread is simpler
+ 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
Thread schedulingThread scheduling
2. Kernel level threads2. Kernel level threadsThreads scheduled like processes.Threads scheduled like processes.
Tradeoffs:Tradeoffs:
- 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 + A thread, blocking on I/O, doesn’t block all other threads in process.block all other threads in process.