title: Setting scheduling policy SCHED_IDLE or SCHED_BATCH fails with EINVAL (22)
body: <p>I'm trying to create threads which are unlikely to be scheduled (this is for a test case, not production code), so I wanted to create a thread with <code>SCHED_IDLE</code> scheduling policy.</p>
  
  <p>Unfortunately this fails with <code>EINVAL</code> (even if running as root) on every distro/kernel version I tried.</p>
  
  <p>Note that this fails when calling <code>pthread_attr_setschedpolicy()</code>, while usually what fails is the creation of a thread with a non-authorised scheduling policy (for instance if you try to use <code>SCHED_RR</code> as non-root).</p>
  
  <p>This is some code to demostrate the problem:</p>
  
  <pre><code>#define _GNU_SOURCE
  
  #include &lt;assert.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct
  {
      const char *name;
      int value;
  } policy_t;
  
  policy_t policies[] = {
      { "SCHED_OTHER",  SCHED_OTHER },
      { "SCHED_IDLE",   SCHED_IDLE },
      { "SCHED_BATCH",  SCHED_BATCH },
      { "SCHED_FIFO",   SCHED_FIFO },
      { "SCHED_RR",     SCHED_RR },
  };
  
  int main(int argc, char *argv[])
  {
      for (int i = 0; i &lt; sizeof(policies) / sizeof(policies[0]); i++) {
          policy_t policy = policies[i];
  
          pthread_attr_t attr;
          pthread_attr_init(&amp;attr);
  
          int e = pthread_attr_setinheritsched(&amp;attr, PTHREAD_EXPLICIT_SCHED);
          assert(e == 0);
  
          e = pthread_attr_setschedpolicy(&amp;attr, policy.value);
          if (e != 0)
              printf("Cannot set sched policy %s (%d): %s (%d)\n",
                     policy.name,
                     policy.value,
                     strerror(e),
                     e);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The output I get is:</p>
  
  <pre><code>Cannot set sched policy SCHED_IDLE (5): Invalid argument (22)
  Cannot set sched policy SCHED_BATCH (3): Invalid argument (22)
  </code></pre>