Chapter 5: CPU Scheduling Operating System Concepts 7 th Edition, Jan 14, 2005 Silberschatz, Galvin and Gagne 2005
Outline Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Thread Scheduling Operating Systems Examples Java Thread Scheduling Algorithm Evaluation
Basic Concepts Operating System Concepts 7 th Edition, Jan 14, 2005 Silberschatz, Galvin and Gagne 2005
Basic Concepts Maximum CPU utilization obtained with multiprogramming CPU scheduling is the basis of multiprogramming Key to the success of CPU scheduling CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait CPU burst, I/O burst, CPU burst, I/O burst CPU burst distribution usually a large number of short CPU bursts and a small number of long CPU bursts I/O bound: has many short CPU bursts CPU-bound: have a few long CPU burst
Alternating Sequence of CPU And I/O Bursts
Histogram of CPU-burst Times
CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: Switches from running to waiting state Switches from running to ready state Switches from waiting to ready state Terminates Nonpreemptive or cooperative scheduling When scheduling takes place only under 1 and 4 Process keeps the CPU until it releases the CPU Preemptive scheduling All others are preemptive A process having obtained the CPU may be forced to release the CPU
Dispatcher Dispatcher module gives control of the CPU to the process selected by the CPU scheduler This involves: Switching context Switching to user mode Jumping to the proper location in the user program to restart that program Dispatch latency time it takes for the dispatcher to stop one process and start another running Should be as fast as possible
Scheduling Criteria CPU utilization keep the CPU as busy as possible Throughput # of processes that complete their execution per time unit Turnaround time amount of time to execute a particular process Waiting time amount of time a process has been waiting in the ready queue Response time amount of time it takes from when a request was submitted until the first response is produced For interactive processes, we may not care the turnaround time
Scheduling Criteria (Cont.) Optimization Criteria -- may be conflict Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time In real cases Minimize the variance in the response time (predictable) Minimize the average waiting time
Scheduling Algorithms Operating System Concepts 7 th Edition, Jan 14, 2005 Silberschatz, Galvin and Gagne 2005
First-Come, First-Served (FCFS) Ready queue is a FIFO queue Example: Process CPU Burst Time Scheduling P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 P 1 P 2 P 3 0 24 27 30
First-Come, First-Served (FCFS) Scheduling (Cont.) Suppose that the processes arrive in the order: P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case P 2 P 3 P 1 0 3 6 30 Convoy effect: all the other processes wait for the one big process to get off the CPU Result in lower CPU and device utilization FCFS scheduling is nonpreemptive Unsuitable for time-sharing systems
Shortest-Job Job-First (SJF) Scheduling Use the of next CPU burst lengths to schedule the process with the shortest CPU burst time Should be termed as: shortest-next-cpu-burst algorithm Depend on the length of next CPU burst of a process, not total length Two schemes: nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. Sometimes called the Shortest-Remaining-Time-First (SRTF)
Example of Non-Preemptive SJF
Example of Preemptive SJF
Shortest-Job Job-First (SJF) Scheduling (Cont.) SJF is optimal gives minimum average waiting time for a given set of processes Problem: cannot be implemented in CPU scheduler No way to know the length of the next CPU burst Sol.: predict by using the length of previous CPU bursts, using exponential averaging 1. 2. 3. 4. t n actual lenght of n n 1 predicted value for the next, 0 1 Define : th CPU burst CPU burst
Examples of Exponential Averaging =0 n+1 = n Recent history does not count =1 n+1 = t n Only the actual last CPU burst counts If we expand the formula, we get: n+1 = t n +(1 - ) t n -1 + +(1 - ) j t n -j + +(1 - ) n +1 0 Since both and (1 - ) are less than or equal to 1, Each successive term has less weight than its predecessor
Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive nonpreemptive Problem: starvation Low priority processes may never execute Solution : aging As time progresses increase the priority of the process
Example of Priority Scheduling Process Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 P 2 P 5 P 1 P 3 P 4 0 1 6 16 18 19 Average Waiting Time = 8.2
Round Robin (RR) Each process gets a small unit of CPU time (time quantum) Usually 10-100 milliseconds. After this time has elapsed, the process is preempted. If there are n processes in the ready queue and the time quantum is q, Each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high Rule of thumb: 80% of CPU bursts should be shorter than the time quantum
Example: RR with Time Quantum = 20
How a Smaller Time Quantum Increases Context Switches
Turnaround Time Varies With The Time Quantum
Multilevel Queue Scheduling Ready queue is partitioned into separate queues: According to process properties and scheduling needs foreground (interactive) and background (batch) Normally, processes are permanently assigned to one queue Each queue has its own scheduling algorithm, for example foreground RR background FCFS Scheduling must be done between the queues Fixed priority scheduling: serve all from foreground then from background. Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes Example: 80% to foreground in RR and 20% to background in FCFS
Multilevel Queue Scheduling
Multilevel Feedback Queue A process can move between the various queues Idea Separate processes with different CPU-burst characteristics A process waiting too long in a lower-priority queue may be moved to a higher-priority queue Thus Leave I/O-bound and interactive processes in the higherpriority queues
Example of Multilevel Feedback Queue Three queues: Q 0 RR with time quantum 8 milliseconds Q 1 RR time quantum 16 milliseconds Q 2 FCFS Scheduling A new job enters queue Q 0 which is served RR. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served RR and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.
Multilevel Feedback Queues
Multiple-Processor Scheduling CPU scheduling more complex when multiple CPUs are available Separate vs. common ready queue Load sharing Asymmetric multiprocessing only the master process handle the scheduling algorithm and accesses the system data structures Symmetric Multiprocessing (SMP) each processor makes its own scheduling decisions Access and update a common data structure Must ensure two processors do not choose the same process Windows, Linux, Mac OS
Processor Affinity Processor Affinity When process migrates to another processor Cache memory must be invalidated and re-populated Most SMP systems avoid migration of process from one processor to another Attempt to keep a process running on the same processor
Load Balancing Keep the workload evenly distributed across all processors Two approaches Push migration A specific task periodically checks the load on each processor If imbalance, push processes from overloaded to idle or less-busy processor Pull migration An idle process pulls a waiting task from a busy processor Can be implemented in parallel But loading balancing counteracts the benefits of processor affinity
Symmetric Multithreading Symmetric multithreading Create multiple logical processors on the same physical processor A feature provided in architecture, not software Hyperthreading technology on Intel processor
A SMT Architecture To the OS, four processors are available
Thread Scheduling Process-contention scope Local Scheduling How the threads library decides which thread to put onto an available LWP System-contention scope Global Scheduling How the kernel decides which kernel thread to run next
Operating System Examples Solaris scheduling Windows XP scheduling Linux scheduling Operating System Concepts 7 th Edition, Jan 14, 2005 Silberschatz, Galvin and Gagne 2005
Solaris Scheduling Priority-based process scheduling Classes: real time, system, time sharing, interactive Each class has different priority and scheduling algorithm
Solaris Scheduling
Windows XP Scheduling Priority-based preemptive scheduling 32-level priority scheme Variable (1-15) and real-time (16-31) classes, 0 (memory manage) A queue for each priority. Traverses the set of queues from highest to lowest until it finds a thread that is ready to run Run the idle thread when no ready thread Base priority of each priority class Initial priority for a thread belonging to that class
Windows XP Priorities Priority class Base priority Relative priority
Linux Scheduling Preemptive, priority-based scheduling with two priority ranges Real-time: 0~99 Nice (for time-sharing): 100~140 Time-sharing: dynamic priority-based The runqueue consists of tasks that are ready to run Consists of active array and expired array Priority is changed depends on task s interactivity Real-time: static priority-based FCFS and RR Highest priority process always runs first
The Relationship Between Priorities and Time-slice length
List of Tasks Indexed According to Priorities
Algorithm Evaluation Operating System Concepts 7 th Edition, Jan 14, 2005 Silberschatz, Galvin and Gagne 2005
Algorithm Evaluation Define the criteria for evaluation and comparison Ex. Maximize CPU utilization under the constraint that the maximum response time is 1 second Evaluation methods Deterministic modeling Queuing models Simulations Implementation Environment in which the scheduling algorithm is used will change Your algorithm is good today, but still good tomorrow?
Deterministic Modeling Analytic evaluation (algorithm) + (workload) = (number of formula) This number is used to evaluate the performance of the algorithm for that workload Deterministic modeling is one analytic evaluation Deterministic modeling Takes a particular predetermined workload and defines the performance of each algorithm for that workload Require exact numbers for input, and answers apply only to the input
Example of Deterministic Modeling Example: CPU burst time: P1 = 10, P2 = 29, P3 = 3, P4 = 7, P5 = 12 Algorithm evaluations If FCFS, average waiting time = 28 If nonpreemptive SJF, average waiting time = 13 If RR, average waiting time = 23 In this case, nonpreemptive SJF is the best Similar to what we have done in this Chapter Simple, fast, and give exact numbers
Gantt Chart FCFS Nonpreemptive SJF RR
Queueing Models Input: Distribution of CPU and I/O bursts Distribution of process arrival time Output: compute the average throughput, utilization, waiting time Mathematical and statistical analysis Approximation of a real system accuracy may be questionable
Simulations Programming a model of the computer system Use software data structure to model queues, CPU, devices, timers Data to drive the simulation Random-number generator according to probability distributions Processes, CPU- and I/O-burst times, arrivals/departures Trace tape the usage logs of a real system Disadvantage expensive
Evaluation of CPU Scheduler by Simulation
Implementation Code a scheduling algorithm, put it in OS, and see Put the actual algorithm in the real system for evaluation under real operating conditions Difficulty High cost Coding the algorithm and modifying the OS Reaction of the users to a constantly changing OS Environment in which the algorithm is used will change Good solutions Flexible scheduling algorithm that can be altered by the system managers or users to tune Use APIs that modify the priority of a process or thread