pthread线程优先级的调度

如果系统定义了 _POSIX_THREAD_PRIORITY_SCHEDULING ,则支持设置线程的实时调度优先级。
我们可以用下面的编译指令来判断:#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) 

调度策略 policy: 可以取三个值(SCHED_FIFO、SCHED_RR、SCHED_OTHER)。
  1. #include <pthread.h>

  2. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
  3. int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
函数:int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 用来设置线程属性 attr 的调度策略为 policy,则用 attr 创建的线程,它的调度策略则为 policy 指定的方式。
函数:int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy); 则用来从线程属性 attr 中获得调度策略,保存在 policy 中。
SCHED_FIFO: 先进先出 调度策略允许一个线程运行直到有更高优先级的线程准备好,或者直到它自愿阻塞自己(比如调用pthread_yield之类的函数)。在该调度策略下,当有一个更高优先级的线程准备好时,它将立即开始执行。

此外SCHED_FIFO是一个非分时的简单调度策略,当一个线程变成可运行状态,它将被追加到对应优先级队列的尾部((POSIX 1003.1)。当所有高优先级的线程终止或者阻塞时,它将被运行。对于相同优先级别的线程,按照简单的先进先运行的规则运行。我们考虑一种很坏的情况,如果有若干相同优先级的线程等待执行,然而最早执行的线程无终止或者阻塞动作,那么其他线程是无法执行的,除非当前线程调用如pthread_yield之类的函数,所以在使用SCHED_FIFO的时候要小心处理相同级别线程的动作。 (百度百科)

SCHED_RR:轮循 调度策略,除了考虑优先级之外,还加入了时间片的限制。当一个线程执行完了一个时间片,并且有其它的SCHED_RR或者SCHED_FIFO调度策略的相同优先级的线程准备好时,运行线程将被准备好的线程抢占。

鉴于SCHED_FIFO调度策略的一些缺点,SCHED_RRSCHED_FIFO做出了一些增强功能。从实质上看,它还是SCHED_FIFO调用策略。它使用最大运行时间来限制当前进程的运行,当运行时间大于等于最大运行时间的时候,当前线程将被切换并放置于相同优先级队列的最后。这样做的好处是其他具有相同级别的线程能在“自私“线程下执行。 (百度百科)

SCHED_OTHER:是Linux默认的分时调度策略。

它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的。简单地说,如果系统使用这种调度策略,程序将无法设置线程的优先级。请注意,这种调度策略也是抢占式的,当高优先级的线程准备运行的时候,当前线程将被抢占并进入等待队列。这种调度策略仅仅决定线程在可运行线程队列中的具有相同优先级的线程的运行次序 (百度百科)

优先级 priority: 各个调度策略支持的最大和最小的优先级可以通过下面的两个函数获得。
  1. #include <sched.h>

  2. int sched_get_priority_max(int policy);
  3. int sched_get_priority_min(int policy);

调度参数 sched_param:调度参数由一个结构体给出
  1. struct sched_param {
  2.                //... ....
  3.                int sched_priority; /* Scheduling priority */
  4.                //... ....
  5. };
POSIX 要求结构体sched_param至少包含调度优先级 sched_priority 一个成员。下面两个函数分别用来设置和获得线程属性attr的调度参数param:
  1. #include <pthread.h>

  2. int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
  3. int pthread_attr_getschedparam(pthread_attr_t *attr, struct sched_param *param);

继承调度属性 inheritsched:当我手动设置了调度策略或优先级时,必须显示的设置线程调度策略的inheritsched属性,因为pthread没有为inheritsched设置默认值。所以在改变了调度策略或优先级时必须总是设置该属性。
  1. #include <pthread.h>

  2. int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
  3. int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
第一个函数中inheritsched的取值为:PTHREAD_INHERIT_SCHED 或者 PTHREAD_EXPLICIT_SCHED。
前者为继承创建线程的调度策略和优先级,后者指定不继承调度策略和优先级,而是使用自己设置的调度策略和优先级。
无论何时,当你需要控制一个线程的调度策略或优先级时,必须将inheritsched属性设置为PTHREAD_EXPLICIT_SCHED。

注意
1)调度策略和优先级是分开来描述的。前者使用预定义的SCHED_RR、SCHED_FIFO、SCHED_OTHER,后者是通过结果体struct sched_param给出的。
2)这些设置调度策略和优先级的函数操作的对象是线程的属性pthread_attr_t,而不是直接来操作线程的调度策略和优先级的。函数的第一个参数都是pthread_attr_t。

