Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Linux上以root用户身份调用SCHED_FIFO线程的pthread_create()时获取EPERM大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我试图在 Linux系统上以root身份生成带有scheD_FIFO或scheD_RR策略的线程,但是我对pthread_create()的调用返回1(EPERM). pthread_create()的手册页说EPERM表示“调用者没有适当的权限来设置所需的调度参数或调度策略.”不应该root能够指定scheD_FIFO或scheD_RR吗? 我已经删除了将一个线程创建到一个只执行该操作的小程序中
我试图在 Linux系统上以root身份生成带有scheD_FIFO或scheD_RR策略的线程,但是我对pthread_create()的调用返回1(EPERM). pthread_create()的手册页说EPERM表示“调用者没有适当的权限来设置所需的调度参数或调度策略.”不应该root能够指定scheD_FIFO或scheD_RR吗?

@H_301_8@

我已经删除了将一个线程创建到一个只执行该操作的小程序中的代码.它看起来对我来说,但仍然得到错误.我究竟做错了什么?@H_301_8@

该程序:@H_301_8@

@H_301_8@

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

static void *_Thread(void *arg)
{
    (void)arg;
    printf("Thread running!\n");
    return NULL;
}

int main(void)
{
    int retVal;
    pthread_attr_t attr;
    struct sched_param schedParam;
    pthread_t thread;

    retVal = pthread_attr_init(&attr);
    if (retVal)
    {
        fprintf(stderr,"pthread_attr_init error %d\n",retVal);
        exit(1);
    }

    retVal = pthread_attr_seTinheritsched(&attr,PTHREAD_EXPLICIT_scheD);
    if (retVal)
    {
        fprintf(stderr,"pthread_attr_seTinheritsched error %d\n",retVal);
        exit(1);
    }

    retVal = pthread_attr_setschedpolicy(&attr,scheD_FIFO);
    if (retVal)
    {
        fprintf(stderr,"pthread_attr_setschedpolicy error %d\n",retVal);
        exit(1);
    }

    schedParam.sched_priority = 1;
    retVal = pthread_attr_setschedparam(&attr,&schedParam);
    if (retVal)
    {
        fprintf(stderr,"pthread_attr_setschedparam error %d\n",retVal);
        exit(1);
    }

    retVal = pthread_create(&thread,&attr,_Thread,null);
    if (retVal)
    {
        fprintf(stderr,"pthread_create error %d\n",retVal);
        exit(1);
    }

    retVal = pthread_join(thread,"pthread_join error %d\n",retVal);
        exit(1);
    }

    printf("main run successfully\n");
    return 0;
}

该程序已编译并以root身份运行.运行时,程序在调用pthread_create时失败,返回EPERm.@H_301_8@

将线程更改为scheD_RR调度没有任何效果 – 仍然由pthread_create返回EPERm.@H_301_8@

将线程更改为scheD_OTHER调度并将其优先级更改为0允许程序无错误地运行.@H_301_8@

解决方法@H_450_30@
必须是您的实时优先级软限制过于严格.

@H_301_8@

在运行代码之前,在sHell中调用ulimit -r unlimited.或者直接在代码调用setrlimit(RLIMIT_RTPRIO,…).@H_301_8@

系统范围的限制在/etc/security/limits.conf中设置.@H_301_8@

大佬总结

以上是大佬教程为你收集整理的在Linux上以root用户身份调用SCHED_FIFO线程的pthread_create()时获取EPERM全部内容,希望文章能够帮你解决在Linux上以root用户身份调用SCHED_FIFO线程的pthread_create()时获取EPERM所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。