当前位置: 技术问答>linux和unix
请问在Linux下怎么修改当前线程的优先级?
来源: 互联网 发布时间:2017-02-22
本文导语: 如题。 | 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). ...
如题。
|
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.
上面告诉你只有RR和FIFO允许设置优先级,它们属于实时调度,通常范围是1-99,但是可移植的方式是:
sched_get_priority_min(2) and sched_get_priority_max(2) to find the range of priorities.
对于线程来说,其调度策略有:
SYNOPSIS
#include
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
Compile and link with -pthread.
这两个接口。
The supported values for policy are SCHED_FIFO, SCHED_RR, and SCHED_OTHER, with the semantics described in sched_setscheduler(2).
它告诉你线程只支持者三种。
int pthread_attr_setschedparam(pthread_attr_t *attr,
const struct sched_param *param);
int pthread_attr_getschedparam(pthread_attr_t *attr,
struct sched_param *param);
这两个函数是在创建线程之前准备pthread_attr_t时候使用的,它们可以设置优先级。
线程一旦启动就没法修改了,以上函数都需要在启动前设置。
|
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
__schedparam成员是一个struct sched_param结构,目前仅有一个sched_priority整型变量表示线程的运行优先级。这个参数仅当调度策略为实时(即SCHED_RR或SCHED_FIFO)时才有效,并可以在运行时通过pthread_setschedparam()函数来改变,缺省为0