实例:(《POSIX多线程程序设计》 )
  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <sched.h>
  4. #include "errors.h"

  5. void *thread_routine(void *arg)
  6. {
  7.     int my_policy;
  8.     struct sched_param my_param;

  9. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined(sun)
  10.     pthread_getschedparam(pthread_self(), &my_policy, &my_param);
  11.     printf("thread_routine running at %s/%d\n",
  12.         (my_policy == SCHED_FIFO ? "FIFO"
  13.             : (my_policy == SCHED_RR ? "RR"
  14.             : (my_policy == SCHED_OTHER ? "OTHER"
  15.             : "unknown"))),
  16.         my_param.sched_priority);
  17. #else
  18.     printf("thread_routine running\n");
  19. #endif
  20.     return NULL;
  21. }

  22. int main(int argc, char *argv[])
  23. {
  24.     pthread_t thread_id;
  25.     pthread_attr_t thread_attr;
  26.     int thread_policy;
  27.     struct sched_param thread_param;
  28.     int status, rr_min_priority, rr_max_priority;

  29.     pthread_attr_init(&thread_attr);

  30. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined(sun)
  31.     pthread_attr_getschedpolicy(&thread_attr, &thread_policy);
  32.     pthread_attr_getschedparam(&thread_attr, &thread_param);
  33.     printf("Default policy is %s, priority is %d\n",
  34.         (thread_policy == SCHED_FIFO ? "FIFO"
  35.          : (thread_policy == SCHED_RR ? "RR"
  36.             : (thread_policy == SCHED_OTHER ? "OTHER"
  37.                : "unknown"))),
  38.         thread_param.sched_priority);

  39.     status = pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
  40.     if(status != 0)
  41.         printf("Unable to set SCHED_RR policy.\n");
  42.     else{
  43.         rr_min_priority = sched_get_priority_min(SCHED_RR);
  44.         if(rr_min_priority == -1)
  45.             errno_abort("Get SCHED_RR min priority");
  46.         rr_max_priority = sched_get_priority_max(SCHED_RR);
  47.         if(rr_max_priority == -1)
  48.             errno_abort("Get SCHED_RR max priority");
  49.         thread_param.sched_priority = (rr_min_priority + rr_max_priority)/2;
  50.         printf("SCHED_RR priority range is %d to %d: using %d\n",
  51.             rr_min_priority, rr_max_priority, thread_param.sched_priority);
  52.         pthread_attr_setschedparam(&thread_attr, &thread_param);
  53.         printf("Creating thread at RR/%d\n", thread_param.sched_priority);
  54.         pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED);
  55.     }
  56. #else
  57.     printf("Priority scheduling not supported\n");
  58. #endif
  59.     pthread_create(&thread_id, &thread_attr, thread_routine, NULL);
  60.     pthread_join(thread_id, NULL);
  61.     printf("Main exiting\n");
  62.     return 0;
  63. }
运行结果:
  1. digdeep@ubuntu:~/pthread/learnthread$ ./sched_attr
  2. Default policy is OTHER, priority is 0
  3. SCHED_RR priority range is 1 to 99: using 50
  4. Creating thread at RR/50
  5. Main exiting

直接设置正在运行的线程的调度策略和优先级:(动态设置线程的调度策略和优先级)
上面提到,前面的那些函数只能通过线程的属性对象 pthread_attr_t 来设置线程的调度策略和优先级,不能够直接设置正在运行的线程的调度策略和优先级。下面的函数可以直接设置:
  1. #include <pthread.h>

  2. pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
  3. pthread_getschedparam(pthread_t thread, int *policystruct sched_param *param);
