CS 519 Advanced Operating Systems More on scheduler

  • Slides: 32
Download presentation
CS 519 Advanced Operating Systems More on scheduler Yeongjin Jang 02/14/19

CS 519 Advanced Operating Systems More on scheduler Yeongjin Jang 02/14/19

Recap: Basic Schedulers • FIFO (First-In-First-Out) • Non-preemptive • SJF (Shortest Job First) •

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

JOB 1 (10) FIFO Scheduler JOB 2(10) Arrival seq: 1, 2, 3, 4, 5

JOB 1 (10) FIFO Scheduler JOB 2(10) Arrival seq: 1, 2, 3, 4, 5 JOB 3 (2) JOB 1 (10) JOB 4 (2) JOB 2 (10) JOB 3 (2) JOB 1 waits 0 seconds JOB 2 waits 10 seconds JOB 3 waits 20 seconds JOB 4 waits 22 seconds JOB 5 waits 24 seconds AVG: 15. 2 seconds of wait time JOB 4 (2) JOB 5 (2)

JOB 1 (10) What if we schedule like. . JOB 2(10) Arrival seq: 3,

JOB 1 (10) What if we schedule like. . JOB 2(10) Arrival seq: 3, 4, 5, 1, 2 JOB 3 (2) JOB 4 (2) JOB 5 (2) JOB 3 waits 0 seconds JOB 2 waits 2 seconds JOB 3 waits 4 seconds JOB 4 waits 6 seconds JOB 5 waits 16 seconds AVG: 5. 6 seconds of wait time JOB 1 (10) JOB 2 (10) 5. 6 vs 15. 2 JOB 4 (2) JOB 5 (2)

Problems: Starvation • I am a job that requires 10 seconds to finish. Let

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… J (1) JOB 1 (10) J (1) J J J (1) (1) JOB 1 (10) 100…. J (1)

Problems: Starvation • After finishing 99 of such small jobs, • Another one hudreds

Problems: Starvation • After finishing 99 of such small jobs, • Another one hudreds of 1 second job has come… J (1) JOB 1 (10) J (1) J J J (1) (1) JOB 1 (10) 100…. J (1)

JOB 1 (10) Round-Robin Example JOB 2(10) J 1 J 2 J 3 JOB

JOB 1 (10) Round-Robin Example JOB 2(10) J 1 J 2 J 3 JOB 3 (2) J 4 J 5 J 1 J 2 J 3 Fin J 4 Fin J 5 Fin J 1 J 2 JOB 4 (2) JOB 5 (2)

Summary • FIFO • Pros: fair scheduling • Cons: high average wait time; not

Summary • FIFO • Pros: fair scheduling • Cons: high average wait time; not responsive • SJF • Pros: low average wait time • Cons: unfair scheduling, starvation happens • RR • Pros: fair and responsive • Cons: high average wait time

Priority Scheduling • Choose the next job by priority • Important jobs gets a

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

(length, priority) JOB 1 (10, 4) Priority Scheduling Ex. JOB 2(10, 2) JOB 3

(length, priority) JOB 1 (10, 4) Priority Scheduling Ex. JOB 2(10, 2) JOB 3 (2, 5) JOB 4 (2, 1) JOB 2(10, 2) JOB 5 (2, 3) JOB 1 (10, 4) JOB 3 (2, 5) JOB 5 (2, 3)

Starvation in Priority Scheduling (10, 4) (3, 1) (3, 1) Add two more jobs…

Starvation in Priority Scheduling (10, 4) (3, 1) (3, 1) Add two more jobs…

Starvation in Priority Scheduling (10, 4) (3, 1) (3, 1) Add two more jobs…

Starvation in Priority Scheduling (10, 4) (3, 1) (3, 1) Add two more jobs… (3, 1) (10, 4)

Aging in Preemptive Priority Scheduling • Increase priority as a function of waiting time

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

Aging in Priority Scheduling (10, 3) (3, 1)

Aging in Priority Scheduling (10, 3) (3, 1)

Aging in Priority Scheduling After 1 time quantum, Run gray… (10, 3) (10, 4)

