2011年4月5日 星期二

Linux Process 排程-3 (Processes Priority and time slice)

     所謂的Process Priority 指的是Process 能使用CPU的優先順序,以Linux 的系統為例,有兩個值影響Process Priority:

    nice value愈大,表示該process 對系統其它的processes愈"nice",因為它使用CPU的優先權較低.

  real -time priority value ,則與nice value相反,它的值愈大,其優先權愈大。

     一般embedded system中,依功能性的不同,而存在不同優先順序的tasks。通常提供服務的task或者是time critical的task,會給予較高的優先權,以提供使用者或client task 較好的response time 及服務,相對的使用服務的task其Priority 就較低。 

     依不同的系統設計,高Priority value 的task 通常會給予較長的time slice 或是優先執行完畢(非Linux目前作法,後述) 。而當多個task 指定同一個Priority value時,可以採取round-robin的方式,讓每個相同priority value的task輪流分到一個time slice 的CPU 使用時段,repeat cycle.(假設以high priority task 優先執行畢的系統為例)

     其中指的Time slice 就是CPU 執行的duration for given process.這個值如果定義的太大,那系統執行起來就沒有concurrent 執行的感覺,也就是user interactive response會大長,感覺不好,但反之如果設的太短,那花在task context switch 的overhead就會太多,浪費了CPU整體的資源。

     在Linux CFS (Completely Fair Scheduler)的設定下,time slice 並非一個固定值,而是依據正在執行的processes 它們的nice value,來分配各個process 使用CPU的比例(proportion),也就是會依系統的loading 及其對應的priority value 來作time slice 動態的調整。

    在CFS 排程下,一個新加入的process 是否能preempting current process,立即執行,是根據該Process 的priority 及available time slice,而Linux 較新的CFS 中,這個動作的決策,則是依據佔CPU proportion 來決定,例如,原本的系統中有三個processes A(50%) -> B(30%) ->C(20%)->A,假設有個D Process 產生在B執行的期間,那如果D佔CPU proportion會高於B, 那它會稍後執行A->B->D->C-A ,反之,D可以preempting B立即執行,那會變成是A->D->B->C->A。(以上的想法的假設,後續更了解後,如果有誤,再回頭修改)



       In Linux, under the new CFS scheduler, the decision is a function of how much of a proportion of the processor the newly runnable processor has consumed. If it has consumed a smaller proportion of the processor than the currently 
executing process, it runs immediately, preempting the current process. If not, it is scheduled 

to run at a later time.






(from Linux.Kernel.Development.3rd.Edition -Robert Love)


    

沒有留言:

張貼留言