P3L1. Scheduling Flashcards
Name three ways an OS scheduler could choose to schedule tasks and the benefit of each.
- Assign tasks immediately
- Benefit: Keeps scheduling simple
- Algorithm: First Come First Serve
- Assign simple tasks first
- Benefit: Mazimize throughtput
- Algorithm: Shortest Job First
- Assign complex tasks first
- Benefit: Maximize all aspects of the platform (utilization of CPU, devices, memory, etc)
- Algorithm: ?
What does a CPU scheduler do?
- Decides how and when processes (and their threads) access shared CPUs.
- Schedules tasks running user-level processes/threads as well as kernel-level threads.
When does the CPU scheduler run?
The CPU scheduler runs when:
- CPU becomes idle
- a new task becomes ready
- timeslice expired timeout
What happens once the scheduler selects a thread to be scheduled onto the CPU?
The thread is dispatched onto the CPU:
- perform context switch
- enter user mode
- set program counter
- new thread starts executing
How does the CPU scheduler choose which task should be run next? And how is this done?
Which task should be selected depends on the scheduling policy/algorithm.
How this is done depends on the runqueue data structure.
The runqueue data structure and the scheduling algorithm are tightly coupled. e.g., certain scheduling policies demand a certain runqueue data structure.
What is “run-to-completion” scheduling?
This type of scheduling assumes that as soon as a task is assigned to a CPU it will run until it completes.
What is First-Come First-Service (FCFS)?
What kind of runqueue data structure is needed to support it?
Pros/Cons?
Tasks are scheduled in the order of arrival.
To make a decision all the scheduler needs to know the head of the queue and how to dequeue tasks, so a FIFO queue would make a good runqueue data structure.
Pros:
- Simple
Cons:
- Wait time is poor even if there’s only one long task in the system that has arrived in front of other shorter tasks.
Say we have a First-Come First-Servce (FCFS) policy with run-to-completion scheduling, and tasks T1, T2, and T2, which take 1 second, 10 seconds, and 1 second to run.
What is the throughput?
What is the average completion time?
What is the average wait time?
Throughput
3 tasks / 12 seconds = 0.25 tasks / second
Average Completion Time
(1 + 11 + 12) / 3 = 8 seconds
T1 completes after 1 second, T2 after 11 seconds, T3 after 12 seconds
Average Wait Time
(0 + 1 + 11) / 3 = 4 seconds
What is Shortest Job First (SJF)?
What kind of runqueue data structure is needed to support it?
Pros/Cons?
Schedules tasks in order of their execution time.
The scheduler needs to know the tail of the queue, and also how to insert tasks in a specific order. We could use an ordered queue data structure or a tree where the leftmost node is always the shortest execution time.
Pros:
- TODO
Cons:
- TODO
What is the formula for throughput?
jobs completed / time to complete all jobs
What is the formula for average completion time?
sum of times to complete each job / jobs completed
What is the formula for average wait time?
sum of time each job spent waiting / jobs completed
What is preemptive scheduling?
This type of scheduling assumes that when a task is assigned to a CPU it can be interrupted so that another task can be scheduled.
When scheduling jobs with the Shortest Job First (SJF) policy, how can the scheduler determine a task’s execution time?
The scheduler can use heuristics based on history of job running time.
- how long did a task run last time?
- how long did a task run last n times? (windowed average)
What is Priority scheduling?
What kind of runqueue data structure is needed to support it?
Pros/Cons?
Tasks have different priority levels. The scheduler will run the highest priority task next.
We can support this by having a queue per priority (the scheduler just selects the next job off the appropriate queue) or we can use a tree ordered based on priority.
Pros:
- TODO
Cons:
- Starvation: low priority task stuck in runqueue. We can protect against this with “priority aging” where priority is a function of the task’s actual priority and the time spent in the run queue.