Linux 線程調度與優先級

【轉】
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; //設置優先級
html

   系統建立線程時,默認的線程是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,&param);
  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 的狀況。ui

#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;
}

spa

下面是該程序的其中之一的運行結果:.net

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),並且用完了它的時間片。
   基於時間片輪轉的實時進程是,不是真正的改變進程的優先級,而是改變進程的基本時間片的長度。因此基於時間片輪轉的進程調度,並不能保證高優先級的進程先運行。
   下面是另外一種運行結果:

unix

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

  能夠看出並無每一次都保證高優先級的線程先運行。

相關文章
相關標籤/搜索