Pthread 线程调度

设置以下关于调度策略之前应该首先通过pthread_attr_setinheritsched设置PTHREAD_EXPLICIT_SCHED.

1) Scheduling Scope
http://man7.org/linux/man-pages/man3/pthread_attr_setscope.3.html

通过pthread_attr_setscope()可以设置线程的资源竞争范围,包括两种,
Linux只支持PTHREAD_SCOPE_SYSTEM。

PTHREAD_SCOPE_SYSTEM
    The thread competes for resources with all other threads in
    all processes on the system that are in the same scheduling
    allocation domain (a group of one or more processors).
    PTHREAD_SCOPE_SYSTEM threads are scheduled relative to one
    another according to their scheduling policy and priority.

PTHREAD_SCOPE_PROCESS
    The thread competes for resources with all other threads in
    the same process that were also created with the
    PTHREAD_SCOPE_PROCESS contention scope.  PTHREAD_SCOPE_PROCESS
    threads are scheduled relative to other threads in the process
    according to their scheduling policy and priority.  POSIX.1
    leaves it unspecified how these threads contend with other
    threads in other process on the system or with other threads
    in the same process that were created with the
    PTHREAD_SCOPE_SYSTEM contention scope.

POSIX.1 requires that an implementation support at least one of these
contention scopes.  Linux supports PTHREAD_SCOPE_SYSTEM, but not
PTHREAD_SCOPE_PROCESS.


pthread_attr_getscope()可以获取当前的scope.

2) Scheduling Priority
http://man7.org/linux/man-pages/man3/pthread_attr_setschedparam.3.html

int pthread_attr_setschedparam(pthread_attr_t *attr,
    const struct sched_param *param);
int pthread_attr_getschedparam(const pthread_attr_t *attr,
    struct sched_param *param);
                                
其中sched_param数据结构为:
struct sched_param {
    int sched_priority;     /* Scheduling priority */
};

Linux下优先级为1~99, range 1 (low) to 99 (high)

也可以通过pthread_setschedprio设置线程优先级:
http://man7.org/linux/man-pages/man3/pthread_setschedprio.3.html

3) Scheduling Policy
http://man7.org/linux/man-pages/man3/pthread_attr_setschedpolicy.3.html
http://man7.org/linux/man-pages/man7/sched.7.html

调度策略:SCHED_FIFO, SCHED_RR, SCHED_OTHER, SCHED_IDLE, SCHED_BATCH, SCHED_DEADLINE(Linux 3.14以后)
其中SCHED_FIFO, SCHED_RR用于设置高优先级实时调度。
    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 threads always have higher
    priority than normal threads.)  Note well: POSIX.1 requires an
    implementation to support only 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.


4) 可以同时设置priority和policy
http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html

int pthread_setschedparam(pthread_t thread, int policy,
        const struct sched_param *param);
int pthread_getschedparam(pthread_t thread, int *policy,
        struct sched_param *param);

http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html

int sched_setscheduler(pid_t pid, int policy,
    const struct sched_param *param);

int sched_getscheduler(pid_t pid);        
          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值