轉載:http://blog.csdn.net/a_ran/article/details/43759729
線程調度間的環境切換
什麼是環境切換。
如果主線程是唯一的線程,那麼他基本上不會被調度出去。另一方面,如果可啟動並執行線程數大於CPU的數量,那麼作業系統最終會將某個正在啟動並執行線程調度出去,從而
使其他線程能夠使用CPU。這將導致一次環境切換。在這個過程中將儲存當前運行線程的執行內容,並將新調度進來的線程的執行內容設定為當前上下文。
切換上下文需要一定的開銷,而線上程調度過程中需要訪問作業系統和JVM共用的資料結構。應用程式、作業系統以及JVM都使用一組相同的CPU。在JVM和作業系統的代碼中消耗越多的CPU刻度,應用程式的可用CPU刻度就越少。但環境切換的開銷並不只是包含JVM和作業系統的開下。當一個新的線程被切換進來時,它所需要額資料可能不在當前處理器的本機快取中,因此環境切換將導致一些緩衝缺失,因而線程在首次調度運行時會更加緩慢。這就是為什麼調度器會每個可啟動並執行線程分配一個最小執行時間,即使有許多其他的線程正在等待執行:它將環境切換的開銷分攤到更多不會中斷的執行時間上,從而提供整體的輸送量(以損失響應性為代價)。
當線程由於等待某個發生競爭的鎖而被阻塞時,JVM通常會將這個線程掛起,並允許它被交換出去。如果線程頻繁的發生阻塞,那麼他們將無法使用完整的調度時間片。在程式中發生越多的阻塞(包括阻塞IO,等待擷取發生競爭的鎖,或者在條件變數上等待),與CPU密集型的程式就會發生越多的環境切換,從而增加調度開銷,並因此降低輸送量。(無阻塞演算法同樣有助於減小環境切換)
環境切換的實際開銷會隨著平台的不同而變化,然而根據經驗來看:在大多數通用的處理器中,環境切換的開銷相當於5000-10000個刻度,也就是幾微秒。
UNIX系統的vmstat命令和Windows系統的perfmon工具都能報告環境切換的次數以及在核心中執行時間所佔比例等資訊。如果核心佔用率較高(超過10%),那麼通常表示調度活動發生的很頻繁,這很可能是由於IO或競爭鎖導致的阻塞引起的。
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 |
可以看出並沒有每一次都保證高優先順序的線程先運行。