CS444/544 Operating Systems II Scheduler 2 Yeongjin Jang 5/30/19
CS444/544Operating Systems II
Scheduler 2Yeongjin Jang
5/30/19
Grading Scheme
• A : >= 93A-: >= 85B+: >= 80B : >= 75B-: >= 70C+: >= 65C : >= 63C-: >= 60D+: >= 55D : >= 53D-: >= 50F : < 50
• Brackets could go down based on the final exam result
• Graduate students (544) will have a higher grading bracket than undergraduate students (444)
• We will not do paper summary for 544 students• 50% for the JOS lab1,2, and 3 and concurrency lab 1 and 2
Final Exam
• 6/12 12:00 pm• 110 minutes• At here (WITH 109)
• Please study the entire things from lecture, lab, and read textbook• Important parts of textbook is linked in the schedule page
• We will do review for the final on 6/6
Recap: Scheduler
• An algorithm that decides which task to run
• Why do we have a scheduler?• We usually have more processes than # of cores• Some job waits for I/O event; running other jobs in the meantime is an
efficient decision
JOB 1 JOB 1Waiting for I/O ops
Recv()
JOB 2
A better CPU Utilization
Recv() returns!
Recap: Scheduler
• Types• Preemptive – OS can forcefully stop a task and switch to others• Non-preemptive – OS waits for a task to finish its job or yield its execution
• Requirements?• Fairness; do not cause starvation• Performance; too slow scheduling will spend more time on scheduling itself
than task execution
J1 J2 J3 J4 J5J1 J2 J3 J4 J5
Scheduler execution..
An efficient scheduler; requires constant time, andAnd its execution is shorter than job execution time slice
An inefficient scheduler;Runs in variable time, and scheduling isLonger than job exec. time slice.
Recap: Basic Schedulers
• FIFO (First-In-First-Out)• Non-preemptive
• SJF (Shortest Job First)• Preemptive version: SRTF (Shortest Remaining Time First)
• RR (Round-Robin)• Preemptive
FIFO Scheduler JOB 1 (10)
JOB 2(10)
JOB 3 (2)
JOB 4(2)
JOB 5(2)
Arrival seq: 1,2,3,4,5
JOB 1 (10)
JOB 2 (10)
JOB 3(2)
JOB 4(2)
JOB 5(2)
JOB 1 waits 0 secondsJOB 2 waits 10 secondsJOB 3 waits 20 secondsJOB 4 waits 22 secondsJOB 5 waits 24 seconds
AVG: 15.2 seconds of wait time
What if we schedule like.. JOB 1 (10)
JOB 2(10)
JOB 3 (2)
JOB 4(2)
JOB 5(2)
Arrival seq: 3,4,5,1,2
JOB 1 (10)
JOB 2 (10)
JOB 3(2)
JOB 4(2)
JOB 5(2)
JOB 3 waits 0 secondsJOB 2 waits 2 secondsJOB 3 waits 4 secondsJOB 4 waits 6 secondsJOB 5 waits 16 seconds
AVG: 5.6 seconds of wait time5.6 vs 15.2
Problems: Starvation
• I am a job that requires 10 seconds to finish. Let me be scheduled!
• Before its scheduling, one hundred of 1 second job has come…
JOB 1 (10)
J(1)
J(1)
J(1)
J(1)
J(1)
J(1)
J(1) J
(1) J(1) J
(1) J(1) J
(1)JOB 1 (10) 100….
Problems: Starvation
• After finishing 99 of such small jobs,• Another one hudreds of 1 second job has come…
JOB 1 (10)
J(1)
J(1)
J(1)
J(1)
J(1)
J(1)
J(1) J
(1) J(1) J
(1) J(1) J
(1)JOB 1 (10) 100….
Round-Robin Example JOB 1 (10)
JOB 2(10)
JOB 3 (2)
JOB 4(2)
JOB 5(2)J1
J2 J3 J4 J5J1
J2 J3J4 J5
Fin FinFin
J1J2
J1J2 J1
J2J1
J2
Summary
• FIFO (non-preemptive)• Pros: fair scheduling• Cons: high average wait time; not responsive
• SJF (non-preemptive)• Pros: low average wait time• Cons: unfair scheduling, starvation happens
• RR (preemptive)• Pros: fair and responsive• Cons: high average wait time
Priority Scheduling
• Choose the next job by priority• Important jobs gets a higher priority• Non-critical jobs gets a lower priority
• SJF?• Priority represented as the length of a job
Priority Scheduling Ex. JOB 1 (10, 4)
JOB 2(10, 2)
JOB 3 (2, 5)
JOB 4(2, 1)
JOB 5(2, 3)JOB 4
(2, 1)
JOB 2(10, 2)
JOB 5(2, 3)
JOB 1 (10, 4)
JOB 3 (2, 5)
(length, priority)
Legend: (job length, priority). Low number means a higher priority.
Starvation in Priority Scheduling
(10, 4)
(3, 1) (3, 1)
(3, 1) (3, 1)
(3, 1)
(3, 1)
(10, 4)
Add two more jobs…
Legend: (job length, priority). Low number means a higher priority.
Starvation in Priority Scheduling
(10, 4)
(3, 1) (3, 1)
(3, 1) (3, 1)
(3, 1)
(3, 1)
(10, 4)
Add two more jobs…
(3, 1)
(3, 1)
Legend: (job length, priority). Low number means a higher priority.
Aging in Preemptive Priority Scheduling• Increase priority as a function of waiting time• (10, 4) -> (10, 3) after waiting a quantum
• Decrease priority as a function of CPU time consumption• (3,1) -> (2,2) after running for a quantum
Legend: (job length, priority). Low number means a higher priority.
Aging in Priority Scheduling
(3, 1)
(10, 3)
(3, 1)
This mark means the current status
Aging in Priority Scheduling(10, 4)
(3, 1)(3, 1) (2, 2)
(10, 3)
(3, 1)
After 1 time quantum,Run gray…
Execution
Increase the priorityafter waiting
Highest priority; can’t change!Decrease the priorityAfter having an execution slice
This mark means the current status
Aging in Priority Scheduling
(10, 4)
(3, 1)
(3, 1) (2, 2)
(10, 3)
After 1 time quantum,Run gray…
After 1 time,Run green…
(2, 2)
(10, 2)
(2, 1)
Execution
This mark means the current status
Aging in Priority Scheduling
(10, 4)
(3, 1)
(3, 1) (2, 2)
(10, 3)
After 1 time quantum,Run gray…
After 1 time,Run green…
(2, 2)
(10, 2)
(2, 1)
Execution
(2, 1)
(2, 1) (1, 2)
(10, 1)This mark means the current status
Aging in Priority Scheduling
(3, 1)
(3, 1) (2, 2)
After 1 time quantum,Run gray…
After 1 time,Run green…
(2, 2)
(2, 1) Execution
(2, 1)
(2, 1) (1, 2)
(10, 1) (9, 2)
(2, 1)
(1, 1)
Priority value gives a better opportunity for scheduling but low priority jobs can get scheduled…
This mark means the current status
Priority Scheduling in Practice
• Choose the next job by priority• -20 is the highest priority, 19 is the lowest priority in Linux• Realtime, high, normal, etc. in Windows
• SJF (Shortest Job First)?• The length of a job is the priority
• FIFO (First-in-First-out)• Job arrival time is the priority
Priority Inversion
• Suppose two tasks, J1 and J2 are depending each other
• What will happen if a high-priority job waits for a low-priority job?• J1, priority 1, waits for J2• J2, priority 20, waits to be scheduled• J3, priority 2…
• A naïve priority scheduler will schedule• J3
• Job finishing sequence:• J3 -> J2 -> J1
J1 (3, 1) J2 (10, 20)
J3 (8, 2)
Waits for..
Priority Inversion
• Tasks• J1 (P1) waits for J2 (P20)
• What will happen if we use a simple priority scheduler?• J1 waits until J2 finishes (to get data)• J2 waits until J1 finishes (to get scheduled)• Many jobs whose priority is in between 1 and 20 come and go..
• J1 suffers starvation even if its priority is the highest..
J1 (3, 1) J2 (10, 20)
Waits for..
J3 (8, 2)
J6 (10, 2)J4 (2, 5)
J5(2, 3)
A Real Example: Mars Pathfinder
• Landed on Mars in July 4th, 1997• Send information gathered by the rover• To Earth!
• Runs with VxWorks• Real-time OS with a priority scheduler• Failure to meet deadline result in a fault..
• Programmed to reboot if such thing happens
• 3 tasks• Meteorological data thread – Low• Communication thread, long – Medium• Bus thread, short, real-time – High
Galactic Scale..
Avg. Distance: 140 million miles..
Avg. Latency: 12.5 minutes even with the lightspeed
Bandwidth: 4KB/sec…
Image from:https://marsmobile.jpl.nasa.gov/msl/mission/communicationwithearth/data/
$280MM project in 1997Took 7 months from launch to landing
Mars Pathfinder Failure
• Bus resource contention between Low/High priority jobs• A high priority job waits for a low priority job
Image from: http://wiki.csie.ncku.edu.tw/embedded/priority-inversion-on-Mars.pdf
Mars Pathfinder Failure
• Periodically runs a Medium priority job• Via interrupt; Low priority job gets preempted• Long execution time
• High priority job• Waits for low priority finishes..• Could not meet deadline..• Reboot!
• Read: https://www.rapitasystems.com/blog/what-really-happened-to-the-software-on-the-mars-pathfinder-spacecraft
Image from: http://wiki.csie.ncku.edu.tw/embedded/priority-inversion-on-Mars.pdf
Example: Time 1
J1 (3, 1) J2 (10, 20)
Waits for..
J3(2, 2)
Periodic MEDIUM job comes..
Scheduler runs J3!
Example: Time 2
J1 (3, 1) J2 (10, 20)
Waits for..
J3(1, 2)
Scheduler runs J3!
Example: Time 3
J1 (3, 1) J2 (10, 20)
Waits for..
J4(2, 2)
A new periodic MEDIUM job comes..
Scheduler runs J4!
This scenario continues, J1 never get scheduled..
Inheriting Priority
• Tasks• J1 (P1) waits for J2 (P20)
• J2 inherits the priority of J1• J1 (P1) and J2 (P1)• Eventually J2 will get scheduled and resolve the wait dependency
J1 (3, 1) J2 (10, 20)
Waits for..
J3 (8, 2)
J2 (10, 1)
J6 (10, 2)J4 (2, 5)
J5(2, 3)
Multi-level Queue: Mix-and-Match
• Have multiple queues of jobs• Each queue is assigned for a priority• E.g., Realtime, high, above normal, normal, below normal, and low
• Use different scheduling algorithms per each queue• Naïve design: use RR!• Real-time jobs? Use SDF (shortest deadline first)
• Even we can make them non-preemptive
• Move jobs among queues• Changing priority (aging?)• Priority inversion?
Multi-level Feedback Queue (MLFQ)
• Set of Queues with priority• Use RR for having fairness
• Higher priority queues will have a shorter quantum• Lower priority queues will have a longer quantum
• Jobs in higher priority queue runs first
• After a job consumes a quantum, its priority drops by 1 level (Aging)• If a job releases the execution before a quantum finishes (via yield()),• then it retains the same priority (this could be problematic).
Multi-level Feedback Queue (MLFQ)
Image from: https://www.cs.ucr.edu/~csong/cs153/l/sched2.pdf
MLFQ: Starvation
• Still suffers starvation• If we have many incoming jobs with priority 1…
• How can we solve this?• After a certain wait period (e.g., 50 quantum), set the priority of the job as 1
• Can a job play a game with the scheduler?• Do yield() right before a quantum finishes• Solution: measure the actual execution time than counting a full quantum…
Canonical UNIX Scheduler
• MLFQ with ~170 priority levels over 3~4 classes • Timesharing: first 60• System: next 40• Real-time: next 60• Interrupt: next 10 • (high number gets higher priority)
• Do priority scheduling among queues, and run RR within a queue• Dynamically adjusting priorities• Increase priority if execution stopped before consuming a quantum• Decrese the priority if execution consumed a whole quantum…
Idea Behind UNIX Scheduler
• Give rewards to interactive processes (responsiveness)• Interactive process requires getting more scheduling event but usually they
are short jobs (even does not consume a quantum)• Interrupts, I/O, etc.
• Delays CPU-bound jobs• Eventually the job get scheduled and run for a longer time..
How Modern OSes are doing?
Image from: https://en.wikipedia.org/wiki/Scheduling_(computing)
Linux
• Before 2.6.0• O(n) MLFQ scheduler
• 2.6.0 – 2.6.23• O(1) Scheduler
• After 2.6.23• O(log N) Completely Fair Scheduler
O(1) Scheduler
• MLFQ, but having 2 queues per each priority• Active Queue• Processes will be scheduled..
• Expired Queue• After running one time slice, processes
will be get into here• Tasks here will never be scheduled
• Switch queues if active queue is empty
O(1) Scheduler
• Problem was not in the queueing model
• Scheduler tries to identify interactive process• Intuition: interactive process requires more attention• Let’s identify them and give a higher priority to them!
• Also penalize non-interactive processes
• Identifying interactive processes by• Measuring average sleep time for user input• Complex calculation, often mis-identifies tasks..
Completely Fair Scheduler (CFS)
• Based on red-black tree• A self-balancing tree• Leftmost node will have the lowest value
• Store priority!• Schedule the leftmost node!
• How to set the priority?• Count ‘execution time’ of a task• Intuition: tasks who have executed more time will get a less chance