linux线程的优先级设置

来源:互联网 发布:中国裁军30万 知乎 编辑:程序博客网 时间:2024/05/18 09:17

如何在linux/unix中设置线程的优先级


int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
来创建线程,但是如何设置线程的优先级呢?
在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);函数来获取所
使用的调度策略,它们是:SCHED_FIFO, SCHED_RR 和 SCHED_OTHER。

我们可以使用
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
来获取线程线程可是设置的最大和最小的优先级值,如果调用成功就返回最大和最小的优先级值,否则返回-1。
从我现在运行的linux系统中,我使用下列程序(程序见附录)获取了对应三种调度策略中的最大和最小优先级:
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 = 0
Set thread policy
Set SCHED_FIFO policy
policy = SCHED_FIFO
Set SCHED_RR policy
policy = SCHED_RR
Restore current policy
policy = SCHED_OTHER

我们可以看到
SCHED_OTHER 是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。从上面的结果我们可以看 出,如果程序控制线程的优先级,一般是用pthread_attr_getschedpolicy来获取系统使用的调度策略,如果是 SCHED_OTHER的话,表明当前策略不支持线程优先级的使用,否则可以。当然所设定的优先级范围必须在最大和最小值之间。我们可以通过 sched_get_priority_max和sched_get_priority_min来获取。

可能网友会问,是否我们可以通过 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);来设定自己所需的调度策略呢?我觉得是完全可以的(有些系统需要定义 _POSIX_THREAD_PRIORITY_SCHEDULING),只要系统实现了对应的调用策略。

说了半天,我们还没有说,在系统允许使用线程优先级别的时候,如何设置优先级别呢?
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);
上面两个函数分别用于设置线程的优先级,struct sched_param的定义如下
struct sched_param
{
int __sched_priority; //所要设定的线程优先级
};

例:创建优先级为10的线程
pthread_attr_t   attr; 
struct   sched_param   param; 
pthread_attr_init(&attr); 
pthread_attr_setschedpolicy(&attr,   SCHED_RR); 
param.sched_priority   =   10; 
pthread_attr_setschedparam(&attr,   &param); 
pthread_create(xxx   ,   &attr   ,   xxx   ,   xxx); 
pthread_attr_destroy(&attr);  

linux并不是实时操作系统,把下面的代码运行一遍就能够理解了,代码有很详细的注释。

[cpp] view plaincopyprint?
  1. #include    <stdio.h>   
  2. #include    <stdlib.h>   
  3. #include    <unistd.h>   
  4. #include    <pthread.h>   
  5. #include    <signal.h>   
  6. #include    <string.h>   
  7. void * thr_fun(void *arg)  
  8. {  
  9.     int policy, ret;  
  10.     struct sched_param param;  
  11.     //获取线程调度参数   
  12.     ret = pthread_getschedparam(pthread_self(), &policy, &param);  
  13.     if(ret!=0)  
  14.     {  
  15.         printf("pthread_getschedparam %s/n", strerror(ret) );  
  16.         exit(1);  
  17.     }  
  18.     if (policy == SCHED_FIFO)  
  19.     {  
  20.         printf("policy:SCHED_FIFO/n");  
  21.     }  
  22.     else if (policy == SCHED_OTHER)  
  23.     {  
  24.         printf("policy:SCHED_OTHER/n");  
  25.     }  
  26.     else if (policy == SCHED_RR)  
  27.     {  
  28.         printf("policy:SCHED_RR/n");  
  29.     }  
  30.     printf("param:%d/n", param.sched_priority);   
  31.     long long i;  
  32.     while (1) {  
  33.         i++;  
  34.         i *= 2;  
  35.     }  
  36.     pthread_exit(NULL);   
  37. }  
  38. int main(void)  
  39. {  
  40.     int ret;  
  41.     pthread_t tid;  
  42.     pthread_attr_t attr;  
  43.     int policy, inher;  
  44.     struct sched_param param;  
  45.       
  46.     //初始化线程属性   
  47.     pthread_attr_init(&attr);  
  48.     //获取继承的调度策略   
  49.     ret = pthread_attr_getinheritsched(&attr, &inher);  
  50.     if (ret!=0)  
  51.     {  
  52.         printf("pthread_attr_getinheritsched/n%s/n", strerror(ret));  
  53.         exit(1);  
  54.     }  
  55.     //   
  56.     if (inher == PTHREAD_EXPLICIT_SCHED)   
  57.     {  
  58.         printf("PTHREAD_EXPLICIT_SCHED/n");  
  59.     }  
  60.     else if (inher == PTHREAD_INHERIT_SCHED)   
  61.     {     
  62.         printf("PTHREAD_INHERIT_SCHED/n");  
  63.         inher = PTHREAD_EXPLICIT_SCHED;  
  64.     }  
  65.     //设置继承的调度策略   
  66.     //必需设置inher的属性为 PTHREAD_EXPLICIT_SCHED,否则设置线程的优先级会被忽略  
  67.     ret = pthread_attr_setinheritsched(&attr, inher);  
  68.     if (ret!=0)  
  69.     {  
  70.         printf("pthread_attr_setinheritsched/n%s/n", strerror(ret));  
  71.         exit(1);  
  72.     }  
  73.       
  74.     policy = SCHED_FIFO;//在Ubuntu9.10上需要root权限  
  75.     //设置线程调度策略   
  76.     ret = pthread_attr_setschedpolicy(&attr, policy);  
  77.     if (ret!=0)  
  78.     {  
  79.         printf(" pthread_attr_setschedpolicy/n%s/n", strerror(ret));  
  80.         exit(1);  
  81.     }  
  82.     param.sched_priority = 3;  
  83.     //设置调度参数   
  84.     ret = pthread_attr_setschedparam(&attr, &param);  
  85.     if (ret!=0)  
  86.     {  
  87.         printf(" pthread_attr_setschedparam/n%s/n", strerror(ret));  
  88.         exit(1);  
  89.     }  
  90.     //创建线程   
  91.     ret = pthread_create(&tid, &attr, thr_fun, NULL);  
  92.     if (ret!=0)  
  93.     {  
  94.         printf("pthread_create/n%s/n", strerror(ret));  
  95.         exit(1);  
  96.     }  
  97.     while (1) {  
  98.         printf("hello world/n");  
  99.     }  
  100.     pthread_join(tid, NULL);  
  101.     pthread_exit(NULL);  
  102. }  

线程中不要用perror,请使用strerror。

原创粉丝点击