title: semaphore + fork in linux
body: <p>I have a problem with semaphore and fork in Linux, here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/sem.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  #define KEY 464
  
  void debug(int semafor_id) {
      printf("Value of sem 0 is: %d \n",  semctl (semafor_id, 0 , GETVAL , 0));
  }
  
  void main()
  {
     int id;  
     struct sembuf operations[1]; 
  
  
     // Sreate semaphore
     id = semget(KEY, 1, 0777 | IPC_CREAT );
  
  
     // set value
      operations[0].sem_num = 0;
      operations[0].sem_op = 10;  
      operations[0].sem_flg = 0;
  
      semop(id, operations, 1);  
  
     // show what is the value 
  
      debug(id);
  
  
     // do the same with the child process 
      if(!fork()) {
          printf("IN child process \n");
          debug(id);
          exit(0);
      }
  
      semctl (id, 0 ,  IPC_RMID , 0);
  
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <ul>
  <li>Value of sem 0 is: 10 </li>
  <li>IN child process:</li>
  <li>value of sem 0 is: -1</li>
  </ul>
  
  <p>So it seems I can't use semaphore in child process. I don't think I need to use shared memory. Help?</p>