title: Getting EPERM when calling pthread_create() for SCHED_FIFO thread as root on Linux
body: <p>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?</p>
  
  <p>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?</p>
  
  <p>The program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  
  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(&amp;attr);
      if (retVal)
      {
          fprintf(stderr, "pthread_attr_init error %d\n", retVal);
          exit(1);
      }
  
      retVal = pthread_attr_setinheritsched(&amp;attr, PTHREAD_EXPLICIT_SCHED);
      if (retVal)
      {
          fprintf(stderr, "pthread_attr_setinheritsched error %d\n", retVal);
          exit(1);
      }
  
      retVal = pthread_attr_setschedpolicy(&amp;attr, SCHED_FIFO);
      if (retVal)
      {
          fprintf(stderr, "pthread_attr_setschedpolicy error %d\n", retVal);
          exit(1);
      }
  
      schedParam.sched_priority = 1;
      retVal = pthread_attr_setschedparam(&amp;attr, &amp;schedParam);
      if (retVal)
      {
          fprintf(stderr, "pthread_attr_setschedparam error %d\n", retVal);
          exit(1);
      }
  
      retVal = pthread_create(&amp;thread,
                              &amp;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;
  }
  </code></pre>
  
  <p>This program has been compiled and run as root. When run, it program fails at the call to pthread_create, returning EPERM.</p>
  
  <p>Changing the thread to SCHED_RR scheduling has no effect - EPERM still returned by pthread_create.</p>
  
  <p>Changing the thread to SCHED_OTHER scheduling and its priority to 0 allows the program to run without error.</p>