与实时优先级的pthreads [英] pthreads with real time priority

查看:225
本文介绍了与实时优先级的pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要管理具有不同优先级的线程池,所以我写了下面的线程启动过程:

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;
}

在原则上没有特权的用户不应该被允许执行code:将在pthread_create()调用应该返回,因为运行具有高优先级的线程安全的影响EPERM

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.

出乎意料它为正常用户,但它不尊重优先在所有

Unexpectedly it works for the normal user, but it doesn't respect the given priority at all.

我试图通过删除 pthread_attr_t 和一旦线程已经创建设置调度属性修改code:

I tried to modify the code by removing the pthread_attr_t and by setting the scheduling attribute once the thread has been created:

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;
}

这方法的方式是更难以管理,因为在错误的情况下,我需要杀掉新创建的线程。至少它似乎对于权限要求(只有root可以执行此)正常工作,但仍优先级不尊重。

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.

我是不是做错了什么?

修改

我刚加入这是由每个线程执行下面的一段code的:

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);
}

使用第一种方法(即 pthread_attr_t 办法)事实证明,该pthread_attr_setschedpolicy是完全无效的,因为优先级为0,政策不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_setschedparam 办法)的函数打印预期的数据,但执行保存在一个错误的行为方式。

With the second method (namely pthread_setschedparam approach) the function prints the expected data, but the execution keeps behaving in a wrong way.

推荐答案

我想你也必须使用 pthread_attr_setinheritsched ,以确保您的优先级设置的变化都考虑帐户。从手册页:

I think you also have to use pthread_attr_setinheritsched to ensure that your changes to the priority setting are taken into account. From the man page:

PTHREAD_INHERIT_SCHED
      指定的调度策略和关联的属性是
  待从所述创建继承
  螺纹,和调度属性
  在这个ATTR说法是要
  忽略。

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_EXPLICIT_SCHED
      指定的调度策略和关联的属性是
  被设置为相应的值
  从此属性对象。

PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be set to the corresponding values from this attribute object.

和手册页得远一点,你有:

And a little bit further in the man page, you have :

在继承调度器属性的新的默认设置
         初始化线程属性对象PTHREAD_INHERIT_SCHED。

The default setting of the inherit-scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED.

这篇关于与实时优先级的pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