CPU Scheduling – Outline
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 5: CPU Scheduling – Outline What is scheduling in the OS ? What are common scheduling criteria ? How to evaluate scheduling algorithms ? What are common scheduling algorithms ? How is thread scheduling different from process scheduling ? What are the issues in multiple-processor scheduling ? Operating systems case studies. 1 Basic Concepts Multiprogramming most processes alternate between CPU bursts and I/O bursts PU free and idle during #$O burst schedule another process on the !" maximizes PU utilization !" bound process spends most of its time in the CPU at least a few long PU bursts #$O bound process spends most its time performing #$O several short PU bursts 2 CPU Scheduler 'esponsible for the selection of the ne%t running process part of the OS dispatcher selects from among the processes in memory that are ready to execute based on a particular strategy When does !" scheduling happen ? process switches from running to waiting state process switches from running to ready state process switches from waiting to ready state process terminates Scheduling under ( and ) is nonpreemptive *ll other scheduling is preemptive 3 Preemptive Vs. Non-preemptive CPU Scheduling +on-preemptive scheduling process voluntarily releases the CPU (conditions 1 and 4) easy, requires no special hardware poor response time for interactive and real-time systems !reemptive scheduling OS can force a running process involuntarily relinquish the !" arrival of a higher priority process running process exceeds its time-slot may require special hardware, eg., timer may require synchronization mechanisms to maintain data consistency complicates design of the kernel favored by most OSes 4 Dispatcher Scheduler is a part of the dispatcher module in the OS 1unctions of the dispatcher get the new process from the scheduler switch out the context of the current process give CPU control to the new process 2ump to the proper location in the new program to restart that program 3ime taken by the dispatcher to stop one process and start another running is called the dispatch latency 5 Scheduling Queues Job /ueue5 consists of all processes all jobs (processes), once submitted. are in the job queue scheduled by the long-term scheduler 'eady /ueue5 consists of processes in memory processes ready and waiting for execution scheduled by the short-term or CPU scheduler 6evice /ueue5 processes waiting for a device multiple processes can be blocked for the same device #$O completion moves process back to ready queue 6 Scheduling Queues (2) 7 Performance Metrics for CPU Scheduling !" utilization: percentage of time that the !U is busy 3hroughput: number of processes that complete their e%ecution per time unit 3urnaround time: amount of time to execute a particular process ,submission time to completion time- Waiting time: amount of time a process has been waiting in the ready /ueue 'esponse time: amount of time it takes from when a re/uest was submitted until the 7rst response is produced Scheduling goals maximize CPU utilization and throughput minimize turnaround time, waiting time and response time be fair to all processes and all users 8 Method for Evaluating CPU Scheduling Algorithms 8valuation criteria define relative importance of the performance metrics include other system-specific measures 6eterministic modeling takes a particular predetermined workload and defines the performance of each algorithm for that workload simple and fast, gives exact numbers difficult to generalize results can recognize algorithm performance trends over several inputs used for explaining scheduling algorithms used in the rest of this chapter ! 9 Workload Models and Gantt Charts Workload model: Process Arrival Time Burst Time P1 0 8 P2 1 4 P3 1 10 P4 6 2 :antt charts5 bar chart to illustrate a particular schedule figureP1 shows batch scheduleP2 P3 P4 0 8 12 22 24 10 Deterministic Modeling Example Suppose we have processes *. ;. and . submitted at time <. We want to 0now the response time. waiting time. and turnaround time of process A turnaround time wait time + + response time = 0 A B C A B C A C A C Time 11 Deterministic Modeling Example Suppose we have processes *. ;. and . submitted at time <. We want to 0now the response time. waiting time. and turnaround time of process B. turnaround time wait time + response time A B C A B C A C A C Time 12 Deterministic Modeling Example Suppose we have processes *. ;. and . submitted at time <. We want to 0now the response time. waiting time. and turnaround time of process turnaround time wait time + + + response time A B C A B C A C A C Time 13 Method for Evaluating CPU Scheduling Algorithms (2) =ueueing models analytically model the queue behavior ,under some assumptions) involves a lot of complicated math can only handle a limited number of distributions and algorithms may not be very accurate because of unrealistic assumptions Simulations get a workload information from a system simulate the scheduling algorithm compute the performance metrics time and space intensive is practically the best evaluation method 14 Simulation Illustration 15 Scheduling Algorithms 1irst Come. 1irst Served ,1 1S- Shortest Job 1irst ,S41- !riority Based 'ound Robin ,''- Multilevel =ueue Scheduling Multilevel 1eedback =ueue Scheduling 16 First Come, First Served Scheduling *ssigns the !" based on the order of the re/uests. #mplemented using a 1#1O /ueue. +o preemption *dvantages straightforward, simple to write and understand 6isadvantages average waiting time may be too long huge variation based on when processes arrive cannot balance CPU-bound and #$O-bound processes convoy effect, short process behind long process cannot be used for time-sharing systems 17 FCFS Scheduling Example Process Arrival Time Burst Time P1 0 24 P2 0 3 P3 0 3 Suppose that the processes arrive in the order5 P . P! . P" !( !@ !C < @) @A C< Waiting time for P > <? P! > @)? P" > @A *verage waiting time5 ,< B @) B @A-$C > (A 18 FCFS Scheduling (2) Suppose that the processes arrive in the order P! . P" . P 3he :antt chart for the schedule is5 !@ !C !( < C D C< Waiting time for P # D$ P! > <$ P" # C *verage waiting time5 ,D B < B C-$C > C Much better than previous case but large variation 19 Shortest Job First (SJF) Scheduling Order each process based on the length of its ne%t !" burst *llocate !" to the process from the front of the list shortest next PU burst *dvantages SJF is optimal achieves minimum average waiting time for a given set of processes 6isadvantages difficult to know the length of the next CPU request can ask the user, who may not know any better ! model-based prediction can lead to process starvation 20 Example of SJF Process Arrival Time Burst Time P1 0 6 P2 0 8 P3 0 7 P4 0 3 S41 scheduling chart ! !C !@ ) !( < C F (D @) *verage waiting time > ,C B (D B F B <- $ ) > A 21 Estimate Length of Next CPU Burst an only estimate the length next PU burst similar to previous !" bursts ? how relevant is the history of previous CPU bursts ? alculated as an exponential average of the previous !" bursts for the process 1ormula5 th 1. tn actual length of n CPU burst 2. n1 predicted value for the next CPU burst 3. , 0 1 4. Define : n1 tn 1 n . 22 Estimate Length of the Next CPU Burst (2) >< nB( > n 'ecent history does not count >( nB( = tn Only the actual last !" burst counts #f we expand the formula. we get5 nB( > tnB,( & ' tn & B G * ()( - ' tn - * B G n B( ()( - ' < Since both and ,( - - are less than or e/ual to (. each successive term has less weight than its predecessor 23 Estimate Length of the Next CPU Burst (3) 24 Preemptive SJF Scheduling +ew shorter process can preempt longer current running process Process Arrival Time Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Shortest 'emaining 3ime 1irst scheduling chart5 P P P P P 1 2 4 1 3 0 1 5 10 17 26 *verage waiting time > D.H 25 Priority Scheduling * priority number associated with each process !" allocated to the process with the highest priority equal priority processes scheduled in F 1S order #nternally determined priorities time limit, memory requirements, etc SJF uses next CPU burst for its priority (how?- 8%ternally specified priorities process importance, user-level. etc an be preemptive or non-preemptive 3ext uses low numbers for high priorities 26 Priority Scheduling (2) *dvantages priorities can be made as general as needed 6isadvantage low priority process may never execute (indefinite blocking or starvation) *ging technique to prevent starvation increase priority of processes with time 27 Round Robin Scheduling (RR) 'ound robin algorithm arrange jobs in FCFS order allocate CPU to the first job in the /ueuefor one time-slice preempt job, add it to the end of the queue allocate CPU to the next job and continue... One time slice is called a time ,uantu+ #s by de7nition preemptive can be considered as 1 1S with preemption *dvantages I simple. avoids starvation 6isadvantages may involve a large context switch overhead higher average waiting time than S41 28 an I/O bound process on a heavily loaded system will run more slow Example of RR with Time Quantum = 4 Process Burst Times P1 24 P2 3 P3 3 3he :antt scheduling chart is: !( !@ !C !( !( !( !( !( < ) A (< () (J @@ @D C< *verage waiting time > H.DD 29 Round Robin Scheduling (2) !erformance depends on the length of the time quantum large time /uantum → FCFS like behavior small time /uantum → large context switch overhead :enerally, time /uanta range from (0 – 100 milliseconds context switch time is less than (0 microseconds 'R has larger waiting time. but provides better response time for interactive systems 3urnaround time depends on the size of the time /uantum 30 Turnaround Time Varies With The Time Quantum 31 Multilevel Queue 'eady /ueue is partitioned into separate /ueues: foreground ,interactive- background ,batch- 8ach /ueue has its own scheduling algorithm foreground I '' background – F 1S Scheduling must be done between the /ueues Fixed priority scheduling? (i.e., serve all from foreground then from background).