但是注意:当 pthread_setschedparam函数的参数  policy == SCHED_RR 或者 SCHED_FIFO 时,程序必须要在超级用户下运行!!!
实例: (《POSIX多线程程序设计》 )
  1. /*
  2.  * sched_thread.c
  3.  * Demonstrate dynamic scheduling policy use.
  4.  */
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. #include <sched.h>
  8. #include "errors.h"

  9. #define THREADS 5

  10. typedef struct thread_tag {
  11.     int index;
  12.     pthread_t id;
  13. } thread_t;

  14. thread_t threads[THREADS];
  15. int rr_min_priority;

  16. void *thread_routine(void *arg)
  17. {
  18.     thread_t *self = (thread_t *)arg;
  19.     struct sched_param my_param;
  20.     int my_policy;
  21.     int status;

  22.     my_param.sched_priority = rr_min_priority + self->index;
  23.     if(pthread_setschedparam(self->id, SCHED_RR, &my_param) != 0)
  24.         printf("pthread_setschedparam failed\n");
  25.     pthread_getschedparam(self->id, &my_policy, &my_param);
  26.     printf("thread_routine %d running at %s/%d\n",
  27.         self->index,
  28.         (my_policy == SCHED_FIFO ? "FIFO"
  29.             : (my_policy == SCHED_RR ? "RR"
  30.             : (my_policy == SCHED_OTHER ? "OTHER"
  31.             : "unknown"))),
  32.         my_param.sched_priority);

  33.     return NULL;
  34. }

  35. int main(int argc, char *argv[])
  36. {
  37.     int count;

  38.     rr_min_priority = sched_get_priority_min(SCHED_RR);
  39.     if(rr_min_priority == -1){
  40.         errno_abort("Get SCHED_RR min priority");
  41.     }
  42.     for(count = 0; count < THREADS; count++){
  43.         threads[count].index = count;
  44.         pthread_create(&threads[count].id, NULL,
  45.                  thread_routine, (void *)&threads[count]);
  46.     }
  47.     for(count = 0; count < THREADS; count++){
  48.         pthread_join(threads[count].id, NULL);
  49.     }
  50.     printf("Main exiting\n");

  51.     return 0;
  52. }
运行结果:
  1. digdeep@ubuntu:~/pthread/learnthread$ gcc -Wall -lpthread -o sched_thread sched_thread.c
  2. digdeep@ubuntu:~/pthread/learnthread$ ./sched_threadpthread_setschedparam failed
  3. pthread_setschedparam failed
  4. pthread_setschedparam failed
  5. thread_routine 1 running at OTHER/0
  6. thread_routine 3 running at OTHER/0
  7. pthread_setschedparam failed
  8. pthread_setschedparam failed
  9. thread_routine 2 running at OTHER/0
  10. thread_routine 4 running at OTHER/0
  11. thread_routine 0 running at OTHER/0
  12. Main exiting
  13. digdeep@ubuntu:~/pthread/learnthread$ sudo ./sched_thread
  14. [sudo] password for digdeep: 
  15. thread_routine 1 running at RR/2
  16. thread_routine 3 running at RR/4
  17. thread_routine 2 running at RR/3
  18. thread_routine 0 running at RR/1
  19. thread_routine 4 running at RR/5
  20. Main exiting
  21. digdeep@ubuntu:~/pthread/learnthread$
从上面的运行结果可以看出:在第一次执行 ./sched_thread 时,函数  pthread_setschedparam(self->id, SCHED_RR, &my_param) 调用失败了,在第二次运行时:
sudo ./sched_thread (输入密码)时,函数  pthread_setschedparam(self->id, SCHED_RR, &my_param) 调用成功。
将SCHED_RR换成SCHED_FIFO,结果也是一样的。

:利 pthread_setschedparam  函数改变在运行线程的调度策略和优先级肯定就不用调用函数来设置inheritsched属性了: pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED) 因为该函数设置的对象是pthread_attr_t !!!

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值