Scheduling
Overview
- scheduling: a policy that decides who should run on the CPU next
- kernel schedules processes/threads onto the CPU
- a process may perform a number of tasks/jobs (mouse click, web request, shell command)
- metrics for evaluting scheduling policies
- latency / turnaround time
- user-perceived time to finish a task (including time waiting)
- throughput
- the rate of task completion (# of tasks done per period of time)
- scheduling overhead
- time it takes to perform scheduling (execution of scheduling policy + context switch)
- fairness
- are tasks given similar times on the CPU & wait for similar amount of time
- starvation
- lack of progress for one task due to higher priority tasks
Scheduling Policies
- first in first out (FIFO)
- run each task to completion (or until it yields the CPU) in FIFO order
- no preemption! minimal scheduling overhead
- simple but high variance in latency based on the arrival ordering
- what happens when a task blocks? what happens when it unblocks?
- how well is FIFO according to the metrics?
- preemptive shortest job first (PSJF)
- schedule the shortest job first (assumes that we know how long each task needs the CPU)
- if a new shorter task arrives, preempts the current task and runs the new task
- what happens when a task blocks? what happens when it unblocks?
- how well is PSJF according to the metrics?
- round robin (RR)
- FIFO but preemptive: each task gets x amt of time before it's preempted
- what happens when the time quantum is too large? too small?
- what happens when a task blocks? what happens when it unblocks?
- how well is RR according to the metrics?
- no starvation but unfair wait time for I/O bound tasks compared to CPU bound tasks
- I/O bound: performance dependent on I/O
- runs 1ms on the CPU, blocks 10ms for I/O
- I/O bound jobs often block before the time slice expires (less time on the CPU)
- but have to wait for CPU bound tasks who use the full time slice (long wait time)
- CPU bound: performance dependent on CPU
- how might we fix this?
- prioritize tasks who spent less time on the CPU (CFS)
- make I/O bound tasks wait less (MLFQ)
- multilevel feedback queue (MLFQ)
- multiple RR queues with different time quantums
- queues with shorter quantum has higher scheduling priority
- tasks within a queue are schedueld in RR fashion
- scheduler starts at the highest priority queue and moves down as higher priroty queues become empty
- current task might be preempted if a higher priority queue has new tasks
- tasks from lower queues are periodically moved up to the top priority queue to avoid starvation
- how are tasks distributed to the queues?
- a task starts at the top queue (shortest quantum, highest scheduling priority)
- if a task uses up the quantum, it moves down a queue (longer quantum), as it needs more CPU
- otherwise it stays in the same queue or moves up a queue