Aging in Priority Scheduling After 1 time quantum, Run gray… (10, 3) (10, 4) (2, 2) (3, 1) Execution (3, 1)

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1)

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1) After 1 time, Run green… (3, 1) (2, 2) Execution (10, 4) (10, 3) (10, 2)

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1)

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1) After 1 time, Run green… (3, 1) (2, 2) (2, 1) (1, 2) (10, 4) (10, 3) (10, 1) (10, 2) Execution

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1)

Aging in Priority Scheduling After 1 time quantum, Run gray… (2, 2) (3, 1) After 1 time, Run green… (3, 1) (2, 2) (2, 1) Execution (2, 1) (1, 2) (10, 1) (2, 1) (1, 1) (9, 2) Priority value gives a better opportunity for scheduling but low priority jobs can get scheduled…

Priority Scheduling in Practice • Choose the next job by priority • -20 is

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? • Priority represented as the length of a job

Summary • FIFO • Pros: fair scheduling • Cons: high average wait time; not

Summary • FIFO • Pros: fair scheduling • Cons: high average wait time; not responsive • SJF (or priority) • Pros: low average wait time (* priority weight) • Cons: unfair scheduling, starvation happens • RR • Pros: fair and responsive • Cons: high average wait time

Multi-level Queue: Mix-and-Match • Have multiple queues of jobs • Each queue is assigned

Multi-level Queue: Mix-and-Match • Have multiple queues of jobs • Each queue is assigned for a priority • E. g. , Realtime, high, above 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) • Move jobs among queues • Changing priority (aging? ) • Priority inversion?

Priority Inversion • Suppose two tasks, J 1 and J 2 are depending each

Priority Inversion • Suppose two tasks, J 1 and J 2 are depending each other • What will happen if high-priority job waits for a low-priority job? • J 1, priority 1, waits for J 2 to parse and send raw data to do some calculation • High priority • J 2, priority 5, waits to be scheduled • Low priority

Priority Inversion • Tasks • J 1 (P 1) waits for J 2 (P

Priority Inversion • Tasks • J 1 (P 1) waits for J 2 (P 5) • What will happen if we use a simple priority scheduler? • J 1 waits until J 2 finishes (to get data) • J 2 waits until J 1 finishes (to get scheduled) • Deadlock by Starvation…

Inheriting Priority • Tasks • J 1 (P 1) waits for J 2 (P

Inheriting Priority • Tasks • J 1 (P 1) waits for J 2 (P 5) • J 2 inherits the priority of J 1 • J 1 (P 1) and J 2 (P 1) • Eventually J 2 will get scheduled and resolve the wait dependency

A Real Example: Mars Pathfinder • Landed on Mars in July 4 th, 1997

A Real Example: Mars Pathfinder • Landed on Mars in July 4 th, 1997 • Send information gathered by the rover • Runs with Vx. Works, priority scheduler • Real-time OS • Failure to meet deadline result in a fault. . • 3 tasks • Meteorological data thread – Low • Communication thread, long – Medium • Bus thread, short, real-time – High

Mars Pathfinder Failure • Bus resource contention between Low/High priority jobs • High priority

Mars Pathfinder Failure • Bus resource contention between Low/High priority jobs • High priority job waits for low priority 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

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

Multi-level Feedback Queue (MLFQ) • Set of Queues with priority • Use RR for

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 • If a job releases the execution before a quantum finishes (via yield()), • It retains the same priority (could be problematic).

Multi-level Feedback Queue (MLFQ) Image from: https: //www. cs. ucr. edu/~csong/cs 153/l/sched 2. pdf

Multi-level Feedback Queue (MLFQ) Image from: https: //www. cs. ucr. edu/~csong/cs 153/l/sched 2. pdf

MLFQ: Starvation • Still suffers starvation • Gets many of jobs with priority 1…

MLFQ: Starvation • Still suffers starvation • Gets many of 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 execution time than quantum…

Canonical UNIX Scheduler • MLFQ with ~170 priority levels over 3~4 classes • •

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

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. .