问题描述
我试图用产卵或SCHED_FIFO SCHED_RR政策在Linux系统上的根,但我的电话线程在pthread_create()正在返回1(EPERM)。对于在pthread_create()的man page说EPERM指出,[T]他调用者没有适当的权限设置所需的调度参数或调度政策。应该不是根能够指定SCHED_FIFO或SCHED_RR?
I am trying to spawn threads with SCHED_FIFO or SCHED_RR policies as root on a Linux system but my calls to pthread_create() are returning 1 (EPERM). The man page for pthread_create() says that EPERM indicates that "[t]he caller does not have appropriate permission to set the required scheduling parameters or scheduling policy." Shouldn't root be able to specify SCHED_FIFO or SCHED_RR?
我已经剥离出来的code,创建一个线程进入一个小程序,确实仅此而已。它看起来我的权利,但仍然得到错误。我在做什么错了?
I've stripped out the code that creates a thread into a small program that does only that. It looks right to me but still gets the error. What am I doing wrong?
程序:
#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, NULL);
if (retVal)
{
fprintf(stderr, "pthread_join error %d\n", retVal);
exit(1);
}
printf("main run successfully\n");
return 0;
}
该程序被编译并以root身份运行。运行时,其程序无法在调用在pthread_create,返回EPERM。
This program has been compiled and run as root. When run, it program fails at the call to pthread_create, returning EPERM.
更改线程调度SCHED_RR没有效果 - 通过EPERM仍然在pthread_create返回
Changing the thread to SCHED_RR scheduling has no effect - EPERM still returned by pthread_create.
更改线程调度SCHED_OTHER及其优先级为0,允许程序运行没有错误。
Changing the thread to SCHED_OTHER scheduling and its priority to 0 allows the program to run without error.
推荐答案
这必须是你的实时优先级软限制过于严格。
It must be that your real-time priority soft limit is too restrictive.
无论是电话的ulimit -r无限
在shell中运行你的code之前。或致电了setrlimit(RLIMIT_RTPRIO,...)
在code直接。
Either call ulimit -r unlimited
in shell before running your code. Or call setrlimit(RLIMIT_RTPRIO, ...)
directly in your code.
系统范围限制在 /etc/security/limits.conf文件
设置。
这篇关于获取EPERM在Linux上呼吁SCHED_FIFO线程在pthread_create()作为根时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!