6.7.3 设置线程调度和优先级
线程是独立执行的。它们被分派到处理器内核上,并执行分给它们的任务。每个线程均有一个调度策略和优先级,决定何时以及如何分配到处理器上。线程或线程组的调度策略可以使用这些函数通过属性对象来设置:
调用形式
- #include <pthread.h>
- #include <sched.h>
-
- int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
- void pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
- int pthread_attr_setschedparam(pthread_attr_t *restrict attr,
- 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结构体至少需要定义这个数据成员:
- struct sched_param {
- int sched_priority;
- //...
- };
它可能还有其他的数据成员,以及多个用来返回和设置最小优先级、最大优先级、调度器、参数等的函数。如果调度策略是SCHED_FIFO或SCHED_RR,那么要求具有值的唯一成员是sched_priority。
按照如下方法使用sched_get_priority_max( )和sched_get_priority_max( ),可以得到优先级的最大值和最小值。
调用形式
- #include <sched.h>
-
- int sched_get_priority_max(int policy);
- int sched_get_priority_min(int policy);
两个函数都以调度策略policy为参数,目的是获得对应调度策略的优先级值,而且都返回调度策略的最大或最小优先级值。
示例6-10显示了如何使用线程属性对象设置线程的调度策略和优先级。
示例6-10
- // Example 6-10 Using the thread attribute object to set scheduling
- // policy and priority of a thread.
-
- #include <pthread.h>
- #include <sched.h>
-
- //...
-
- pthread_t ThreadA;
- pthread_attr_t SchedAttr;
- sched_param SchedParam;
- int MidPriority,MaxPriority,MinPriority;
-
- int main(int argc, char *argv[])
- {
- //...
-
- // Step 1: initialize attribute object
- pthread_attr_init(&SchedAttr);
-
- // Step 2: retrieve min and max priority values for scheduling policy
- MinPriority = sched_get_priority_max(SCHED_RR);
- MaxPriority = sched_get_priority_min(SCHED_RR);
-
- // Step 3: calculate priority value
- MidPriority = (MaxPriority + MinPriority)/2;
-
- // Step 4: assign priority value to sched_param structure
- SchedParam.sched_priority = MidPriority;
-
- // Step 5: set attribute object with scheduling parameter
- pthread_attr_setschedparam(&SchedAttr,&SchedParam);
-
- // Step 6: set scheduling attributes to be determined by attribute object
- pthread_attr_setinheritsched(&SchedAttr,PTHREAD_EXPLICIT_SCHED);
-
- // Step 7: set scheduling policy
- pthread_attr_setschedpolicy(&SchedAttr,SCHED_RR);
-
- // Step 8: create thread with scheduling attribute object
- pthread_create(&ThreadA,&SchedAttr,task1,NULL);
-
- //...
- }
在示例6-10中,ThreadA的调度策略和优先级是使用线程属性对象SchedAttr来设置的。通过8个步骤完成:
(1) 初始化属性对象
(2) 为调度策略提取最大和最小优先级值
(3) 计算优先级值
(4) 将优先级值赋给sched_param结构体
(5) 使用调度参数设置属性对象
(6) 将调度属性设置为由属性对象决定
(7) 设置调度策略
(8) 使用调度属性对象创建一个线程
在示例6-10中,我们将优先级设置为一个平均值。但是优先级可以设置为介于线程调度策略所允许的最大和最小优先级值之间的任何值。有了这些方法,调度策略和优先级可以在线程被创建或运行之前,先设置在线程属性对象中。为了动态改变调度策略和优先级,可以使用pthread_setschedparam( )和pthread_setschedprio( )。
调用形式
- #include <pthread.h>
-
- int pthread_setschedparam(pthread_t thread, int policy,
- const struct sched_param *param);
- int pthread_getschedparam(pthread_t thread, int *restrict policy,
- struct sched_param *restrict param);
- 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指向的线程不存在;
实现不允许应用程序将优先
级改变为指定的值 |
注意:
要记得仔细考虑为何有必要改变运行线程的调度策略或优先级。这可能会严重影响应用程序的总体性能。有着较高优先级的线程会抢占运行的较低优先级的线程。这可能会产生饿死,即线程持续被抢占,因此无法完成执行。