title: Linux semaphore.h pending / waiting quene policy
body: <p>I'm testing pending policy of linux's semaphore.</p>
  
  <p>Linux's manpage doesn't tell pending policy:
  <a href="http://linux.die.net/man/3/sem_wait" rel="nofollow">http://linux.die.net/man/3/sem_wait</a>,</p>
  
  <p>Maybe it's decided by scheduler. Which means highest priority thread pending on this semaphore can run first. </p>
  
  <p>So I create 4 pthreads with priority 10, 20, 30, 40, and set scheduler policy to SCHED_FIFO.</p>
  
  <p>However, test result shows the pending policy is FIFO.
  Does anyone knows if there any document or source code of semaphore.h for waiting policy information?</p>
  
  <p>Thanks</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include "stdlib.h"
  #include "stdio.h"
  
  sem_t b_sem;
  
  int test_routine(int my_num)
  {
      usleep(1+ my_num * 1000);
      printf("I'm %d, going to wait sem\n", my_num);
      sem_wait(&amp;b_sem);
      printf("I'm %d, I got the sem\n", my_num);
      while(1)
      {
              sleep(1);
      }
  }
  
  int main()
  {
      int i;
      int pthrid[4] = {0,0,0,0};
      pthread_attr_t attr[4];
      struct sched_param prv_priority[4];
  
      struct sched_param param;
      param.sched_priority = sched_get_priority_max(SCHED_FIFO);
      sched_setscheduler(0, SCHED_FIFO, &amp;param);
  
      sem_init(&amp;b_sem, 1, 0);
  
      for( i = 0 ; i &lt; 4 ; i++ )
      {
              pthread_attr_init(&amp;attr[i]);
              pthread_attr_getschedparam(&amp;attr[i], &amp;prv_priority[i]);
              pthread_attr_setschedpolicy(&amp;attr[i], SCHED_FIFO);
              prv_priority[i].sched_priority = (10 + i*10);
              printf("test start, thread %d has priority %d\n", i ,prv_priority[i].sched_priority);
              pthread_attr_setschedparam(&amp;attr[i], &amp;prv_priority[i]);
              pthread_create(&amp;pthrid[i], &amp;attr[i], test_routine, i);
      }
  
      sleep(1);
  
      for( i = 0 ; i &lt; 4 ; i++ )
      {
              printf("give sem\n");
              sem_post(&amp;b_sem);
              sleep(1);
      }
  
      sleep(100000);
  
  }
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>test start, thread 0 has priority 10
  test start, thread 1 has priority 20
  test start, thread 2 has priority 30
  test start, thread 3 has priority 40
  I'm 0, going to wait sem
  I'm 1, going to wait sem
  I'm 2, going to wait sem
  I'm 3, going to wait sem
  give sem
  I'm 0, I got the sem
  give sem
  I'm 1, I got the sem
  give sem
  I'm 2, I got the sem
  give sem
  I'm 3, I got the sem
  </code></pre>