【轉】
Linux內核的三種調度策略:
1,SCHED_OTHER 分時調度策略,
2,SCHED_FIFO實時調度策略,先到先服務。一旦占用cpu則一直運行。一直運行直到有更高優先級任務到達或自己放棄
3,SCHED_RR實時調度策略,時間片輪轉。當進程的時間片用完,系統將重新分配時間片,並置於就緒隊列尾。放在隊列尾保證了所有具有相同優先級的RR任務的調度公平
Linux線程優先級設置
首先,可以通過以下兩個函數來獲得線程可以設置的最高和最低優先級,函數中的策略即上述三種策略的宏定義:
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
SCHED_OTHER是不支持優先級使用的,而SCHED_FIFO和SCHED_RR支持優先級的使用,他們分別為1和99,數值越大優先級越高。
設置和獲取優先級通過以下兩個函數
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; //設置優先級
|
系統創建線程時,默認的線程是SCHED_OTHER。所以如果我們要改變線程的調度策略的話,可以通過下面的這個函數實現。
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); |
上面的param使用了下面的這個數據結構:
struct sched_param { int __sched_priority; //所要設定的線程優先級 }; |
我們可以通過下面的測試程序來說明,我們自己使用的系統的支持的優先級:
#include <stdio.h> #include <pthread.h> #include <sched.h> #include <assert.h>
static int get_thread_policy(pthread_attr_t *attr) { int policy; int rs = pthread_attr_getschedpolicy(attr,&policy); assert(rs==0); switch(policy) { case SCHED_FIFO: printf("policy= SCHED_FIFO\n"); break; case SCHED_RR: printf("policy= SCHED_RR"); break; case SCHED_OTHER: printf("policy=SCHED_OTHER\n"); break; default: printf("policy=UNKNOWN\n"); break; } return policy; }
static void show_thread_priority(pthread_attr_t *attr,int policy) { int priority = sched_get_priority_max(policy); assert(priority!=-1); printf("max_priority=%d\n",priority); priority= sched_get_priority_min(policy); assert(priority!=-1); printf("min_priority=%d\n",priority); }
static int get_thread_priority(pthread_attr_t *attr) { struct sched_param param; int rs = pthread_attr_getschedparam(attr,¶m); assert(rs==0); printf("priority=%d",param.__sched_priority); return param.__sched_priority; }
static void set_thread_policy(pthread_attr_t *attr,int policy) { int rs = pthread_attr_setschedpolicy(attr,policy); assert(rs==0); get_thread_policy(attr); }
int main(void) { pthread_attr_t attr; struct sched_param sched; int rs; rs = pthread_attr_init(&attr); assert(rs==0);
int policy = get_thread_policy(&attr); printf("Show current configuration of priority\n"); show_thread_priority(&attr,policy); printf("show SCHED_FIFO of priority\n"); show_thread_priority(&attr,SCHED_FIFO); printf("show SCHED_RR of priority\n"); show_thread_priority(&attr,SCHED_RR); printf("show priority of current thread\n"); int priority = get_thread_priority(&attr);
printf("Set thread policy\n"); printf("set SCHED_FIFO policy\n"); set_thread_policy(&attr,SCHED_FIFO); printf("set SCHED_RR policy\n"); set_thread_policy(&attr,SCHED_RR); printf("Restore current policy\n"); set_thread_policy(&attr,policy);
rs = pthread_attr_destroy(&attr); assert(rs==0); return 0; }
|
下面是測試程序的運行結果:
policy=SCHED_OTHER Show current configuration of priority max_priority=0 min_priority=0 show SCHED_FIFO of priority max_priority=99 min_priority=1 show SCHED_RR of priority max_priority=99 min_priority=1 show priority of current thread priority=0Set thread policy set SCHED_FIFO policy policy= SCHED_FIFO set SCHED_RR policy policy= SCHED_RRRestore current policy policy=SCHED_OTHER
|
這里測試一下其中的兩種特性,SCHED_OTHER和SCHED_RR,還有就是優先級的問題,是不是能夠保證,高優先級的線程,就可以保證先運行。
下面的這個測試程序,創建了三個線程,默認創建的線程的調度策略是SCHED_OTHER,其余的兩個線程的調度策略設置成SCHED_RR。我的Linux的內核版本是2.6.31。SCHED_RR是根據時間片來確定線程的調度。時間片用完了,不管這個線程的優先級有多高都不會在運行,而是進入就緒隊列中,等待下一個時間片的到了,那這個時間片到底要持續多長時間?在《深入理解Linux內核》中的第七章進程調度中,是這樣描訴的,Linux采取單憑經驗的方法,即選擇盡可能長、同時能保持良好相應時間的一個時間片。這里也沒有給出一個具體的時間來,可能會根據不同的CPU 來定,還有就是多CPU 的情況。
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h>
void Thread1() { sleep(1); int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER\n"); if(policy == SCHED_RR); printf("SCHED_RR 1 \n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO\n");
for(i=1;i<10;i++) { for(j=1;j<5000000;j++) { } printf("thread 1\n"); } printf("Pthread 1 exit\n"); }
void Thread2() { sleep(1); int i,j,m; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER\n"); if(policy == SCHED_RR); printf("SCHED_RR\n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO\n");
for(i=1;i<10;i++) { for(j=1;j<5000000;j++) { } printf("thread 2\n"); } printf("Pthread 2 exit\n"); }
void Thread3() { sleep(1); int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER\n"); if(policy == SCHED_RR) printf("SCHED_RR \n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO\n");
for(i=1;i<10;i++) { for(j=1;j<5000000;j++) { } printf("thread 3\n"); } printf("Pthread 3 exit\n"); }
int main() { int i; i = getuid(); if(i==0) printf("The current user is root\n"); else printf("The current user is not root\n");
pthread_t ppid1,ppid2,ppid3; struct sched_param param;
pthread_attr_t attr,attr1,attr2; pthread_attr_init(&attr1); pthread_attr_init(&attr); pthread_attr_init(&attr2); param.sched_priority = 51;
pthread_attr_setschedpolicy(&attr2,SCHED_RR); pthread_attr_setschedparam(&attr2,¶m); pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使優先級其作用必須要有這句話
param.sched_priority = 21; pthread_attr_setschedpolicy(&attr1,SCHED_RR); pthread_attr_setschedparam(&attr1,¶m); pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED); pthread_create(&ppid3,&attr,(void *)Thread3,NULL); pthread_create(&ppid2,&attr1,(void *)Thread2,NULL); pthread_create(&ppid1,&attr2,(void *)Thread1,NULL); pthread_join(ppid3,NULL); pthread_join(ppid2,NULL); pthread_join(ppid1,NULL); pthread_attr_destroy(&attr2); pthread_attr_destroy(&attr1); return 0; }
|
下面是該程序的其中之一的運行結果:
sudo ./prio_test The current user is root SCHED_OTHER SCHED_RR SCHED_RR 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 Pthread 1 exit thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 Pthread 2 exit thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 Pthread 3 exit |
這里我們可以看到,由於線程3的調度策略是SCHED_OTHER,而線程2的調度策略是SCHED_RR,所以,在Thread3中,線程3被線程1,線程2給搶占了。由於線程1的優先級大於線程2的優先級,所以,在線程1以先於線程2運行,不過,這里線程2有一部分代碼還是先於線程1運行了。
我原以為,只要線程的優先級高,就會一定先運行,其實,這樣的理解是片面的,特別是在SMP的PC機上更會增加其不確定性。
其實,普通進程的調度,是CPU根據進程優先級算出時間片,這樣並不能一定保證高優先級的進程一定先運行,只不過和優先級低的進程相比,通常優先級較高的進程獲得的CPU時間片會更長而已。其實,如果要想保證一個線程運行完在運行另一個線程的話,就要使用多線程的同步技術,信號量,條件變量等方法。而不是絕對依靠優先級的高低,來保證。
不過,從運行的結果上,我們可以看到,調度策略為SCHED_RR的線程1,線程2確實搶占了調度策略為SCHED_OTHER的線程3。這個是可以理解的,由於SCHER_RR是實時調度策略。
只有在下述事件之一發生時,實時進程才會被另外一個進程取代。
(1) 進程被另外一個具有更高實時優先級的實時進程搶占。
(2) 進程執行了阻塞操作並進入睡眠
(3)進程停止(處於TASK_STOPPED 或TASK_TRACED狀態)或被殺死。
(4)進程通過調用系統調用sched_yield(),自願放棄CPU 。
(5)進程基於時間片輪轉的實時進程(SCHED_RR),而且用完了它的時間片。
基於時間片輪轉的實時進程是,不是真正的改變進程的優先級,而是改變進程的基本時間片的長度。所以基於時間片輪轉的進程調度,並不能保證高優先級的進程先運行。
下面是另一種運行結果:
sudo ./prio_test The current user is root SCHED_OTHER SCHED_RR 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 thread 1 Pthread 1 exit SCHED_RR thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 thread 2 Pthread 2 exit thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 thread 3 Pthread 3 exit
|
可以看出並沒有每一次都保證高優先級的線程先運行。