线程调度算法和优先级

 函数pthread_attr_setschedpolicypthread_attr_getschedpolicy分别用来设置和得到线程的调度策略。

名称:

pthread_attr_getschedpolicy

pthread_attr_setschedpolicy

功能:

获得/设置线程的调度策略

头文件:

#include <pthread.h>

函数原形:

int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy);

int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy);

参数:

attr          线程属性变量

policy        调度策略

返回值:

若成功返回0,若失败返回-1

      

 

 

 

 

 

 

 

 

 

 

 

这两个函数具有两个参数,第1个参数是指向属性对象的指针,第2个参数是调度策略或指向调度策略的指针。调度策略可能的值是先进先出(SCHED_FIFO)、轮转法(SCHED_RR,或其它(SCHED_OTHER)。

       SCHED_FIFO策略允许一个线程运行直到有更高优先级的线程准备好,或者直到它自愿阻塞自己。在SCHED_FIFO调度策略下,当有一个线程准备好时,除非有平等或更高优先级的线程已经在运行,否则它会很快开始执行。

    SCHED_RR(轮循)策略是基本相同的,不同之处在于:如果有一个SCHED_RR

策略的线程执行了超过一个固定的时期(时间片间隔)没有阻塞,而另外的SCHED_RRSCHBD_FIPO策略的相同优先级的线程准备好时,运行的线程将被抢占以便准备好的线程可以执行。

    当有SCHED_FIFOSCHED_RR策赂的线程在一个条件变量上等待或等待加锁同一个互斥量时,它们将以优先级顺序被唤醒。即,如果一个低优先级的SCHED_FIFO线程和一个高优先织的SCHED_FIFO线程都在等待相同的互斥锁,则当互斥量被解锁时,高优先级线程将总是首先被解除阻塞。

 

线程的调度参数

 函数pthread_attr_getschedparampthread_attr_setschedparam分别用来设置和得到线程的调度参数。

名称:

pthread_attr_getschedparam

pthread_attr_setschedparam

功能:

获得/设置线程的调度参数

头文件:

#include <pthread.h>

函数原形:

int pthread_attr_getschedparam(const pthread_attr_t *attr,struct sched_param *param);

int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);

参数:

attr          线程属性变量

param          sched_param结构

返回值:

若成功返回0,若失败返回-1

      

 

 

 

 

 

 

 

这两个函数具有两个参数,第1个参数是指向属性对象的指针,第2个参数是sched_param结构或指向该结构的指针。结构sched_param在文件/usr/include /bits/sched.h中定义如下:

      

struct sched_param

{

       int _sched_priority;

};

 

结构sched_param的子成员sched_priority控制一个优先权值,大的优先权值对应高的优先权。系统支持的最大和最小优先权值可以用sched_get_priority_max函数和sched_get_priority_min函数分别得到。

 

注意:如果不是编写实时程序,不建议修改线程的优先级。因为,调度策略是一件非常复杂的事情,如果不正确使用会导致程序错误,从而导致死锁等问题。如:在多线程应用程序中为线程设置不同的优先级别,有可能因为共享资源而导致优先级倒置。

 

 

名称:

sched_get_priority_max

sched_get_priority_min

功能:

获得系统支持的线程优先权的最大和最小值

头文件:

#include <pthread.h>

函数原形:

int sched_get_priority_max(int policy);

int sched_get_priority_min(int policy);

参数:

policy调用策略          

返回值:

线程优先权的最大和最小值

      

 

 

 

 

 

 

 

 

 

 

 

实例如下:

#include <pthread.h>

#include <sched.h>

 

void *child_thread(void *arg)

{

int policy;

int max_priority,min_priority;

struct sched_param param;

pthread_attr_t attr;

 

pthread_attr_init(&attr); /*初始化线程属性变量*/

#if 0

pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED); /*设置线程继承性*/

pthread_attr_getinheritsched(&attr,&policy); /*获得线程的继承性*/

if(policy==PTHREAD_EXPLICIT_SCHED)

    printf(“Inheritsched:PTHREAD_EXPLICIT_SCHED\n”);

if(policy==PTHREAD_INHERIT_SCHED)

    printf(“Inheritsched:PTHREAD_INHERIT_SCHED\n”);

 #endif

pthread_attr_setschedpolicy(&attr,SCHED_RR);/*设置线程调度策略*/

pthread_attr_getschedpolicy(&attr,&policy);/*取得线程的调度策略*/

 switch ( policy )
    {
        case SCHED_FIFO:
                printf("policy = SCHED_FIFO\n");
                break;
        case SCHED_RR:
                printf("policy = SCHED_RR\n");
                break;
        case SCHED_OTHER:
                printf("policy = SCHED_OTHER\n");
                break;
        default:
                printf("policy = UNKNOWN\n");
                break;
    } 

max_priority = sched_get_priority_max(policy);   /*获得系统支持的线程优先权的最大值*/
min_priority = sched_get_priority_min(policy);    /* 获得系统支持的线程优先权的最小值*/

printf(“Max priority:%u\n”,max_priority);

printf(“Min priority:%u\n”,min_priority);

 

param._sched_priority=max_priority;   //或者将此函数增加参数,将max_priority换成函数的参数值,

                                                           //(min_priority, max_priority)区间内的值

pthread_attr_setschedparam(&attr,&param);/*设置线程的调度参数*/

printf(“sched_priority:%u\n”,param._sched_priority);/*获得线程的调度参数*/

pthread_attr_destroy(&attr);

}

 

int main(int argc,char *argv[ ])

{

pthread_t child_thread_id;

 

pthread_create(&child_thread_id,NULL,child_thread,NULL);

pthread_join(child_thread_id,NULL);

}


inherit [英]ɪn'herɪt  
vt. vi.继承


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

调用形式

 
 
  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调度和优先级函数

失败的条件

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指向的线程不存在;

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

级改变为指定的值


注意:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值