title: why sem_wait doesn't wait semaphore on mac OSX?
body: <p>The following code shows a producer-consumer example:
  Once a product is produced, the consumer will get this product. </p>
  
  <p>But I'm surprised that the consumer will sill get a product when there is no product. </p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;semaphore.h&gt;
  
  #define NUM 5
  int queue[NUM];
  int i;
  sem_t *blank_number, *product_number;
  
  void *producer(void *arg) {
      int p = 0;
      while (1) {
          sem_wait(blank_number);
          queue[p] = rand() % 1000 + 1;
          printf("Produce queue[%d]:%d\n", p, queue[p]);
          i = sem_post(product_number);
          //printf("i_p=%d\n", i);
          p = (p+1)%NUM;
          sleep(rand()%5);
      }
  }
  
  void *consumer(void *arg) {
      int c = 0;
      while (1) {
          sem_wait(product_number);
          printf("Consume queue[%d]:%d\n", c, queue[c]);
          queue[c] = 0;
          i = sem_post(blank_number);
          //printf("i_c=%d\n", i);
          c = (c+1)%NUM;
          sleep(rand()%5);
      }
  }
  
  int main(int argc, char *argv[]) {
      pthread_t pid, cid;  
  
      //set blank_number to NUM
      blank_number = sem_open("blank_number", O_CREAT, S_IRWXU, NUM);
      if(blank_number == SEM_FAILED){
          perror("open blank_number");
          return 1;
      }
      //set product_number to 0
      product_number = sem_open("product_number", O_CREAT, S_IRWXU, 0);
      if(product_number == SEM_FAILED){
          perror("open product_number");
          return 1;
      }
  
      pthread_create(&amp;pid, NULL, producer, NULL);
      pthread_create(&amp;cid, NULL, consumer, NULL);
      pthread_join(pid, NULL);
      pthread_join(cid, NULL);
      sem_close(blank_number);
      sem_close(product_number);
      return 0;
  }
  </code></pre>
  
  <p>In my test result, there is only <em>one product</em>: 808, but the consumer gets <em>two products</em>: 808 and 0;</p>
  
  <pre><code>$ sudo ./a.out
  Produce queue[0]:808
  Consume queue[0]:808
  Consume queue[1]:0
  </code></pre>
  
  <p>Is there any wrong in my code?</p>