Uniprocessor Scheduling • The basic concepts of scheduling apply to many types of systems: • Manufacturing, shipping, network routing • Process and device scheduling in computer systems • Often goals of user friendliness (minimized response time, ease of use, reliability, fairness) conflict with goals of efficiency (maximize mean throughput, minimize mean turnaround time), maximize resource use, as well as of security • Scheduling algorithms also must handle – Boundedness – Specific system goals • Ex: time-constraints for real-time systems Chapt 9 uniprocessor scheduling 1 Scheduling goals: • General: policy enforcement, enforce priorities, fairness within same priority level • Batch systems: maximum throughput; minimize turnaround time; keep CPU and other components busy • Interactive systems: minimize response time • Hard real-time systems: meet deadlines, satisfy predictability requirements Chapt 9 uniprocessor scheduling 2 Mix CPU & I/O burst cycles for efficient CPU utilization • Programs have different characteristics – I/O-bound programs (most COBOL, SAS, and transaction processing) have short bursts of CPU time before relinquishing CPU for I/O – CPU-bound programs (computation) have long bursts of CPU time before relinquishing CPU – CPU-bound programs can use CPU while I/Obound programs are waiting for I/O Chapt 9 uniprocessor scheduling 3 Non-preemptive scheduling vs preemptive scheduling • Non-preemptive systems – Processor is not taken away from process unless it is relinquished for system call or program error/termination • If I/O interrupt occurs, the ISR returns the CPU to the interrupted process at the end of its interrupt handling • Windows 3.1, 98, Mac OS 5.0 • Preemptive systems – Processor is removed if time-quantum expires • Requires timer interrupt – Processor is removed if high priority process arrives or is unblocked – Adds a great deal of complexity to systems Chapt 9 uniprocessor scheduling 4 Dispatcher • Dispatcher is the kernel process that takes a process from ready state to run state – Handles context switching • Saves state of currently executing process • Restores/ initiates state of new process – Places new user PC in PC register – Stores/ Restores register and other values – Changes mode • Dispatcher runs in supervisor mode • Mode changed to user mode for user processes – Dispatcher execution time must be minimized (dispatch latency) • Motivation for multiple register sets Chapt 9 uniprocessor scheduling 5 Questions • Is the dispatcher part of the kernel? Why? – Yes. Dispatcher executes for each process allocation to CPU • Should dispatching in a multiprocessing OS be implemented within a (microkernel or portion of) OS running on each processor? – I believe Yes. Each processor in a multiprocessing system should have its own, efficient dispatcher. Where should the dispatch list be? Chapt 9 uniprocessor scheduling 6 Short-term scheduler • Decides which ready process will be dispatched next • Text says that dispatcher and short-term scheduler are the same process – Typically that is not so • Short-term scheduler decides dispatching order • Dispatcher actually does context switching and mode changes very very quickly Chapt 9 uniprocessor scheduling 7 Medium-term scheduler • Decides which (and when) processes should be made non-resident, or swapped back into memory – Only required when too many processes are in a state of execution Chapt 9 uniprocessor scheduling 8 Long term scheduler • Decides when and which programs to be made into processes – Controls the degree of multiprogramming – In batch multiprogramming systems, decides • Whether to accept more processes from batch spool • Which of the processes to accept based on scheduling goals – In time-sharing system, accepts all arriving processes up to a limit (then must deny others a connection) Chapt 9 uniprocessor scheduling 9 Scheduling Algorithms • We consider several scheduling algorithms in discussing goals of priority, shortest waiting time, fairness – The following processes are all available at scheduling time: – Process – W – X – Y – Z Service Time 24 3 3 8 Chapt 9 uniprocessor scheduling 10 FCFS Scheduling Algorithms • Gantt charts – FCFS (non-preemptive) W 0 X 24ms Y 27 Z 30 38 Mean response time is (0+24+27+30)/4= 81/4 =20 ms; Mean turnaround time is (24 + 27 + 30 + 38)/4 = 30 ms Normalized turnaround time (ratio of turnaround time to service time) for W = 24/24= 1; X = 27/3= 9; Y = 30/3= 10; Z = 38/8= 4.5 Increasing values indicate decreasing level of service Chapt 9 uniprocessor scheduling 11 FCFS with different arrival times Chapt 9 uniprocessor scheduling 12 First-Come-First-Served • Each process joins the Ready queue • When the current process ceases to execute, the process that has been in the Ready queue the longest is selected Shortest Process Next (SPN) • Processes must state their total (or burst) service needs • This example is non-preemptive – Preemptive type is considered later • A [0-3] B [3-9] E [9-11] C[11-15] D [15-20] • Mean turnaround time = – (3 + (9-2) + (15-4) + (20-6) + (11-8))/5 = 7.6 • Mean Normalized Turnaround time = ((3/3+ 7/6 + 11/4 + 14/5 + 3/2))/5 =1.84 Chapt 9 uniprocessor scheduling 14 Shortest Process Next A__B___E__C____D_____ 0 3 9 11 15 20 • Mean turnaround time A finishes at 3 and starts at 0 ((3-0) + (9-2) + (15-4) + (20-6) + (11-8))/5 = 7.6 • Mean Normalized Turnaround time = ((3/3+ (9-2)/6 + (15-4)/4 + (20-6)/5 + 3/2))/5 =1.84 Chapt 9 uniprocessor scheduling 15 FCFS/SPN Scheduling Algorithms - Issues – (Non-preemptive )FCFS • If long process (P1) is first, it hogs CPU; devices stay idle • Poor response time for later processes – Not appropriate for real-time systems – Preemptive or non-preemptive SPN (shortest process next) • Provides minimal average waiting time • Yet if short jobs get CPU first, they may all compete for devices – many requests to DMA slow down the CPU • How do we estimate which is the shortest job? – Batch systems may require that users pre-state CPU needs – Scheduler may estimate by process history » But process behavior may change (between CPU and I/O) – If SPN is preemptive, time slices help us in estimate Chapt 9 uniprocessor scheduling 16 Shortest Remaining Time (SRT) • Preemptive SPN- prestate needs – Assume 1 unit time quantum (q =1) • A[0-3]B[3-4]C [4-8] E[8-10] B[10-15]D[15-20] A__B_C__E___B___D__ 0 3 4 8 10 15 20 • Mean turnaround time = – (3 + (15-2) + (8-4) + (20-6) + (10-8))/5 = 7.2 • Mean Normalized Turnaround time = ((3/3+ 13/6 + 4/4 + 14/5 + 2/2))/5 =1.593 Chapt 9 uniprocessor scheduling 17 Round Robin Scheduling • Preemptive form of FCFS • Requires timing interrupt support • A time slice is chosen for a process’s maximum burst (how long the process can hold CPU) – If process exceeds time slice, the processor is forcibly removed – If it surrenders early (perhaps for an I/O), processor is allocated to another process Chapt 9 uniprocessor scheduling 18 Round-robin time quantum • What should the time quantum be? – Turnaround time suffers and efficiency suffers if time quantum is too low – Becomes (non-preemptive) FCFS if time quantum is too large (with longer average response times) • 80% of CPU bursts should be < time quantum • Still, CPU-bound jobs get more of the CPU than I/O- bound jobs – Inefficient use of devices – Increased variance of response time dependent on job mix Chapt 9 uniprocessor scheduling 19 Round Robin • Uses preemption based on a clock – also known as time slicing, because each process is given a slice of time before being preempted. Round Robin A’s turnaround time is 4. A’s Normalized is 4/3 = 1.3 B’s turnaround time is (18-2). Normalized is 16/6 = 2.7 C’s turnaround time is (17-4). Normalized is 13/4 = 3.25 D’s turnaround time is (20-6). Normalized is 14/5 = 2.8 E’s turnaround time is (15-8) Normalized is 7/2 = 3.5 Newly arrived process is placed on queue before preempted/suspended process Chapt 9 uniprocessor scheduling 21 Priority Queuing • Scheduling with prioritized queues – Lower-priority processes may never be scheduled (starve) – We can raise their priorities dynamically by waiting time • Called aging – Static priorities typically MUST be scheduled by priority assignment Chapt 9 uniprocessor scheduling 22 Types of Priority Scheduling • Internal priorities • High priority assigned to I/O bound jobs – expected to complete service quickly • Memory and file needs or allocation may affect priority • External priorities • • • • Real-time needs President of company Premium service classes Console operator decides to boost or lower priorities • Dynamic or static priority assignments • Indefinite blocking (starvation) is enabled • Controlled by increasing priority by waiting time (aging) Chapt 9 uniprocessor scheduling 23 Priority Scheduling • Assume low values are higher priority. Preemption can occur after each time slice of 2 ms. (In case of a tie, use FCFS) arrives burst – – – – P1 P2 P3 P4 0 ms 1 2 3 10ms 2 ms 1 ms 3ms priority 3 3 2 1 P1 P3 P4 P1 P2 0-2 2-4 4-7 7-15 15-18 Average turnaround time ((15-0)+(18-1) + (4-2) +(7-3))/4 = 9.5 ms Chapt 9 uniprocessor scheduling 24 Multilevel feedback queues (DEC VMS) • Top 16 queues for systems processes scheduled by priority interrupts – Real-time needs – These are fast and non-preemptive • Each queue uses FCFS • Next 13 RR queues are for priority time-sharing processes • Process is placed at the end of higher priority queue if it behaves well (relinquishes CPU voluntarily) • Process placed at end of lower queue if CPU forcibly removed • Batch jobs on bottom 3 queues – FCFS (starvation possible) • Larger time quanta assigned to lower RR queues Chapt 9 uniprocessor scheduling 25 Fair-Share Scheduling • System keeps track of number of processes (or threads) of a group to estimate how often each process (thread) gets a turn • Good for scheduling threads mapped to kernel threads • Different schemes exist but typically include factors of static thread priority, thread processor use; group processor use Chapt 9 uniprocessor scheduling 26 Real-Time Scheduling • Soft real-time • Multimedia, interactive graphics • Priority scheduling • System calls may be preempted – Must, however, protect shared data • Hard real-time processes are assigned priorities based on their time constraints (preemptive scheduling) • System must decide # of levels and how to assign priorities • May be interrupt driven based on priorities • If scheduling is required, processes prestate CPU needs (resource reservation) so that system can guarantee time constraints – Scheduling function must have minimal overhead Chapt 9 uniprocessor scheduling 27