Getting EPERM by calling pthread_create () on a SCHED_FIFO thread as root in Linux

I am trying to create threads using the SCHED_FIFO or SCHED_RR rules as root on a Linux system, but my pthread_create () calls return 1 (EPERM). The man page for pthread_create () indicates that EPERM indicates that "[t] it does not have the appropriate permission to set the necessary planning parameters or planning policy." Should root not specify SCHED_FIFO or SCHED_RR?

I removed the code that creates the stream into a small program that does just that. This looks right to me, but still getting the error. What am I doing wrong?

Program:

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

This program has been compiled and launched with administrator privileges. At startup, the program terminates when pthread_create is called, returning EPERM.

SCHED_RR - EPERM pthread_create.

SCHED_OTHER 0 .

+5
3

, .

ulimit -r unlimited . setrlimit(RLIMIT_RTPRIO, ...) .

/etc/security/limits.conf.

+4

Linux ( 2.6), Solaris 9 10. . . :

ulimit -r unlimited

. ....

+1

Run the program as the root user. Because we are busy with planning (for FIFO and RR). you also impute (implying) relative priorities and what only the root can do.

When executing the program normally, pthread_creare () returns EPERM, but since the root user is working fine.

For more information, visit: http://www.linuxforums.org/forum/programming-scripting/111359-pthread-error.html

0
source

All Articles