260 likes | 480 Views
CS423UG Operating Systems . CPU Scheduling, Part Deux. Indranil Gupta Lecture 6 Sep 7, 2005. Content: Scheduling Algorithms. Batch systems First come First Serve (FCFS) Shortest job first Interactive systems Round-robin Priority scheduling
E N D
CS423UG Operating Systems CPU Scheduling, Part Deux Indranil Gupta Lecture 6 Sep 7, 2005
Content: Scheduling Algorithms • Batch systems • First come First Serve (FCFS) • Shortest job first • Interactive systems • Round-robin • Priority scheduling • Multi Queue & Multi-level Feedback • Shortest process time • Guaranteed Scheduling • Lottery Scheduling • Fair Sharing Scheduling CS 423UG - Operating Systems, Indranil Gupta
Review: Scheduling • Short-term: Deciding which process/thread should occupy the resource (CPU, disk, etc) • Preemptive vs. non-preemptive • Scheduling objectives • Scheduling performance metrics • First-come-first-serve (FCFS) • Process that enters ready queue FIRST is allocated the CPU FIRST • Used in batch systems CS 423UG - Operating Systems, Indranil Gupta
I. Shortest Job First (SJF) “Job”=Process • Schedule the job with the shortest running time first • Scheduling in Batch Systems • Non-preemptive only (for now) • Requirement: the running time of the process needs to be known in advance • SJF Achieves Optimal AWTif all jobs are available simultaneously (provable) • “Optimal AWT”=least possible avg. waiting time among all possible scheduling algorithms (includes, FCFS, priority, round robin, etc.)! CS 423UG - Operating Systems, Indranil Gupta
Non-preemptive SJF: Example P4 (3) P1 (6) P3 (7) P2 (8) 3 9 16 24 0 P4 waiting time: 0-0 P1 waiting time: 3-0 P3 waiting time: 9-0 P2 waiting time: 16-0 The total running time is: 24 The average waiting time (AWT): (0+3+9+16)/4 = 7 time units CS 423UG - Operating Systems, Indranil Gupta
Comparing to FCFS (Breaking ties by choosing process with lower ID) P1 (6) P2 (8) P3 (7) P4 (3) 14 21 24 0 6 The total time is the same. The average waiting time (AWT): (0+6+14+21)/4 = 10.25 time units (> SJF’s 7 time units) P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21 CS 423UG - Operating Systems, Indranil Gupta
Caveat: SJF may not be optimal when… …all the jobs are not available simultaneously P1 (10) P2 (2) 0 2 (p2 arrives) 10 12 The average waiting time (AWT): (0+8)/2 = 4 P1 waiting time: 0 P2 waiting time: 8 CS 423UG - Operating Systems, Indranil Gupta
What if the scheduler waits for 2 time units? (Do it yourself) P2 (2) P1 (10) 0 2 4 14 The average waiting time (AWT): (0+4)/2 = 2 Downside: Wastes 2 time units of CPU. How long to wait? P1 waiting time: 4 P2 waiting time: 0 CS 423UG - Operating Systems, Indranil Gupta
II. Preemptive SJF “Job”=Process • Also called Shortest Remaining Time First • Schedule the job that has the shortest remaining running time • Requirement: the total running time of a process needs to be known in advance CS 423UG - Operating Systems, Indranil Gupta
Preemptive SJF: Same Example P2 (2) P1 (2) P1 (8) 0 2 4 12 The average waiting time (AWT): (0+2)/2 = 1 No CPU time wasted!!! P1 waiting time: 4-2 =2 P2 waiting time: 0 CS 423UG - Operating Systems, Indranil Gupta
So, what’s the catch with SJF? • Starvation • SJF may cause some jobs to wait forever! • Example: SJF • Process A with running time of 1 hour arrives at time 0 • Then, every 2 minutes, a short process with running time of 2 minutes arrives • Process A never gets to run CS 423UG - Operating Systems, Indranil Gupta
Interactive Scheduling Algorithms • Usually preemptive • Time is sliced into quanta (plural of quantum) • Quantum=(fixed) time interval • Scheduling decision is made at start of each quantum • Performance Criteria • Min Response time • Best proportionality • Representative algorithms: • Priority-based • Round-robin • Multi Queue & Multi-level Feedback • Shortest process time • Guaranteed Scheduling • Lottery Scheduling • Fair Sharing Scheduling CS 423UG - Operating Systems, Indranil Gupta
III. Priority Scheduling • Each job is assigned a priority. • Priority could be based on user, urgency, interactivity, etc. • Scheduler • Select highest priority task that is currently ready • Caveats: • May not give the best AWT • Could cause starvation in some cases CS 423UG - Operating Systems, Indranil Gupta
Setting a Process’ Priority • Two approaches • Static (for system with well-known and regular application behaviors) • Dynamic • Priority may be based on: • Cost to user • Importance of user • Aging • Percentage of CPU time used in last X hours. • Ex. priorities: DVD player > sendmail; Senior’s job > Junior’s job CS 423UG - Operating Systems, Indranil Gupta
Priority Scheduling: Example Lower priority #== more important P2 (8) P4 (3) P3 (7) P1 (6) 24 0 8 11 18 P2 waiting time: 0 P4 waiting time: 8 P3 waiting time: 11 P1 waiting time: 18 The average waiting time (AWT): (0+8+11+18)/4 = 9.25 (worse than SJF’s) CS 423UG - Operating Systems, Indranil Gupta
IV. Round-Robin • Simple, popular, one of the oldest scheduling strategies • Idea: • Select process from head of queue • Run it for 1 quantum • Put process back at tail of queue • Repeat • Advantages: • Fair • Avoids starvation • Problems: • Does not consider priority • High Context switch overhead CS 423UG - Operating Systems, Indranil Gupta
Round-robin: Example Suppose time quantum is: 1 unit. P1, P2 & P3 have no I/O P1 P2 P3 P1 P2 P3 P1 P2 P3 P2 0 10 P1 waiting time: 4 P2 waiting time: 6 P3 waiting time: 6 The average waiting time (AWT): (4+6+6)/3 = 5.33 CS 423UG - Operating Systems, Indranil Gupta
Selecting a good Time Quantum • Time slice too large • FIFO behavior • Poor response time • Time slice too small • Too many context switches (high overhead) • Inefficient CPU utilization • Heuristic (rule of thumb): select quantum so that 70%-80% of jobs block within quantum length • Typical time-slice: 10 ms to 100 ms • Time spent in system depends on size of the job and the number of jobs CS 423UG - Operating Systems, Indranil Gupta
V. Multi-Queue Scheduling • Hybrid between Priority and Round-Robin • Multiple and finite number of queues • Each process assigned permanently to one of these queues • Scheduler: first choose a queue, then a job from that queue • Scheduler: Choosing a queue/level • Fixed Priorities • % CPU spent on queue • Scheduler: Choosing a job within a queue • Round Robin • FCFS • Example: one queue/level for each of • System processes • Interactive programs • Student Processes • Background Processes • % CPU + Round Robin addresses the starvation and infinite blocking problems CS 423UG - Operating Systems, Indranil Gupta
Multi-Queue Scheduling: Example 20% 30% 50% CS 423UG - Operating Systems, Indranil Gupta
Real Life Analogy • Tasks (to-do list) for poor Bob • Class 3 priority (lowest): Bob’s tasks • Watch football on TV (20%) • Class 2 priority: tasks given by his boss • “Where is the report?” (30%) • Class 1 priority (highest) : tasks given by his girlfriend • Buy me a … (50%) CS 423UG - Operating Systems, Indranil Gupta
VI. A Variation: Multi-level Feedback Algorithm • Multi-Level Queue with priorities • Processes may move between queues • Each queue represents jobs with similar CPU usage • Each queue is RR with a fixed quantum, but different queues have different quantum values • Rationale: Once an I/O-bound process completes an I/O request, it should have higher CPU priority CS 423UG - Operating Systems, Indranil Gupta
Multi-level Feedback Algorithm: Example CS 423UG - Operating Systems, Indranil Gupta
Multi-level Feedback Algorithm (Details) • Example: Queuei has time-slice t = 2i • If a job in Queuei doesn't block by end of time-slice, it is moved to Queuei +1 • Lowest priority Queue (highest i value) is FIFO • Problem: • Starvation: Aging may move Process to lower priority queue CS 423UG - Operating Systems, Indranil Gupta
Summary We’ve learnt many scheduling algorithms! • Shortest job first (SJF) • SJF-preemptive • Priority • Round-robin • Multi-Queue • Multi-level Feedback CS 423UG - Operating Systems, Indranil Gupta
Announcements • Reading for this lecture was: Section 2.5.3 • Reading for this Friday: Rest of 2.5 • Real-time systems • Multiple processors • Threads • You’ve all the information that you need for MP1! • HW1 due this Friday at start of lecture. CS 423UG - Operating Systems, Indranil Gupta