Linux下線程的調度策略與優先級(二)

上一篇文章介紹了Linux下的調度策略和優先級,在Ubuntu09.10上的一些特性,這裏測試一下其中的兩種特性,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, & param) ; 
  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, & param) ; 
 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, & param) ; 
 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, & param) ; 
 pthread_attr_setinheritsched ( & attr2, PTHREAD_EXPLICIT_SCHED ) ;//要使優先級其作用必須要有這句話 

 param. sched_priority = 21; 
 pthread_attr_setschedpolicy ( & attr1, SCHED_RR) ; 
 pthread_attr_setschedparam ( & attr1, & param) ; 
 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 

  可以看出並沒有每一次都保證高優先級的線程先運行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章