Operating Systems CSE 411 CPU Management Sept. 20 2006 - Lecture 7 Instructor: Bhuvan Urgaonkar • Last class – Dispatcher – Context switch, FCFS, SJF, SRPT, RR, Priority, Quantum size – Read on your own: Multi-level (feedback) queue based scheduling • Today – More on CPU scheduling Proportional-Share Schedulers • A general class of scheduling algorithms • Process Pi given a CPU weight wi > 0 • The scheduler needs to ensure the following – forall i, j, |Ti(t1, t2)/Tj(t1,t2) - wi/wj| ≤ e – Given Pi and Pj were backlogged during [t1,t2] • Who chooses the weights and how? • Application modeling problem: non-trivial – Approaches: analytical, empirical • A part of my Ph.D. thesis • Many PS schedulers developed in the 90s – E.g., Start-time Fair Queueing (Qlinux UT-Austin/Umass-Amherst) Lottery Scheduling [Carl Waldspurger, MIT, ~1995] • Perhaps the simplest proportional-share scheduler • Create lottery tickets equal to the sum of the weights of all processes – What if the weights are non-integral? • Draw a lottery ticket and schedule the process that owns that ticket – What if the process is not ready? • Draw tickets only for ready processes – Homework 1: Calculate the time/space complexity of the operations Lottery scheduling will involve – Likely question on Exam 1 !!! Lottery Scheduling Example P1=6 P2=9 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 9 Schedule P2 Lottery Scheduling Example P1=6 P2=9 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 3 Schedule P1 Lottery Scheduling Example P1=6 P2=9 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 11 • As t ∞, processes will get their share (unless they were blocked a lot) • Problem with Lottery scheduling: Only probabilistic guarantee • What does the scheduler have to do – When a new process arrives? – When a process terminates? Schedule P2 Work Conservation • Examples of work-conserving schedulers: All schedulers we have studied so far • Examples of non-work-conserving schedulers: – DFS, a PS scheduler for multi-processors (Abhishek Chandra, Umass, 2000, now at Univ. of Minnesota) • Experiments showed the scheduler had become NWC! • Fair Airport to convert into a WC scheme NWC Scheduler WC Scheduler Reservation-based Schedulers • Each process has a pair (x, y) – Divide time into periods of length y each – Guaranteed to get x time units every period • Can be Non-work-conserving Rate Regulation: Leaky Bucket • A type of reservation-based scheduling • Leaky bucket policing – Rate ri for process Pi – CPU cycles over period t ri * t CPU requirement bi ri CPU the process gets burst Max. rate at which the process may progress Rate Regulation: Token Bucket • A type of reservation-based scheduling • Token bucket policing – Rate ri and burst bi for process Pi – CPU cycles over period t ri * t + bi bi tokens CPU requirement ri burst Deadline-based Scheduling • • • • Can be NWC Several variants NP-hard Real-time systems “Soft” real-time systems – E.g., media servers: 30 MPEG-1 frames/sec – A few violations may be tolerable An Interesting Problem: An Invitation to do Research on CPU Scheduling • Can we achieve the effect of a scheduler that can provide resource guarantees using a priority-based scheduler? • You are welcome to talk to me during office hours if you find this interesting or if you have any thoughts on this Hierarchical Schedulers Reservation-based (4, 10) (6, 10) Round-robin w=1 UNIX Lottery w=2 UNIX Processes • Variety of schedulers • Subsets of processes with different scheduling needs Scheduler Considerations: Context-Switch Overhead • Switching context – – – – – – • • • • User mode to Kernel mode Save PCB Process interrupt (e.g, TCP/IP processing) if needed Run scheduling algorithm Load registers (e.g., PC) from PCB of chosen process Flush TLB (will discuss this under memory management) Switching to user mode Jumping to the proper location in the user program to restart that program Context switch time for Pentium/Linux: tens of microsec So how often should the scheduler be invoked? Scheduler Considerations: Quantum Length • The duration for which a CPU-intensive process will run before being scheduled out in favor of another process • Different processes may have different quantum lengths – E.g., UNIX: Higher priority => Larger quantum • Typically tens of msec in modern systems • Small => Context switch overhead high • Large => Bad responsiveness => Interactive processes suffer Scheduler Considerations: CPU Accounting • Scheduler maintains CPU usage, last scheduling instant etc. in each PCB • Who should be charged for the CPU usage during interrupt processing? – Optional reading: Resource containers (Rice University, OSDI 1999, Gaurav Banga et al.) Scheduler Considerations: Time and Space Requirements • Run time (n processes) – FCFS: O(1) – RR: O(1) – Deadline-based algos: NP-hard variants, poly-time heuristics • Update time: Operations done when set of processes changes (new, terminate, block, become ready) • Space requirements – Space to store various data structs Scheduler Evaluation • Optimize one or more of response time, waiting time, throughput, fairness, utilization, .. • Overheads – Run time (time to pick the next process to schedule) – Update time – Space requirements Analytical Approaches to Scheduler Evaluation • Example 1: Our evaluation of FCFS and SJF • Example 2: Queueing Theory – Little’s Law: applies to any scheduling discipline – avg_num_processes_in_system = tput * avg_time_spent_in_system yet to arrive serviced waiting running – Queuing theory provides expressions for mean response time for certain scheduling policies (FCFS, PS, LCFS) – Other statistical properties of response time only under restrictive assumptions on arrival process and service time Discussion: Analytical Approaches • Pros: – Intellectually satisfying! – Quick, no code to be written – May provide generally applicable results • Cons: – Often over-simplified • Hard to model complex scheduling algorithms • Lack of info about workloads Simulation-based Evaluation • Write software that mimics the behavior of the scheduler • Subject it to realistic workload • Observe output and make inferences about performance of scheduler • Only need to know the algorithms, no need to do complex math • Easier to implement than the actual system Prototyping • Build it! Analysis, Simulation, and Prototyping Compared accuracy, effort, time investment, programming skills mathematical skills, generality Analysis Simulation Prototyping Hybrid Approaches Analysis Simulation Prototyping Threads What is a Thread? • A basic unit of CPU utilization like a process (not necessarily known to the OS though) • “Smaller” than a process – Part of a process – Shares code + data + some other OS resources with other threads that belong to the same process User Threads • Thread management done by user-level threads library • OS doesn’t know about the existence of these threads • Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads Kernel Threads • OS sees and manages these threads • OS provides system calls to create, terminate, etc. (just like the system calls it provides for processes) • Examples – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X Benefits • • • • Responsiveness Resource Sharing Economy Utilization of MP Architectures