Lecture 5 CPU Scheduling Contents n Why CPU
- Slides: 33
Lecture 5: CPU Scheduling
Contents n Why CPU Scheduling n Scheduling Criteria & Optimization n Basic Scheduling Approaches n Priority Scheduling n Queuing and Queues Organization n Scheduling Examples in Real OS n Deadline Real-Time CPU Scheduling AE 4 B 33 OSS Lecture 5 / Page 2 Silberschatz, Galvin and Gagne © 2005
Basic Concepts n Maximum CPU utilization obtained with multiprogramming n CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait n CPU burst distribution AE 4 B 33 OSS Lecture 5 / Page 3 Silberschatz, Galvin and Gagne © 2005
CPU Scheduler n Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them n CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates n Scheduling under 1 and 4 is nonpreemptive n All other scheduling is preemptive AE 4 B 33 OSS Lecture 5 / Page 4 Silberschatz, Galvin and Gagne © 2005
Dispatcher n Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context l switching to user mode l jumping to the proper location in the user program to restart that program l n Dispatch latency – time it takes for the dispatcher to stop one process and start another running – overhead AE 4 B 33 OSS Lecture 5 / Page 5 Silberschatz, Galvin and Gagne © 2005
Scheduling Criteria & Optimization n CPU utilization – keep the CPU as busy as possible l Maximize CPU utilization n Throughput – # of processes that complete their execution per time unit l Maximize throughput n Turnaround time – amount of time to execute a particular process l Minimize turnaround time n Waiting time – amount of time a process has been waiting in the ready queue l Minimize waiting time n Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing and interactive environment ) l AE 4 B 33 OSS Minimize response time Lecture 5 / Page 6 Silberschatz, Galvin and Gagne © 2005
First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P 3 3 n Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: P 1 P 2 0 24 P 3 27 30 n Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 n Average waiting time: (0 + 24 + 27)/3 = 17 AE 4 B 33 OSS Lecture 5 / Page 7 Silberschatz, Galvin and Gagne © 2005
FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order P 2 , P 3 , P 1 n The Gantt chart for the schedule is: P 2 0 P 3 3 P 1 6 30 n Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 n Average waiting time: (6 + 0 + 3)/3 = 3 n Much better than previous case n Convoy effect short process behind long process AE 4 B 33 OSS Lecture 5 / Page 8 Silberschatz, Galvin and Gagne © 2005
Shortest-Job-First (SJF) Scheduling n Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time n Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst l preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF) l n SJF is optimal – gives minimum average waiting time for a given set of processes AE 4 B 33 OSS Lecture 5 / Page 9 Silberschatz, Galvin and Gagne © 2005
Example of Non-Preemptive SJF Process P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 Arrival Time 7 4 1 4 Burst Time n SJF (non-preemptive) P 1 0 3 P 3 7 P 2 8 P 4 12 16 n Average waiting time = (0 + 6 + 3 + 7)/4 = 4 AE 4 B 33 OSS Lecture 5 / Page 10 Silberschatz, Galvin and Gagne © 2005
Example of Preemptive SJF Process P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 Arrival Time 7 4 1 4 Burst Time n SJF (preemptive) P 1 0 P 2 2 P 3 4 P 2 5 P 4 7 P 1 11 16 n Average waiting time = (9 + 1 + 0 +2)/4 = 3 AE 4 B 33 OSS Lecture 5 / Page 11 Silberschatz, Galvin and Gagne © 2005
Determining Length of Next CPU Burst n Can only estimate the length n Can be done by using the length of previous CPU bursts, using exponential averaging AE 4 B 33 OSS Lecture 5 / Page 12 Silberschatz, Galvin and Gagne © 2005
Examples of Exponential Averaging n =0 n+1 = n l Recent history does not count l n =1 n+1 = tn l Only the actual last CPU burst counts l n If we expand the formula, we get: n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -j + … +(1 - )n +1 0 n Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor AE 4 B 33 OSS Lecture 5 / Page 13 Silberschatz, Galvin and Gagne © 2005
Priority Scheduling n A priority number (integer) is associated with each process n The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive l Nonpreemptive l n SJF is a priority scheduling where priority is the predicted next CPU burst time n Problem Starvation – low priority processes may never execute n Solution: Aging – as time progresses increase the priority of the process AE 4 B 33 OSS Lecture 5 / Page 14 Silberschatz, Galvin and Gagne © 2005
Round Robin (RR) n Each process gets a small unit of CPU time (time quantum), usually 10 -100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. n If there are n processes in the ready queue and the time quantum is q, then 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. n Performance q large FCFS l q small q must be large with respect to context switch, otherwise overhead is too high l AE 4 B 33 OSS Lecture 5 / Page 15 Silberschatz, Galvin and Gagne © 2005
Example of RR with Time Quantum = 20 Process P 1 53 P 2 17 P 3 68 P 4 24 Burst Time n The Gantt chart is: P 1 0 P 2 20 37 P 3 P 4 57 P 1 77 P 3 P 4 P 1 P 3 97 117 121 134 154 162 n Typically, higher average turnaround than SJF, but better response AE 4 B 33 OSS Lecture 5 / Page 16 Silberschatz, Galvin and Gagne © 2005
Time Quantum and Context Switch Time Turnaround Time Varies With the Time Quantum AE 4 B 33 OSS Lecture 5 / Page 17 Silberschatz, Galvin and Gagne © 2005
Multilevel Queue n Ready queue is partitioned into separate queues: foreground (interactive) background (batch) n Each queue has its own scheduling algorithm foreground – RR l background – FCFS l n Scheduling must be done between the queues Fixed priority scheduling; (i. e. , serve all from foreground then from background). Danger of starvation. l Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i. e. , 80% to foreground in RR l 20% to background in FCFS l AE 4 B 33 OSS Lecture 5 / Page 18 Silberschatz, Galvin and Gagne © 2005
Multilevel Queue Scheduling AE 4 B 33 OSS Lecture 5 / Page 19 Silberschatz, Galvin and Gagne © 2005
Multilevel Feedback Queue n A process can move between the various queues; aging can be treated this way n Multilevel-feedback-queue scheduler defined by the following parameters: l l l AE 4 B 33 OSS number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service Lecture 5 / Page 20 Silberschatz, Galvin and Gagne © 2005
Example of Multilevel Feedback Queue n Three queues: l Q 0 – RR with time quantum 8 milliseconds l Q 1 – RR time quantum 16 milliseconds l Q 2 – FCFS n Scheduling l A new job enters queue Q 0. When it gains CPU, job receives 8 milliseconds. If it exhausts 8 milliseconds, job is moved to queue Q 1. l At Q 1 the job receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. AE 4 B 33 OSS Lecture 5 / Page 21 Silberschatz, Galvin and Gagne © 2005
Multiple-Processor Scheduling n CPU scheduling more complex when multiple CPUs are available l Multiple-Processor Scheduling has to decide not only which process to execute but also where (i. e. on which CPU) to execute it n Homogeneous processors within a multiprocessor n Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing n Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common ready queue, or each has its own private queue of ready processes n Processor affinity – process has affinity for the processor on which it has been recently running Reason: Some data might be still in cache l Soft affinity is usually used – the process can migrate among CPUs l AE 4 B 33 OSS Lecture 5 / Page 22 Silberschatz, Galvin and Gagne © 2005
Thread Scheduling n Local Scheduling – How the threads library decides which thread to put onto an available LWP n Global Scheduling – How the kernel decides which kernel thread to run next n Pthreads library has calls to choose different scheduling policies and parameters AE 4 B 33 OSS Lecture 5 / Page 23 Silberschatz, Galvin and Gagne © 2005
Windows XP Priorities Relative priorities within each class Priority classes (assigned to each process) Relative priority “normal” is a base priority for each class – starting priority of the thread l When the thread exhausts its quantum, the priority is lowered l When the thread comes from a wait-state, the priority is increased depending on the reason for waiting l 4 A thread released from waiting for keyboard gets more boost than a thread having been waiting for disk I/O AE 4 B 33 OSS Lecture 5 / Page 24 Silberschatz, Galvin and Gagne © 2005
Linux Scheduling n Two algorithms: time-sharing and real-time n Time-sharing Prioritized credit-based – process with most credits is scheduled next l Credit subtracted when timer interrupt occurs l When credit = 0, another process chosen l When all processes have credit = 0, recrediting occurs l 4 Based on factors including priority and history n Real-time Soft real-time l POSIX. 1 b compliant – two classes l 4 FCFS and RR 4 Highest priority process always runs first AE 4 B 33 OSS Lecture 5 / Page 25 Silberschatz, Galvin and Gagne © 2005
Real-Time Systems n A real-time system requires that results be not only correct but in time l produced within a specified deadline period n An embedded system is a computing device that is part of a larger system l automobile, airliner, dishwasher, . . . n A safety-critical system is a real-time system with catastrophic results in case of failure l e. g. , railway traffic control system n A hard real-time system guarantees that real-time tasks be completed within their required deadlines l mainly single-purpose systems n A soft real-time system provides priority of real-time tasks over non real-time tasks l AE 4 B 33 OSS a “standard” computing system with a real-time part that takes precedence Lecture 5 / Page 26 Silberschatz, Galvin and Gagne © 2005
Real-Time CPU Scheduling n Periodic processes require the CPU at specified intervals (periods) n p is the duration of the period n d is the deadline by when the process must be serviced (must finish within d) – often equal to p n t is the processing time AE 4 B 33 OSS Lecture 5 / Page 27 Silberschatz, Galvin and Gagne © 2005
Scheduling of two tasks (N = number of processes) Can be scheduled if r – CPU utilization Process P 1: service time = 20, period = 50, deadline = 50 Process P 2: service time = 35, period = 100, deadline = 100 When P 2 has a higher priority than P 1, a failure occurs: AE 4 B 33 OSS Lecture 5 / Page 28 Silberschatz, Galvin and Gagne © 2005
Rate Monotonic Scheduling (RMS) n A process priority is assigned based on the inverse of its period n Shorter periods = higher priority; n Longer periods = lower priority n P 1 is assigned a higher priority than P 2. Process P 1: service time = 20, period = 50, deadline = 50 Process P 2: service time = 35, period = 100, deadline = 100 works well AE 4 B 33 OSS Lecture 5 / Page 29 Silberschatz, Galvin and Gagne © 2005
Missed Deadlines with RMS failure Process P 1: service time = 25, period = 50, deadline = 50 Process P 2: service time = 35, period = 80, deadline = 80 N 2 3 4 5 10 20 RMS is guaranteed to work if N = number of processes sufficient condition AE 4 B 33 OSS Lecture 5 / Page 30 0, 828427 0, 779763 0, 756828 0, 743491 0, 717734 0, 705298 Silberschatz, Galvin and Gagne © 2005
Earliest Deadline First (EDF) Scheduling n Priorities are assigned according to deadlines: the earlier the deadline, the higher the priority; the later the deadline, the lower the priority. Process P 1: service time = 25, period = 50, deadline = 50 Process P 2: service time = 35, period = 80, deadline = 80 Works well even for the case when RMS failed PREEMPTION may occur AE 4 B 33 OSS Lecture 5 / Page 31 Silberschatz, Galvin and Gagne © 2005
RMS and EDF Comparison n RMS: Deeply elaborated algorithm l Deadline guaranteed if the condition is satisfied (sufficient condition) l Used in many RT OS l n EDF: Periodic processes deadlines kept even at 100% CPU load l Consequences of the overload are unknown and unpredictable l When the deadlines and periods are not equal, the behaviour is unknown l AE 4 B 33 OSS Lecture 5 / Page 32 Silberschatz, Galvin and Gagne © 2005
End of Lecture 5 Questions?
- Andreas carlsson bye bye bye
- Job scheduling vs process scheduling
- Sjf cpu scheduling
- Preemptive sjf
- Sjf cpu scheduling
- Fcfs calculator
- Sjf cpu scheduling
- Sjf cpu scheduling
- Scheduling concepts
- A cpu scheduling algorithm determines an order
- Cpu scheduling project
- Scheduling della cpu
- Cpu scheduling project
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Dont ask why why why
- When organizing your career portfolio you should
- Deep perineal pouch contents
- Trali symptoms
- Fresh frozen plasma vs platelets
- Thoracic skin
- Popliteal fossa
- Middle mediastinum: contents mnemonic
- The immortal life of henrietta lacks table of contents
- Medial lemniscus
- Rhomboideus minor
- Ark of the covenant lampstand
- Comic book table of contents
- Calot's triangle boundaries
- Mla table of contents
- Stylistic lexicology
- School magazine cover
- Continuous variable example
- How to write an appendix
- Fresh frozen plasma vs platelets