简体   繁体   English

具有实时优先级的pthreads

[英]pthreads with real time priority

I need to manage a pool of threads having different priorities, so I wrote the following thread startup procedure: 我需要管理具有不同优先级的线程池,因此我编写了以下线程启动过程:

static
int startup(thrd_t *thrd, thrd_sync_t *sync, int prio)
{
    pthread_attr_t attr;
    int err;
    struct sched_param param = {
        .sched_priority = prio
    };

    assert(pthread_attr_init(&attr) == 0);
    assert(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0);
    assert(pthread_attr_setschedparam(&attr, &param) == 0);
    err = pthread_create(&thrd->handler, &attr, thread_routine, (void *)thrd);
    pthread_attr_destroy(&attr);

    return err;
}

In principle the unprivileged user should not be allowed to execute this code: the pthread_create() call should return EPERM because of the security implications of running a thread with high priority. 原则上,不应允许非特权用户执行此代码:pthread_create()调用应返回EPERM,因为运行具有高优先级的线程具有安全隐患。

Unexpectedly it works for the normal user, but it doesn't respect the given priority at all. 出乎意料的是,它适用于普通用户,但它根本不尊重给定的优先级。

I tried to modify the code by removing the pthread_attr_t and by setting the scheduling attribute once the thread has been created: 我尝试通过删除pthread_attr_t并在创建线程后设置调度属性来修改代码:

static
int startup(thrd_t *thrd, thrd_sync_t *sync, int prio)
{
    pthread_attr_t attr;
    int err;
    struct sched_param param = {
        .sched_priority = prio
    };

    err = pthread_create(&thrd->handler, NULL /*&attr*/, thread_routine,
                         (void *)thrd);
    if (err != 0) return err;

    err = pthread_setschedparam(thrd->handler, SCHED_FIFO, &param);
    if (err != 0) return err;

    return err;
}

This approach by the way is much more difficult to manage, since in case of error I need to kill the newly created thread. 顺便说一下,这种方法管理起来要困难得多,因为如果出现错误,我需要终止新创建的线程。 At least it seems to work properly with respect to permission requirements (only root can execute this), but still priorities are not respected. 至少它似乎在权限要求方面正常工作(只有root可以执行此操作),但仍然不遵守优先级。

Am I doing something wrong? 难道我做错了什么?

EDIT 编辑

I've just added the following piece of code which is executed by every thread: 我刚刚添加了以下一段由每个线程执行的代码:

static
void getinfo ()
{
    struct sched_param param;
    int policy;

    sched_getparam(0, &param);
    DEBUG_FMT("Priority of this process: %d", param.sched_priority);

    pthread_getschedparam(pthread_self(), &policy, &param);

    DEBUG_FMT("Priority of the thread: %d, current policy is: %d and should be %d",
              param.sched_priority, policy, SCHED_FIFO);
}

With the first method (namely pthread_attr_t approach) it turns out that the pthread_attr_setschedpolicy is totally not effective, since the priority is 0 and the policy is not SCHED_FIFO. 使用第一种方法(即pthread_attr_t方法),结果是pthread_attr_setschedpolicy完全无效,因为优先级为0且策略不是SCHED_FIFO。

With the second method (namely pthread_setschedparam approach) the function prints the expected data, but the execution keeps behaving in a wrong way. 使用第二种方法(即pthread_setschedparam方法),该函数打印预期的数据,但执行仍然以错误的方式运行。

I think you also have to use pthread_attr_setinheritsched to ensure that your changes to the priority setting are taken into account. 我认为您还必须使用pthread_attr_setinheritsched来确保考虑对优先级设置的更改。 From the man page: 从手册页:

PTHREAD_INHERIT_SCHED Specifies that the scheduling policy and associated attributes are to be inherited from the creating thread, and the scheduling attributes in this attr argument are to be ignored. PTHREAD_INHERIT_SCHED指定从创建线程继承调度策略和关联属性,并忽略此attr参数中的调度属性。

PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be set to the corresponding values from this attribute object. PTHREAD_EXPLICIT_SCHED指定将调度策略和关联的属性设置为此属性对象的相应值。

And a little bit further in the man page, you have : 在手册页中,您还有以下内容:

The default setting of the inherit-scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED. 新初始化的线程属性对象中的inherit-scheduler属性的缺省设置是PTHREAD_INHERIT_SCHED。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM