设为首页 加入收藏

TOP

6.7.3 设置线程调度和优先级
2013-10-07 12:59:16 来源: 作者: 【 】 浏览:55
Tags:6.7.3 设置 线程 调度 优先

6.7.3  设置线程调度和优先级

线程是独立执行的。它们被分派到处理器内核上,并执行分给它们的任务。每个线程均有一个调度策略和优先级,决定何时以及如何分配到处理器上。线程或线程组的调度策略可以使用这些函数通过属性对象来设置:

调用形式

  1. #include <pthread.h> 
  2. #include <sched.h> 
  3.  
  4. int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);  
  5. void pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);  
  6. int pthread_attr_setschedparam(pthread_attr_t *restrict attr,  
  7.                          const struct sched_param *restrict param); 

pthread_attr_setinheritesched( )用于确定如何设置线程的调度属性,可以从创建者线程或从一个属性对象来继承调度属性。inheritsched可以为如下值。

PTHREAD_INHERIT_SCHED:线程调度属性是从创建者线程继承得到,attr的任何调度属性都被忽略。

PTHREAD_EXPLICIT_SCHED:线程调度属性设置为属性对象attr的调度属性。

如果inheritsched值是PTHREAD_EXPLICIT_SCHED,则pthread_attr_setschedpolicy( )被用于设置调度策略,而pthread_attr_setschedparam( )被用于设置优先级。

pthread_attr_setschedpolicy( )设置线程属性对象attr的调度策略。policy的值可以为在<sched.h>头文件中定义的以下值。

SCHED_FIFO:先进先出调度策略,执行线程运行到结束。

SCHED_RR:轮询调度策略,按照时间片将每个线程分配到处理器上。

SCHED_OTHER:另外的调度策略(根据实现定义)。这是任何新创建线程的默认调度策略。

使用pthread_attr_setschedparam( )可以设置调度策略所使用的属性对象attr的调度参数。param是包含参数的结构体。sched_param结构体至少需要定义这个数据成员:

  1. struct sched_param {  
  2.    int sched_priority;  
  3.    //...  
  4. }; 

它可能还有其他的数据成员,以及多个用来返回和设置最小优先级、最大优先级、调度器、参数等的函数。如果调度策略是SCHED_FIFO或SCHED_RR,那么要求具有值的唯一成员是sched_priority。

按照如下方法使用sched_get_priority_max( )和sched_get_priority_max( ),可以得到优先级的最大值和最小值。

调用形式

  1. #include <sched.h> 
  2.  
  3. int sched_get_priority_max(int policy);  
  4. int sched_get_priority_min(int policy); 

两个函数都以调度策略policy为参数,目的是获得对应调度策略的优先级值,而且都返回调度策略的最大或最小优先级值。

示例6-10显示了如何使用线程属性对象设置线程的调度策略和优先级。

