480 likes | 607 Views
This lecture covers key scheduling algorithms used in operating systems, including First-Come, First-Served (FCFS), Round-Robin, Shortest-Job First (SJF), Multilevel Feedback Queues (MLFQ), and Lottery Scheduling. We discuss the importance of scheduling in minimizing response time and maximizing throughput while considering fairness. Round-Robin is emphasized with examples illustrating its behavior, quantum effect, and its comparison to FCFS. We also explore how MLFQ adapts to job behavior and how Lottery Scheduling approximates SJF by distributing CPU time based on ticket allocation.
E N D
Operating SystemsCMPSCI 377Lecture 6: Scheduling Emery Berger University of Massachusetts, Amherst
Last Time: Threads & Scheduling • Thread = execution stream within process • User-level, kernel-level, hybrid • There’s no perfect scheduling algorithm! • Competing goals: • Minimize response time • Maximize throughput • Fairness • Policy decision
This Time: Scheduling Algorithms • FCFS • First-Come, First-Served • Round-robin • SJF • Multilevel Feedback Queues • Lottery Scheduling
Round-Robin Scheduling • Quantum expires: move to back of queue • Variants used in most real systems • Quantum length – • Large: response time increases • quantum )1 = FCFS • Small: throughput decreases • quantum ) 0 = overhead dominates • context switches, cache misses
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin
waiting running Example: Round-Robin • Fair • Long average wait times
Round-Robin vs. FCFS • Example 1: • 5 jobs, 100 seconds each, quantum = 1s • ignore context switch time
Round-Robin vs. FCFS • Example 1: • 5 jobs, 100 seconds each, quantum = 1s • ignore context switch time
Round-Robin vs. FCFS • Example 2: • 5 jobs: 50, 40, 30, 20, 10 seconds each
Round-Robin vs. FCFS • Example 2: • 5 jobs: 50, 40, 30, 20, 10 seconds each
This Time: Scheduling Algorithms • FCFS • First-Come, First-Served • Round-robin • SJF • Multilevel Feedback Queues • Lottery Scheduling
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
0 10 30 60 100 150 Example: SJF • Schedule job with least work until I/O or done 10 20 30 40 50
Example: SJF • 5 jobs, length 50, 40, 30, 20, 10 seconds
Example: SJF • 5 jobs, length 50, 40, 30, 20, 10 seconds
SJF/SRTF: Shortest-Job First • Advantages: • Optimal – minimizes average waiting time • Works for preemptive & non-preemptive schedulers • Preemptive SJF = SRTF • Shortest remaining time first • I/O-bound jobs get priority over CPU-bound jobs • Disadvantages: • Impossible to predict CPU time job has left • Long-running CPU-bound jobs can starve
This Time: Scheduling Algorithms • FCFS • First-Come, First-Served • Round-robin • SJF • Multilevel Feedback Queues • Lottery Scheduling
Multilevel Feedback Queues (MLFQ) • Use past behavior to predict future! • Overcome prediction problem in SJF • Assumption: • I/O-bound in past, I/O-bound in future • Scheduler favors jobs that use least CPU time • Adaptive: • Change in behavior ) change in scheduling decisions
MLFQ: Approximating SJF • Multiple queues, different priorities • Round-robin scheduling at each priority level • Run all at highest priority first, then next, etc. • Can lead to starvation • Increase quantum exponentially at lower priorities
MLFQ: Assigning Priorities • Job starts in highest priority queue • Quantum expires ) CPU-bound • Drop priority one level • Quantum does not expire ) I/O-bound • Increase priority one level • CPU-bound jobs move down,I/O-bound jobs move up
Improving Fairness • SJF: optimal, but unfair • Increase fairness = give long jobs CPU time • degrades average waiting time • Solutions: • Each queue – fraction of CPU time • Fair iff even distribution of jobs among queues • Adjust priority of jobs w/o service • Originally done by UNIX • Avoids starvation • Under load, waiting time suffers
This Time: Scheduling Algorithms • FCFS • First-Come, First-Served • Round-robin • SJF • Multilevel Feedback Queues • Lottery Scheduling
Lottery Scheduling • Every job gets lottery tickets • Each quantum: randomly pick winner • On average:CPU time proportional to # of tickets • Give most tickets to short-running jobs (approximates SJF) • Give every job at least one ticket • Degrades gracefully as load changes
Example: Lottery Scheduling • Paying customers: 40%, guests: 60% • 2:1 ticket ratio 2 1 1 1 1 1 1 2
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Paying customers: 40%, guests: 60% • 2:1 ticket ratio
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 2/5=40%
Example: Lottery Scheduling 2 1 1 1 1 1 1 2 • Probabilistically achieves desired proportions 3/5=60% 2/5=40%
Summary of Scheduling Algorithms • FCFS: • unfair, average waiting time poor • Round robin: • fair, average waiting time poor • SJF: • unfair, minimizes average waiting time • requires accurate prediction • Multilevel Feedback Queueing: • approximates SJF • Lottery scheduling: • fair, low average waiting time • poor fit to priority
Next Time • Synchronization