title: How to use POSIX semaphores on forked processes in C?
body: <p>I want to fork multiple processes and then use a semaphore on them. Here is what I tried:</p>
  
  <pre><code>sem_init(&amp;sem, 1, 1);   /* semaphore*, pshared, value */
  .
  .
  .
  if(pid != 0){ /* parent process */
      wait(NULL); /* wait all child processes */
  
      printf("\nParent: All children have exited.\n");
      .
      .
      /* cleanup semaphores */
      sem_destroy(&amp;sem);      
      exit(0);
  }
  else{ /* child process */
      sem_wait(&amp;sem);     /* P operation */
      printf("  Child(%d) is in critical section.\n",i);
      sleep(1);
      *p += i%3;  /* increment *p by 0, 1 or 2 based on i */
      printf("  Child(%d) new value of *p=%d.\n",i,*p);
      sem_post(&amp;sem);     /* V operation */
      exit(0);
  }
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre>child(0) forked
  child(1) forked
    Child(0) is in critical section.
    Child(1) is in critical section.
  child(2) forked
    Child(2) is in critical section.
  child(3) forked
    Child(3) is in critical section.
  child(4) forked
    Child(4) is in critical section.
    Child(0) new value of *p=0.
    Child(1) new value of *p=1.
    Child(2) new value of *p=3.
    Child(3) new value of *p=3.
  
    Child(4) new value of *p=4.
  Parent: All children have exited.</pre>
  
  <p>This clearly means the semaphore didn't work as it was supposed to.  Can you explain how I should use semaphores on forked processes?</p>