资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,October 2013,Operating System Concepts-Chapter 6 CPU Scheduling -,*,Chapter 5CPU Scheduling,An Introduction to Chapter 5,Basic issues about scheduling (5.1),Is a scheduling algorithm good or bad?,scheduling criteria (5.2),evaluation methods (5.7),Several,basic,algorithms/mechanisms (5.3),Scheduling in Multiple-Processor Systems(5.4),Thread scheduling(5.5),Case studies,scheduling in modern OS(5.6):Linux,Solaris 2,Windows XP,October 2013,2,Operating System Concepts-Chapter 6 CPU Scheduling -,5.1 Basic Concepts,5.1.1,CPU,scheduling,The procedure of,selecting,running entities in main memory(i.e.,processes or threads in the ready queue)according to some criteria,allocating,CPU to the selected running entities,and then,enabling,them to run on CPU,selecting+allocating+enabling,October 2013,3,Operating System Concepts-Chapter 6 CPU Scheduling -,5.1 Basic Concepts(cont.),5.1.2 The cycle of process execution,Process execution consists of a cycle of CPU execution and I/O wait,i.e.,CPU burst,and,I/O burst,e.g.Fig.5.1,When a CPU burst occurs,the process is in the,running,state;,When a I/O burst occurs,the process is in the,waiting,state,CPU-burst times distribution,Fig.5.2,October 2013,4,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.1 Alternating Sequence of CPU And I/O Bursts,October 2013,5,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.2 CPU burst distribution histogram of CPU-burst Times,October 2013,6,Operating System Concepts-Chapter 6 CPU Scheduling -,5.1.2 The Cycle of Process Execution(cont.),CPU-bound process might have a few very long CPU bursts,I/O-bound process might have many very short CPU bursts and some long I/O bursts.,October 2013,7,Operating System Concepts-Chapter 6 CPU Scheduling -,5.1.3 Who conduct CPU Scheduling,As components of OS,schedule,r(5.1.2)and,dispatcher,(5.1.4,分配器,调度器),are responsible for CPU,scheduling,The(,short-term,),scheduler,is responsible for CPU scheduling,selects from the processes in memory that are ready to execute,on the basis of,s,cheduling algorithms,allocates the CPU to one of them,/*组织和维护,(,就绪,),进程,/,线程队列,5.1 Basic Concepts(cont.),October 2013,8,Operating System Concepts-Chapter 6 CPU Scheduling -,The,dispatcher,gives control of the CPU to the process selected by the scheduler,i.e.starts the selected process:,process context switching,switching to user mode,jumping to the proper location in the user program to restart that program,5.1.3 Who Conduct CPU Scheduling(cont.),October 2013,9,Operating System Concepts-Chapter 6 CPU Scheduling -,5.1.4 When CPU scheduling occurs!,When CPU is idle or some events occurs in systems(e.g.real-time system),the OS scheduler should take the control of CPU,and then make scheduling decisions,process terminates,the running process switches from running to waiting state(for example,I/O,requests),the running process switches from running to ready state,(e.g.,interrupts,occurs,or the timeslot is out),switches from waiting to ready(for example,completion of I/O,),5.1 Basic Concepts(cont.),October 2013,10,Operating System Concepts-Chapter 6 CPU Scheduling -,Scheduling under condition 1 and 2 is,nonpreemptive,(,非抢占式),because the CPU becomes in idle,All other scheduling is,preemptive(,抢占式),,often used in real-time systems,5.1.5 CPU/mode/context switching in CPU scheduling,CPU scheduling results in CPU switching between user mode and kernel mode.,refer to the figure in next page.,Dispatch latency,(,调度等待时间),time it takes for the,dispatcher and,scheduler,to stop one process and start another running,i.e.,t0,t3 in the figure,5.1 Basic Concepts(cont.),October 2013,11,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.0.1 Illustration of,mode switching,in CPU scheduling,time,P,i,t0,user mode,P,j,or P,i,kernel mode,user mode,t1,t2,t3,:system calls,interrupt handling,etc.,,,under four circumstances(as described in P155-156),:scheduler,:dispatcher,CPU mode switching,5.1.5 Mode switching in CPU scheduling(cont.),October 2013,12,Operating System Concepts-Chapter 6 CPU Scheduling -,5.2 Scheduling Criteria,/*,目标,/,性能指标,/,评价指标,The following criteria are often used to compare or evaluate different CPU scheduling algorithms.,CPU utilization keep the CPU as busy as possible,amount of time during which,user,processes run on CPU,amount of time during which,user and system processes,run on CPU,100%,keeping CPU in user mode as possible,refer to Fig.5.0.1,e.g.,task manager,in Windows OS,October 2013,13,Operating System Concepts-Chapter 6 CPU Scheduling -,5.2 Scheduling Criteria(cont.),throughput(,吞吐量),number of processes that complete their execution,per time unit,throughtput,=number of processes terminated in T1,T2,e.g.4,/length of T1,T2,T1,T1,4,October 2013,14,Operating System Concepts-Chapter 6 CPU Scheduling -,5.2 Scheduling Criteria(cont.),turnaround,time,(周转时间),amount of time taken to execute/complete a particular process,that is,the length of the process lifetime cycle.,refer to,Fig.5.0.2,waiting time the sum of the periods spent waiting in the ready queue,i.e.amount of time a process has been waiting,in the,ready,queue,refer to,Fig.5.0.2,October 2013,15,Operating System Concepts-Chapter 6 CPU Scheduling -,turnaround time,:t0,t8,waiting time,:t0,t1+t3,t4+t6,t7,time,CPU burst,t0,t1,t2,t3,I/O burst,CPU burst,I/O burst,CPU burst,submission,t4,t5,user,request,response,to user,response,output to user,completion,t6,5.2 Scheduling Criteria(cont.),idle,:,ready,state,:,running,states,:,waiting,states,t7,t8,Fig.5.0.2 Lifetime cycle of a process,response,time,October 2013,16,Operating System Concepts-Chapter 6 CPU Scheduling -,response time,(响应时间),in interactive systems,amount of time it takes from when a request was submitted until the first response is produced,the amount of time it takes to start responding,not,the time it takes to output that response,e.g.query request in DBS;Web search;,refer to,Fig.5.0.2,Note,as system level criteria,CPU utilization,and,throughput,are specific to the whole processes in the system,as process level criteria,turnaround time,waiting time,and,response time,are specific to a particular process,5.2 Scheduling Criteria(cont.),October 2013,17,Operating System Concepts-Chapter 6 CPU Scheduling -,Scheduler aims to optimize criteria,!,maximize CPU utilization,maximize throughput,minimize turnaround time,minimize waiting time,minimize response time,5.2 Scheduling Criteria(cont.),October 2013,18,Operating System Concepts-Chapter 6 CPU Scheduling -,E.g.,there are three processes,P,1,P,2,P,3,the CPU bursts of each process are,5.3 Scheduling algorithms,5.3.1 First-Come,First-Served(FCFS)scheduling,Principle,the process that requests the CPU first is allocated the CPU first,Implementation,the,new created process is inserted into a,FIFO ready queue,i.e.inserted into the tail of the ready queue.,scheduler selects,the first,process in the ready queue,October 2013,19,Operating System Concepts-Chapter 6 CPU Scheduling -,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,24,27,30,0,Process,(CPU and I/O)Burst Time,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:,5.3.1 FCFS Scheduling(cont.),October 2013,20,Operating System Concepts-Chapter 6 CPU Scheduling -,suppose that the processes arrive in the order,P,2,P,3,P,1,.,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,because the processes,with shorter bursts,run at first.,P,1,P,3,P,2,6,3,30,0,5.3.1 FCFS Scheduling(cont.),October 2013,21,Operating System Concepts-Chapter 6 CPU Scheduling -,Features,FCFS is,nonpreemptive,the average waiting time under FCFS is generally not minimal,and may vary substantially if the process CPU-burst times vary greatly,FCFS,prefers to,the processes with shorter CPU-burst,5.3.1 FCFS Scheduling(cont.),October 2013,22,Operating System Concepts-Chapter 6 CPU Scheduling -,5.3.2 Shortest-Job-First(SJR)scheduling,Principles,associate with each process the length of its,next CPU burst,.,use these lengths to schedule the process with the shortest time,i.e.,allocate CPU to the process with the,smallest,next CPU burst,if two processes have the same next CPU bursts,FCFS scheduling is used.,Implementation:,nonpreemptive,once CPU given to the process it cannot be preempted until completes its CPU burst.,5.3 Scheduling Algorithms(cont.),October 2013,23,Operating System Concepts-Chapter 6 CPU Scheduling -,preemptive if a new process(,as an external event,)arrives with CPU burst length less than remaining time of current executing process,preempting and scheduling occurs,according to the remaining burst of the preempted process.,preemptive SJF is know as the Shortest-Remaining-Time-First,(SRTF)!,Feature!,with respect to,minimum average waiting time,for a given set of processes,SJF is optimal,5.3.2 Shortest-Job-First Scheduling(cont.),October 2013,24,Operating System Concepts-Chapter 6 CPU Scheduling -,P,1,P,3,P,2,7,3,16,0,P,4,8,12,average waiting time=(0+6+3+7)/4=4,E.g.SJF(non-preemptive),Process,Arrival Time,Burst Time,P1,0.0 7,P2,2.0 4,P3,4.0 1,P4,5.0 4,5.3.2 Shortest-Job-First Scheduling(cont.),October 2013,25,Operating System Concepts-Chapter 6 CPU Scheduling -,E.g.Preemptive SJF,Process Arrival Time,Burst Time,P,1,0.07,P,2,2.04,P,3,4.01,P,4,5.04,5.3.2 Shortest-Job-First scheduling(cont.),October 2013,26,Operating System Concepts-Chapter 6 CPU Scheduling -,P,1,P,3,P,2,4,2,11,0,P,4,5,7,P,2,P,1,16,average waiting time=(9+1+0+2)/4=3,5.3.2 Shortest-Job-First scheduling(cont.),P1:7,P1:5,P2:4,P1:5,P2:2,P3:1,P1:5,P2:2,P3:0,P4:4,P1:5,P2:0,P3:0,P4:4,P1:5,P2:0,P3:0,P4:0,P1:0,P2:0,P3:0,P4:0,October 2013,27,Operating System Concepts-Chapter 6 CPU Scheduling -,How to determining the length of next CPU burst,the,length only be estimated,be,predicted by using the length of previous CPU bursts,using exponential averaging,using the formula,given in page 161 in the textbook.,for more details about exponential averaging prediction,refer to Appendix 5.A,5.3.2 Shortest-Job-First scheduling(cont.),October 2013,28,Operating System Concepts-Chapter 6 CPU Scheduling -,5.3.3 Priority scheduling,Principles,a priority number(integer)is associated with each process,the CPU is allocated to the process with the highest priority(,often,smallest integer,highest priority,),Implementation,preemptive,preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process.,nonpreemptive,insert the new process into the head of the ready queue,and wait for CPU scheduling on the basis of priorities,5.3 Scheduling Algorithms(cont.),October 2013,29,Operating System Concepts-Chapter 6 CPU Scheduling -,Process,Burst Time,Priority,P,1,103,P,2,11,P,3,24,P,4,15,P,5,5 2,An Example,P,2,P,5,1,0,6,P,1,16,P,3,1,8,P,4,1,9,October 2013,30,Operating System Concepts-Chapter 6 CPU Scheduling -,How to determine the priority,defined internally,defined externally,Types of priority,static priority:the priority remains unchanged during process lifetime,dynamic priority:the priority may vary,5.3.3 Priority Scheduling(cont.),October 2013,31,Operating System Concepts-Chapter 6 CPU Scheduling -,Features,commonly used in modern operating systems,may be indefinite blocking/starvation,FCFS and SJF can be viewed as priority-based scheduling,in FCFS,the priority is the waiting time of the process from its being created,in SJF,the priority is the predicted next CPU burst time,5.3.3 Priority Scheduling(cont.),October 2013,32,Operating System Concepts-Chapter 6 CPU Scheduling -,Starvation,Problem,low priority processes may never execute.,solution,aging as time progresses increase the priority of the process,i.e.,Dynamic priority,is adopted.,E.g1.Priority-based scheduling in Unix,for more details,refer to,Appendix B,Priority-based scheduling in Unix,E.g 2.CPU scheduling in Linux,refer to,Linux-6-process scheduling in Linux as Case Studies,5.3.3 Priority Scheduling(cont.),October 2013,33,Operating System Concepts-Chapter 6 CPU Scheduling -,5.3.4 Round robin(RR,,时间片轮转,),Principle.,CPU time is divided into a series of small time units,called,time quantum(,量子,)or time slice(,时间片),each process in ready queues is allocated a,time slice,usually 10-100 milliseconds,and begins to run on CPU.,the scheduler,selects,the first process(head process,)in the ready queue,this process is allocated a CPU,time slice,after this time slice has elapsed,the process is preempted and added to,the tail,of the ready queue,and the CPU is allocated to the next process in the ready queue.,Features,typically,higher average,turnaround,than SJF,but better,response time!,commonly used in time-sharing systems,5.3 Scheduling Algorithms(cont.),October 2013,34,Operating System Concepts-Chapter 6 CPU Scheduling -,E.g.RR with Time Quantum=20,Process,Burst Time,P,1,53,P,2,17,P,3,68,P,4,24,The Gantt chart is,P,1,P,2,P,3,P,4,P,1,P,3,P,4,P,1,P,3,P,3,0,20,37,57,77,97,117,121,134,154,162,5.3.4 Round Robin(cont.),October 2013,35,Operating System Concepts-Chapter 6 CPU Scheduling -,/*了解:,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.,refer to the next two pages,5.3.4 Round Robin(cont.),October 2013,36,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.4 Time Quantum and Context Switch Time,October 2013,37,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.5 Turnaround Time Varies With The Time Quantum,October 2013,38,Operating System Concepts-Chapter 6 CPU Scheduling -,5.3.5 Multilevel queue scheduling,Principles:two-level scheduling,as an example,the ready queue is partitioned into separate queues,for example,foreground(interactive),background(batch)Performance,each queue has its own scheduling algorithm,foreground RR,background FCFS,scheduling is done among the queues at first,the selected queue is then scheduled among its processes,5.3 Scheduling Algorithms(cont.),October 2013,39,Operating System Concepts-Chapter 6 CPU Scheduling -,Implementation:for queue-level scheduling,fixed priority scheduling for scheduling among queues(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,refer to Fig.5.6,5.3.5 Multilevel Queue Scheduling(cont.),October 2013,40,Operating System Concepts-Chapter 6 CPU Scheduling -,Fig.5.6 Multilevel Queue Scheduling,October 2013,41,Operating System Concepts-Chapter 6 CPU Scheduling -,5.3.6 Multilevel feedback queue,Principles,a process can move between the various queues;aging can be implemented in this way,multilevel-feedbac
展开阅读全文