日期:2014-05-16  浏览次数:20706 次

请问在Linux下怎么修改当前线程的优先级?
如题。

------解决方案--------------------
pthread_create()中的attr参数的__schedpolicy成员,表示新线程的调度策略,主要包括SCHED_OTHER(正常、非实时)、SCHED_RR(实时、轮转法)和SCHED_FIFO(实时、先入先出)三种,缺省为SCHED_OTHER,后两种调度策略仅对超级用户有效。运行时可以用过pthread_setschedparam()来改变。
__schedparam成员是一个struct sched_param结构,目前仅有一个sched_priority整型变量表示线程的运行优先级。这个参数仅当调度策略为实时(即SCHED_RR或SCHED_FIFO)时才有效,并可以在运行时通过pthread_setschedparam()函数来改变,缺省为0

------解决方案--------------------
C/C++ code
       For processes scheduled under one of the normal scheduling policies (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used
       in scheduling decisions (it must be specified as 0).

       Processes  scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the range 1 (low) to 99
       (high).  (As the numbers imply, real-time processes always have higher priority than normal processes.)  Note well: POSIX.1-2001  only
       requires  an  implementation to support a minimum 32 distinct priority levels for the real-time policies, and some systems supply just
       this minimum.  Portable programs should use sched_get_priority_min(2) and sched_get_priority_max(2) to find the  range  of  priorities
       supported for a particular policy.