Lecture: Scheduling
- Problem: choosing which thread to run on the CPU next
- More generally: multiplex tasks onto limited resources
- Cloud service, limited machines, lots of requests
- Supermarket, limited cashiers, lots of customers
Definitions/Metrics
- Task: user request (mouse click, web request, shell command)
- Latency/Response time: user-perceived time to do a task
- Throughput: # of tasks done per unit of time
- Scheduling Overhead: time to switch from one task to another
- Fairness: how equal is the resource allocation
- Predictability: how consistent is the response time
- Starvation: lack of progress for one task (due to higher priority tasks)
Scheduling Mechanism
- Context switch
- Timer interrupt: preemption
Scheduling Policies
- First In First Out (FIFO)
- Complete one task at a time, in the order they come in
- Pros/cons?
- Average response time: best case? worst case?
- Shortest Job First (SJF) / Shortest Remaining Time First (SRTF)
- Schedule the task with the shortest remaining work to do
- If a shorter task arrives, preempts the current task and switches to the new task
- Pros/cons?
- Average response time: best case? worst case?
- Round Robin (RR)
- FIFO but each task only gets a fixed amount of time (time slice/quantum)
- How to pick the time quantum?
- Pros/cons?
- Average response time: best case? worst case?
- I/O bound vs CPU bound tasks
- I/O bound: task that relies on/bottlenecked by I/O, uses little CPU and yields while waiting for I/O
- CPU bound: task that relies on/bottlenecked by CPU, heavy on computation
- Multilevel Feedback Queue (MLFQ)
- Goal: good average response time, but also want to be interactive
- Multiple levels of RR queues, with increasing time quantum
- Task starts at the top queue with smallest quantum
- If task doesn't take up the whole time quantum, it moves up a queue (stays in the same queue if already at the top).
- Otherwise it moves down a queue, as more CPU times are needed
- Scheduler selects a task to run from the top queue in a RR fashion and works its way down
- Periodically, all tasks are moved to the top queue (priority boost)
- Completely Fair Scheduler (CFS)
- Ideal world: 2 processes each get 50% of CPU time, if one gets less, it should have priority to catch up to its fair time
- Each CPU has a time-ordered red-black tree (ordered, self-balancing binary tree)
- Time = time the task had been using the CPU
- Always picks the task with least amount of time executed (to help it catch up)
- As the task gains more CPU time, another tasks becomes the task with least amount of CPU time
- xk scheduler
- How often is the scheduler invoked?
kernel/trap.c
- What scheduling policy does it have?
kernel/proc.c