一文帶你詳解Linux線程調(diào)度與優(yōu)先級(jí)(含代碼測試)
Linux內(nèi)核的三種調(diào)度策略:
SCHED_OTHER 分時(shí)調(diào)度策略,
SCHED_FIFO實(shí)時(shí)調(diào)度策略,先到先服務(wù)。一旦占用cpu則一直運(yùn)行。一直運(yùn)行直到有更高優(yōu)先級(jí)任務(wù)到達(dá)或自己放棄
SCHED_RR實(shí)時(shí)調(diào)度策略,時(shí)間片輪轉(zhuǎn)。當(dāng)進(jìn)程的時(shí)間片用完,系統(tǒng)將重新分配時(shí)間片,并置于就緒隊(duì)列尾。放在隊(duì)列尾保證了所有具有相同優(yōu)先級(jí)的RR任務(wù)的調(diào)度公平
Linux線程優(yōu)先級(jí)設(shè)置
首先,可以通過以下兩個(gè)函數(shù)來獲得線程可以設(shè)置的最高和最低優(yōu)先級(jí),函數(shù)中的策略即上述三種策略的宏定義:
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
SCHED_OTHER是不支持優(yōu)先級(jí)使用的,而SCHED_FIFO和SCHED_RR支持優(yōu)先級(jí)的使用,他們分別為1和99,數(shù)值越大優(yōu)先級(jí)越高。
設(shè)置和獲取優(yōu)先級(jí)通過以下兩個(gè)函數(shù)
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; //設(shè)置優(yōu)先級(jí)
系統(tǒng)創(chuàng)建線程時(shí),默認(rèn)的線程是SCHED_OTHER。所以如果我們要改變線程的調(diào)度策略的話,可以通過下面的這個(gè)函數(shù)實(shí)現(xiàn)。
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
上面的param使用了下面的這個(gè)數(shù)據(jù)結(jié)構(gòu):
struct sched_param
{
? ?int __sched_priority; //所要設(shè)定的線程優(yōu)先級(jí)
};
【文章福利】小編推薦自己的Linux內(nèi)核技術(shù)交流群:【891587639】整理了一些個(gè)人覺得比較好的學(xué)習(xí)書籍、視頻資料共享在群文件里面,有需要的可以自行添加哦?。。∏?00名進(jìn)群領(lǐng)取,額外贈(zèng)送一份價(jià)值699的內(nèi)核資料包(含視頻教程、電子書、實(shí)戰(zhàn)項(xiàng)目及代碼)?

我們可以通過下面的測試程序來說明,我們自己使用的系統(tǒng)的支持的優(yōu)先級(jí):
#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;
}
下面是測試程序的運(yùn)行結(jié)果:
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,還有就是優(yōu)先級(jí)的問題,是不是能夠保證,高優(yōu)先級(jí)的線程,就可以保證先運(yùn)行。
下面的這個(gè)測試程序,創(chuàng)建了三個(gè)線程,默認(rèn)創(chuàng)建的線程的調(diào)度策略是SCHED_OTHER,其余的兩個(gè)線程的調(diào)度策略設(shè)置成SCHED_RR。我的Linux的內(nèi)核版本是2.6.31。SCHED_RR是根據(jù)時(shí)間片來確定線程的調(diào)度。時(shí)間片用完了,不管這個(gè)線程的優(yōu)先級(jí)有多高都不會(huì)在運(yùn)行,而是進(jìn)入就緒隊(duì)列中,等待下一個(gè)時(shí)間片的到了,那這個(gè)時(shí)間片到底要持續(xù)多長時(shí)間?在《深入理解Linux內(nèi)核》中的第七章進(jìn)程調(diào)度中,是這樣描訴的,Linux采取單憑經(jīng)驗(yàn)的方法,即選擇盡可能長、同時(shí)能保持良好相應(yīng)時(shí)間的一個(gè)時(shí)間片。這里也沒有給出一個(gè)具體的時(shí)間來,可能會(huì)根據(jù)不同的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);//要使優(yōu)先級(jí)其作用必須要有這句話
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;
}
下面是該程序的其中之一的運(yùn)行結(jié)果:
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的調(diào)度策略是SCHED_OTHER,而線程2的調(diào)度策略是SCHED_RR,所以,在Thread3中,線程3被線程1,線程2給搶占了。由于線程1的優(yōu)先級(jí)大于線程2的優(yōu)先級(jí),所以,在線程1以先于線程2運(yùn)行,不過,這里線程2有一部分代碼還是先于線程1運(yùn)行了。
我原以為,只要線程的優(yōu)先級(jí)高,就會(huì)一定先運(yùn)行,其實(shí),這樣的理解是片面的,特別是在SMP的PC機(jī)上更會(huì)增加其不確定性。
其實(shí),普通進(jìn)程的調(diào)度,是CPU根據(jù)進(jìn)程優(yōu)先級(jí)算出時(shí)間片,這樣并不能一定保證高優(yōu)先級(jí)的進(jìn)程一定先運(yùn)行,只不過和優(yōu)先級(jí)低的進(jìn)程相比,通常優(yōu)先級(jí)較高的進(jìn)程獲得的CPU時(shí)間片會(huì)更長而已。其實(shí),如果要想保證一個(gè)線程運(yùn)行完在運(yùn)行另一個(gè)線程的話,就要使用多線程的同步技術(shù),信號(hào)量,條件變量等方法。而不是絕對依靠優(yōu)先級(jí)的高低,來保證。
不過,從運(yùn)行的結(jié)果上,我們可以看到,調(diào)度策略為SCHED_RR的線程1,線程2確實(shí)搶占了調(diào)度策略為SCHED_OTHER的線程3。這個(gè)是可以理解的,由于SCHER_RR是實(shí)時(shí)調(diào)度策略。
? 只有在下述事件之一發(fā)生時(shí),實(shí)時(shí)進(jìn)程才會(huì)被另外一個(gè)進(jìn)程取代。
進(jìn)程被另外一個(gè)具有更高實(shí)時(shí)優(yōu)先級(jí)的實(shí)時(shí)進(jìn)程搶占。
進(jìn)程執(zhí)行了阻塞操作并進(jìn)入睡眠
進(jìn)程停止(處于TASK_STOPPED 或TASK_TRACED狀態(tài))或被殺死。
進(jìn)程通過調(diào)用系統(tǒng)調(diào)用sched_yield(),自愿放棄CPU 。
進(jìn)程基于時(shí)間片輪轉(zhuǎn)的實(shí)時(shí)進(jìn)程(SCHED_RR),而且用完了它的時(shí)間片。
基于時(shí)間片輪轉(zhuǎn)的實(shí)時(shí)進(jìn)程是,不是真正的改變進(jìn)程的優(yōu)先級(jí),而是改變進(jìn)程的基本時(shí)間片的長度。所以基于時(shí)間片輪轉(zhuǎn)的進(jìn)程調(diào)度,并不能保證高優(yōu)先級(jí)的進(jìn)程先運(yùn)行。
下面是另一種運(yùn)行結(jié)果:
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
可以看出并沒有每一次都保證高優(yōu)先級(jí)的線程先運(yùn)行。