示例6-10

  1. // Example 6-10 Using the thread attribute object to set scheduling  
  2. // policy and priority of a thread.  
  3.  
  4. #include <pthread.h> 
  5. #include <sched.h> 
  6.  
  7. //...  
  8.  
  9. pthread_t ThreadA;  
  10. pthread_attr_t SchedAttr;  
  11. sched_param SchedParam;  
  12. int MidPriority,MaxPriority,MinPriority;  
  13.  
  14. int main(int argc, char *argv[])  
  15. {  
  16.    //...  
  17.  
  18.    // Step 1: initialize attribute object  
  19.    pthread_attr_init(&SchedAttr);  
  20.  
  21.    // Step 2: retrieve min and max priority values for scheduling policy  
  22.    MinPriority = sched_get_priority_max(SCHED_RR);  
  23.    MaxPriority = sched_get_priority_min(SCHED_RR);  
  24.  
  25.    // Step 3: calculate priority value  
  26.    MidPriority = (MaxPriority + MinPriority)/2;  
  27.  
  28.    // Step 4: assign priority value to sched_param structure  
  29.    SchedParam.sched_priority = MidPriority;  
  30.  
  31.    // Step 5: set attribute object with scheduling parameter  
  32.    pthread_attr_setschedparam(&SchedAttr,&SchedParam);  
  33.  
  34.    // Step 6: set scheduling attributes to be determined by attribute object  
  35.    pthread_attr_setinheritsched(&SchedAttr,PTHREAD_EXPLICIT_SCHED);  
  36.  
  37.    // Step 7: set scheduling policy  
  38.    pthread_attr_setschedpolicy(&SchedAttr,SCHED_RR);  
  39.  
  40.    // Step 8: create thread with scheduling attribute object  
  41.    pthread_create(&ThreadA,&SchedAttr,task1,NULL);  
  42.  
  43.    //...  

在示例6-10中,ThreadA的调度策略和优先级是使用线程属性对象SchedAttr来设置的。通过8个步骤完成:

(1) 初始化属性对象

(2) 为调度策略提取最大和最小优先级值

(3) 计算优先级值

(4) 将优先级值赋给sched_param结构体

(5) 使用调度参数设置属性对象

(6) 将调度属性设置为由属性对象决定

(7) 设置调度策略

(8) 使用调度属性对象创建一个线程

在示例6-10中,我们将优先级设置为一个平均值。但是优先级可以设置为介于线程调度策略所允许的最大和最小优先级值之间的任何值。有了这些方法,调度策略和优先级可以在线程被创建或运行之前,先设置在线程属性对象中。为了动态改变调度策略和优先级,可以使用pthread_setschedparam( )和pthread_setschedprio( )。

调用形式

  1. #include <pthread.h> 
  2.  
  3. int pthread_setschedparam(pthread_t thread, int policy,  
  4.                           const struct sched_param *param);  
  5. int pthread_getschedparam(pthread_t thread, int *restrict policy,  
  6.                           struct sched_param *restrict param);  
  7. int pthread_setschedprio(pthread_t thread, int prio); 

pthread_setschedparam( )不需要使用属性对象即可直接设置线程的调度策略和优先级。thread是线程的id,policy是新的调度策略,param包含调度优先级。如果成功,则pthread_getschedparam( )返回调度策略和调度参数,并将它们的值分别保存在policy和param参数中。如果成功,则两个函数都返回0。如果不成功,两个函数都返回错误号。表6-7列出了这些函数可能失败的条件。

pthread_setschedprio( )用来设置正在执行中的线程的调度优先级,该进程的id由thread指定。prio指定了线程的新调度优先级。如果函数失败,线程的优先级不发生变化,返回一个错误号。如果成功,则函数返回0。表6-7也列出了这个函数可能失败的条件。

表6-7

pthread调度和优先级函数< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

失败的条件

int pthread_getschedparam

(pthread_t thread,

int *restrict policy, struct

sched_param *restrict param);

thread参数所指向的线程不存在

int pthread_setschedparam

(pthread_t thread,

int *policy, const

struct sched_param *param);

参数policy或同参数policy

关联的调度参数之一无效;

参数policy或调度参数之一的值不被支持;

调用线程没有适当的权限来设

置指定线程的调度参数或策略;

参数thread指向的线程不存在;

实现不允许应用程序将参数

改动为特定的值

int pthread_setschedprio

(pthread_t thread, int prio);

参数prio对于指定线程的调度策略无效;

参数prio的值不被支持;

调用线程没有适当的权限来设

置指定线程的调度优先级;

参数thread指向的线程不存在;

实现不允许应用程序将优先

级改变为指定的值


注意:

要记得仔细考虑为何有必要改变运行线程的调度策略或优先级。这可能会严重影响应用程序的总体性能。有着较高优先级的线程会抢占运行的较低优先级的线程。这可能会产生饿死,即线程持续被抢占,因此无法完成执行。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇6.4.1 线程状态 下一篇6.4.3 调度策略和优先级

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: