1 / 45

Chapter 6: CPU Scheduling

Chapter 6: CPU Scheduling. Basic Concepts Scheduling Criteria (调度准则) Scheduling Algorithms (调度算法) Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation. 6.1 Basic Concepts. Maximum CPU utilizationobtained with multiprogramming ( 多道程序设计使 CPU 利用率最大化 )

ezamora
Télécharger la présentation

Chapter 6: CPU Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6: CPU Scheduling • Basic Concepts • Scheduling Criteria (调度准则) • Scheduling Algorithms(调度算法) • Multiple-Processor Scheduling • Real-Time Scheduling • Algorithm Evaluation 11/3/2020

  2. 6.1 Basic Concepts • Maximum CPU utilizationobtained with multiprogramming (多道程序设计使CPU利用率最大化) • CPU–I/O Burst Cycle( 区间周期)– Process execution consists of a cycle of CPU execution and I/O wait. • Process execution begins with a CPU burst,That is followed by an I/O burst. Then…… • CPU burst distribution(区间分布) 21/3/2020

  3. Alternating Sequence(交替序列) of CPU And I/O Bursts _F6.1 31/3/2020

  4. Histogram (直方图)of CPU-burst Times_F6.2 41/3/2020

  5. 6.1.2 CPU Scheduler • Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.(CPU空闲) • 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. • Scheduling under 1 and 4 is nonpreemptive. (非抢占的) (cpu一旦分配,该进程会一直占用cpu到终止或切换到等待状态) • All other scheduling is preemptive.(抢占的) 51/3/2020

  6. 6.1.4 Dispatcher(分派程序) • Dispatcher module gives control of the CPU to the process selected by the short-term 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. 61/3/2020

  7. 6.2 Scheduling Criteria(调度准则) • CPU utilization(利用率)– keep the CPU as busy as possible. • Throughput(吞吐量)– number of processes that complete their execution per time unit. • Turnaround time (周转时间)– amount of time to execute a particular process. • Waiting time(等待时间)– amount of time which a process has been waiting in the ready queue. (relation with the above one?) • Response time(响应时间)– amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) 71/3/2020

  8. 6.3 Scheduling Algorithms Optimization Criteria ——‘Purpose’ • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time 81/3/2020

  9. P1 P2 P3 0 24 27 30 6.3.1 First-Come, First-Served (FCFS) Scheduling(先来先服务调度) ProcessBurst Time P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 91/3/2020

  10. P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1 . • The Gantt chart for the schedule is: • Waiting time for P1 = 6;P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case. 101/3/2020

  11. FCFS Scheduling (Cont.) • Convoy effect(护航效果)-short process behind long process • It is nonpreemptive. • It is troublesome for time-sharing system. 111/3/2020

  12. 6.3.2 Shortest-Job-First (SJF) Scheduling(最短作业优先调度) • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.(P158) • 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. This scheme is know as the Shortest-Remaining-Time-First (SRTF). 121/3/2020

  13. Shortest-Job-First (SJR) Scheduling • SJF is optimal (最理想的)– gives minimum average waiting time for a given set of processes. (Question: if there are several equal CPU burst in the following, which go first?) 131/3/2020

  14. P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) • Average waiting time = (0 + 6 + 3 + 7)/4 = 4 141/3/2020

  15. P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive) • Average waiting time = (9 + 1 + 0 +2)/4 = 3 151/3/2020

  16. Determining Length of Next CPU Burst • Any question for SJF? Yes, the difficulty for SJF is how to know the burst for the next CPU request. • We can only estimate the length. • Can be done by using the length of previous CPU bursts, using exponential averaging. 161/3/2020

  17. Prediction of the Length of the Next CPU Burst(下一个CPU区间长度的预测)_F6.3 171/3/2020

  18. Examples of Exponential Averaging •  =0 • n+1 = n • Recent history does not count. •  =1 • n+1 = tn • Only the actual last CPU burst counts. • If we expand the formula, we get: n+1 =  tn+(1 - )  tn-1+ …+(1 -  )j  tn-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.(后项比前项的权要小) 181/3/2020

  19. 6.3.3 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 • SJF is a priority scheduling where priority is the predicted next CPU burst time. 191/3/2020

  20. Priority Scheduling ProcessBurst TimePriority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 P5 P2 P1 P4 P3 0 1 6 16 18 19 The average waiting time =(6+0+16+18+1)/5=8.2ms. 201/3/2020

  21. Priority Scheduling • Priorities can be defined either internally or externally. • Problem  Starvation (饥饿)– low priority processes may never execute. • Solution  Aging (老化)– as time progresses increase the priority of the process. 211/3/2020

  22. 6.3.4 Round Robin (RR) • The round-robin(RR) scheduling algorithm is designed especially for time-sharing systems. • Each process gets a small unit of CPU time (time quantum or time slice), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. 221/3/2020

  23. Round Robin (RR) • 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. • Performance • q large  FIFO • q small  q must be large with respect to context switch, otherwise overhead is too high. 231/3/2020

  24. P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example of RR with Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: • Typically, higher average turnaround than SJF, but better response. 241/3/2020

  25. Time Quantum and Context Switch Time 251/3/2020

  26. Turnaround Time Varies With The Time Quantum 261/3/2020

  27. 6.3.5 Multilevel Queue • Ready queue is partitioned into separate queues:foreground (interactive)background (batch) • Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS 271/3/2020

  28. Multilevel Queue Scheduling 281/3/2020

  29. Multilevel Queue • Scheduling must be done between the queues. • Fixed priority scheduling; (i.e., 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; i.e., 80% to foreground in RR. • 20% to background in FCFS 291/3/2020

  30. 6.3.6 Multilevel Feedback Queue • A process can move between the various queues; aging can be implemented this way. • Multilevel-feedback-queue scheduler defined by the following parameters: • 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 301/3/2020

  31. Example of Multilevel Feedback Queue • Three queues: • Q0– time quantum 8 milliseconds • Q1– time quantum 16 milliseconds • Q2– FCFS • Scheduling • A new job enters queue Q0which is servedFCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. • At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2. 311/3/2020

  32. Multilevel Feedback Queues 321/3/2020

  33. 6.4 Multiple-Processor Scheduling • CPU scheduling more complex when multiple CPUs are available. • Homogeneous(同构的) processors within a multiprocessor. • Load sharing(负载分配)—Provide a separate queue for each processor. • Or use a common ready queue .One of two scheduling approaches may be used. 331/3/2020

  34. Multiple-Processor Scheduling • In one approach, each processor is self-scheduling. • The other approach is that appointing one processor as scheduler for the other processors,thus creating a master-slave structure. • Asymmetric multiprocessing(非对称处理)– only one processor accesses the system data structures, alleviating(减轻) the need for data sharing. 341/3/2020

  35. 6.5 Real-Time Scheduling • Hard real-time systems – required to complete a critical task within a guaranteed(保证) amount of time. • Soft real-time computing – requires that critical processes receive priority over less fortunate ones. • Implementing soft real-time functionality requires careful design of the scheduler and related aspects of the operating system. • First, the real-time processes must have the highest priority. • Second, the dispatch latency must be small. 351/3/2020

  36. Dispatch Latency 361/3/2020

  37. 6.6 Algorithm Evaluation(评估) • Deterministic modeling(确定性建模)– takes a particular predetermined workload and defines the performance of each algorithm for that workload. • Queueing models(排队模型)—Knowing arrival rates and service rates, we can compute utilization, average queue length,average wait time,and so on. • Simulations—programming a model of the computer system.However it can be expensive. • Implementation—The only completely accurate way is to code it,put it in the operating system,and see how it works. 371/3/2020

  38. Evaluation of CPU Schedulers by Simulation 381/3/2020

  39. 6.7 Process Scheduling Model——Solaris 2 Scheduling 391/3/2020

  40. Windows 2000 Priorities 401/3/2020

  41. Exercises 6.3 • Consider the following set of processes,with the length of the CPU-burst time given in milliseconds: ProcessBurst TimePriority P1 10 3 P2 1 1 P3 2 3 P4 1 4 P5 5 2 The processes are assumed to have arrived in the order P1,P2,P3,P4,P5, all at time 0. 411/3/2020

  42. Exercises 6.3 • Draw four Gantt charts illustrating the execution of these processes using FCFS,SJF, a nonpreemptive priority, and RR(quantum=1) scheduling. • What is the turnaround time of each process for each of the scheduling algorithms in part a? • What is the waiting time of each process for each of the scheduling algorithms in part a? • Which of the schedules of part a results in the minimal average waiting time(over all processes)? 421/3/2020

  43. Exercises 6.3 431/3/2020

  44. Exercises 6.4 • Suppose that the following processes arrive for execution at the times indicated.Each process will run the listed amount of time. In answering the questions, use nonpreemptive scheduling and base all decisions on the information you have at the time the decision must be made. ProcessArrival TimeBurst Time P1 0.0 8 P2 0.4 4 P3 1.0 1 441/3/2020

  45. Exercises 6.4 • What is the average turnaround time for these processes with the FCFS and the SJF scheduling algorithm? • The SJF algorithm is supposed to improve performance,but notice that we chose to run process P1 at time 0 because we did not know that two shorter processes would arrive soon. Compute what the average turnaround time will be if the CPU is left idle for the first 1 unit and then SJF scheduling is used. Remember that peocesses P1 and P2 are waiting during this idle time,so their waiting time may increase. This algorithm could be known as future-knowledge(预知) scheduling. 451/3/2020

More Related