2.5 Scheduling Given a multiprogramming system. Given a multiprogramming system. Many times when more than 1 process is waiting for the CPU (in the ready queue). Many times when more than 1 process is waiting for the CPU (in the ready queue). The scheduler (using a scheduling algorithm) decides which process will run next. The scheduler (using a scheduling algorithm) decides which process will run next. User satisfaction is important. User satisfaction is important.
Context switch When the CPU changes from one process to another. When the CPU changes from one process to another. Expensive operation. Expensive operation. 1. User mode to kernel mode 2. Save state of current process Registers, invalidate cache, MMU Registers, invalidate cache, MMU 3. Run scheduler Pick next process to run Pick next process to run Load state of next process Load state of next process 4. Run next process
Process behavior Types: Types: I/O bound – spend most time performing I/O I/O bound – spend most time performing I/O Compute bound – spend most time performing computation Compute bound – spend most time performing computation Mixture Mixture
When do we need to schedule? creation (before/after parent?) creation (before/after parent?) exit exit block (I/O, semaphore, sleep, wait, etc.) block (I/O, semaphore, sleep, wait, etc.) I/O interrupt/completion I/O interrupt/completion clock interrupt clock interrupt non preemptive (run until you block or “cooperate” non preemptive (run until you block or “cooperate” preemptive preemptive
Scheduling environments Batch Batch Interactive Interactive Real time Real time
Scheduling algorithm goals
Concepts & definitions Throughput = # of jobs completed per hour Throughput = # of jobs completed per hour Turnaround time = avg of “start (submit) to completion” times; avg wait time Turnaround time = avg of “start (submit) to completion” times; avg wait time CPU utilization = avg of CPU busyness CPU utilization = avg of CPU busyness Response time = time between issuing a command and getting the result Response time = time between issuing a command and getting the result Proportionality = user perception that “complex” things take a long time and that is fine but “simple” things must be quick Proportionality = user perception that “complex” things take a long time and that is fine but “simple” things must be quick Predictability = regularity, especially important for audio and video streaming Predictability = regularity, especially important for audio and video streaming
Batch scheduling First-come first-served First-come first-served Shortest job first Shortest job first Shortest remaining time next Shortest remaining time next Three-level scheduling Three-level scheduling
Batch scheduling First-come first-served First-come first-served Simple Simple Non preemptive Non preemptive Process runs until it either blocks on I/O or finishes Process runs until it either blocks on I/O or finishes
Batch scheduling Shortest job first Shortest job first Optimal turnaround time (when all start together) Optimal turnaround time (when all start together) Requires that we know run time a priori Requires that we know run time a priori
Batch scheduling Shortest remaining time next Shortest remaining time next Preemptive version of shortest job first Preemptive version of shortest job first Requires a priori information Requires a priori information When a new job arrives, its total time is compared to the current process’ remaining time. If the new job needs less time to finish, the new job is started. When a new job arrives, its total time is compared to the current process’ remaining time. If the new job needs less time to finish, the new job is started. New, short jobs get good service New, short jobs get good service
Batch scheduling Three-level scheduling Three-level scheduling Admission scheduler – chooses next job begin Admission scheduler – chooses next job begin Memory scheduler – which jobs are kept in memory and which jobs are swapped to disk Memory scheduler – which jobs are kept in memory and which jobs are swapped to disk How long swapped in or out? How long swapped in or out? How much CPU time recently? How much CPU time recently? How big is the process? How big is the process? How important is the process? How important is the process? Degree of multiprogramming – number of processes in memory Degree of multiprogramming – number of processes in memory CPU scheduler – picks which runs next CPU scheduler – picks which runs next
Interactive scheduling Round-robin scheduling Round-robin scheduling Priority scheduling Priority scheduling Multiple queues Multiple queues Shortest process next Shortest process next Guaranteed scheduling Guaranteed scheduling Lottery scheduling Lottery scheduling Fair-share scheduling Fair-share scheduling
Interactive scheduling Round-robin scheduling Round-robin scheduling Simple, fair, widely used, preemptive Simple, fair, widely used, preemptive Quantum = time interval Quantum = time interval Process/context switch is expensive Process/context switch is expensive Too small and we waste time Too small and we waste time Too large and interactive system will appear sluggish Too large and interactive system will appear sluggish ~20-50 msec is good ~20-50 msec is good Every process has equal priority Every process has equal priority
Interactive scheduling Priority scheduling Priority scheduling Each process is assigned a priority; process with highest priority is next to run. Each process is assigned a priority; process with highest priority is next to run. Types: static or dynamic (ex. I/O bound jobs get a boost) Types: static or dynamic (ex. I/O bound jobs get a boost) Unix/Linux nice command Unix/Linux nice command
Interactive scheduling Ex. 4 priorities & RR w/in a priority Ex. 4 priorities & RR w/in a priority
Interactive scheduling Multiple queues Multiple queues Different types Different types Ex. 4 queues for: Ex. 4 queues for: Terminal, I/O, short quantum, and long quantum Terminal, I/O, short quantum, and long quantum
Interactive scheduling Shortest process next Shortest process next Shortest job first always produces min avg response time (for batch systems) Shortest job first always produces min avg response time (for batch systems) How do we estimate this? How do we estimate this? From recent behavior (of interactive commands, T i ) From recent behavior (of interactive commands, T i ) Example of aging (or IIR filter) Example of aging (or IIR filter) alpha near 1 implies little memory alpha near 1 implies little memory alpha near 0 implies much memory alpha near 0 implies much memory
Interactive scheduling Guaranteed scheduling Guaranteed scheduling Given n processes, each process should get 1/n of the CPU time Given n processes, each process should get 1/n of the CPU time Say we keep track of the actual CPU used vs. what we should receive (entitled to/deserved). Say we keep track of the actual CPU used vs. what we should receive (entitled to/deserved). K = actual / entitled K = actual / entitled K = 1 we got what we deserved K = 1 we got what we deserved K < 1 we got less than deserved K < 1 we got less than deserved K > 1 we got more than deserved K > 1 we got more than deserved Pick process w/ min K to run next Pick process w/ min K to run next
Interactive scheduling Lottery scheduling Lottery scheduling Each process gets tickets; # of tickets can vary from process to process. Each process gets tickets; # of tickets can vary from process to process. If you ticket is chosen, you run next. If you ticket is chosen, you run next. Highly responsive (new process might run right away) Highly responsive (new process might run right away) Processes can cooperate (give each other their tickets) Processes can cooperate (give each other their tickets)
Interactive scheduling Fair-share scheduling Fair-share scheduling Consider who (user) owns the process Consider who (user) owns the process Ex. Ex. User A has 1 process User A has 1 process User B has 9 processes User B has 9 processes Should user A get 10% and user B get 90% or should A get 50% and B get 50% (5.6% for each of the 9 processes)? Latter is fair-share. Should user A get 10% and user B get 90% or should A get 50% and B get 50% (5.6% for each of the 9 processes)? Latter is fair-share.
Real time scheduling Time plays an essential role Time plays an essential role Must react w/in a fixed amount of time Must react w/in a fixed amount of time Categories: Categories: Hard – absolute deadlines must be met Hard – absolute deadlines must be met Soft – missing an occasional deadline is tolerable Soft – missing an occasional deadline is tolerable Event types: Event types: Periodic = occurring at regular intervals Periodic = occurring at regular intervals Aperiodic = occurring unpredictably Aperiodic = occurring unpredictably Algorithm types: Algorithm types: Static (before the system starts running) Static (before the system starts running) Dynamic (scheduling decisions at run time) Dynamic (scheduling decisions at run time)
Real time scheduling Given m periodic events. Given m periodic events. Event i occurs w/ period P i and requires C i seconds of CPU time Event i occurs w/ period P i and requires C i seconds of CPU time Schedulable iff: Schedulable iff: (basically normalizing C by P)
Schedulable example Given P i = 100, 200, and 500 msec Given P i = 100, 200, and 500 msec Given C i = 50, 30, 100 msec Given C i = 50, 30, 100 msec Can we handle another event w/ P 4 =1 sec? Can we handle another event w/ P 4 =1 sec? Yes, as long as C 4 <=150 msec Yes, as long as C 4 <=150 msec
Thread scheduling User level threads User level threads No clock interrupts (per thread) No clock interrupts (per thread) A compute bound thread will dominate its process but not the CPU A compute bound thread will dominate its process but not the CPU A thread can yield to other threads within the same process A thread can yield to other threads within the same process Typically round robin or priority Typically round robin or priority + Context switch from thread to thread is simpler + App specific thread scheduler can be used - If a thread blocks on I/O, the entire process (all threads) block
Thread scheduling Kernel level threads Kernel level threads - Context switch from thread to thread is expensive (but scheduler can make more informed choices) + A thread, blocking on I/O, doesn’t block all other threads in process