P3L1: Scheduling Flashcards
What is the role of the CPU scheduler?
The CPU scheduler decides how and when processes and threads access the CPU, i.e., how and when tasks are selected from the ready queue to run on the CPU.
Does the CPU scheduler schedule user tasks, kernel tasks, or both?
Both
What are four reasons why a task might be added to the ready queue?
- An I/O operation the task has been waiting on has completed
- The task has been woken from a wait
- The task was just created, i.e., a new thread is created by forking
- The task was ready, ran on the CPU until its timeslice expired, then was interrupted and put back on ready queue
When is the CPU scheduler run?
Whenever the CPU becomes idle. This may be because a task completed, or was interrupted.
What’s another name for the ready queue?
Run queue
What happens when the CPU scheduler selects a task to be scheduled?
The task is dispatched to the CPU. This entails:
- Context switch, entering the context of new task
- Enter user mode
- Set the program counter
- Go!
What are four good metrics for comparing CPU schedulers?
- Throughput (# of processes executed)
- Average job completion time
- Average job wait time
- CPU utilization (% of CPU resources used)
For First-Come-First-Serve scheduling, what data structure should you use for the runqueue?
FIFO queue
For Shortest-Job-First scheduling, what data structure should you use for the runqueue?
Ordered queue or a tree
How do you calculate throughput?
jobs completed / time_to_complete_all_jobs
How do you calculate average completion time?
sum_of_times_to_complete_each_job / jobs_completed
How do you calculate average wait time?
sum_of_all_wait_times / jobs_completed
Wait time is the time from when process arrives until it begins executing
For Shortest-Job-First scheduling, we need to estimate how long a job will take. What are two heuristics we might use?
- How long did the job take last time?
2. How long did the job take, on average, over some previous window of time (say, the last n times the job ran)?
What is “Run to Completion” scheduling?
Simply when jobs run to completion, i.e., they cannot be interrupted/preempted by the scheduler
What is Preemptive scheduling?
When the scheduler can interrupt/preempt processes to run a different processes instead.
For example, a long process arrives first and starts executing. A shorter process arrives later. A “Shortest Job First” scheduler will preempt the long process and run the short process.
What is Priority Scheduling?
Scheduling based on some priority level. OS tasks may be given higher priority than user tasks, or interactive user tasks may be given higher priority than background tasks.
What are three data structures we could use to implement a runqueue when using a Priority Scheduler?
- Ordered queue
- Tree
- Multiple queues, with each queue assigned a priority level.
What is a danger of Priority Scheduling?
Starvation: Low priority tasks may never run
How do we protect against starvation when using a Priority Scheduler?
Priority aging: As tasks wait to execute, their priority levels increase. In this case, priority is a function of both the primary priority measure and the age of the task.
What is Priority Inversion?
Priority Inversion happens when a low priority job locks a mutex that is needed by a higher priority task, but is preempted by other tasks of intermediate priority.
Say we have P1, P2, and P3 with priorities ordered like P3 < P2 < P1. Also assume P1 and P3 need the same lock to complete.
If P3 arrives first and gets the lock, then is preempted by P2 arriving, P3 doesn’t get to complete. But it still has the lock! So when P1 arrives and tries to get the lock, it’s blocked. So P2 completes, followed by P3. Only then does P1 get the lock and run.
So we wind up with jobs completing in the order P2, P3, P1 (when we would have liked them to complete in order of priority, P1, P2, P3).
How do we prevent Priority Inversion?
We can prevent priority inversion by temporarily boosting the priority of mutex owner so that it doesn’t get preempted. This allows it to complete earlier and allows any higher priority jobs that need the mutex to also complete earlier.
Describe three variants of Round Robin Scheduling
- In all Round Robin Scheduling systems the process must be allowed to yield the CPU rather than being required to run to completion.
1. Vanilla Round Robin: Tasks are scheduled first come first serve, but they don’t have to complete before yielding. They may instead yield when waiting, for example, on an I/O operation.
2. Round Robin with Priority: Tasks are scheduled first come first serve unless a higher priority task arrives, in which case that is scheduled. This includes preemption.
3. Round Robin with Interleaving: We give each task a timeslice in which it is allowed to run. When it reaches the end of the timeslice, it is interrupted and the next tasks starts.
What is a timeslice?
Maximum amount of uninterrupted time given to a task (on the CPU). Also called “time quantum.”
What do timeslices allow us to achieve?
Timeslices allow us to interleave CPU-bound tasks.
I/O-bound tasks are always giving up the CPU anyway, but for CPU-bound tasks we need the timeslice mechanism to interleave.