title: Why do these processes stay blocked even though the semaphore supposedly frees them?
body: <p>I'm learning about synchronization with semaphores and message queues and I'm having a problem with an assignment.</p>
  <p>I am trying to synchronize several child processes (the parent has a completely different function that is irrelevant to this question) so that when all of them get to a point they stop until everyone is ready, but when it's time to continue only one of them does.</p>
  <p>I've made a small program simulating the code i have problems with.</p>
  <p>Supposedly the &quot;main&quot; child process should receive one message per other child process and then free them so they can finish the program, however in practice only the &quot;main&quot; one continues.</p>
  <p>The code i pasted does actually give me a segmentation fault, but i'm not sure if the reason is the same i had in the other project or something i overlooked whilst trying to make it similar to the original.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/sem.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;ctype.h&gt;
  
  
  
  int sem, msgqueue, alive;
  
  void childFunction(int i);
  
  struct sembuf sopsEntrar1, sopsSalir1, sopsEntrar2, sopsSalir2;
  
  
  union semun{
      int val;
      struct semid_ds *buf;   
      unsigned short *array;
  }semn1, semn2;
  
  //Message type
  
      
  struct TipoDispara{
      long tipo;
      char dispara[2];    
  };
  
  struct TipoDispara td, tenv;
  
  int main(int argc, char *argv[]){
      /*In the actual code there are checks to make sure the argvs are valid, 
      for the sake of this question any number higher than two will do*/
      alive= atoi(argv[1]);
      
      sopsEntrar1.sem_num=0;
      sopsEntrar1.sem_op=-1;
      sopsEntrar1.sem_flg=0;
  
  
      sopsSalir1.sem_num=0;
      sopsSalir1.sem_op=1;
      sopsSalir1.sem_flg=0;
  
      sopsEntrar2.sem_num=1;
      sopsEntrar2.sem_op=-1;
      sopsEntrar2.sem_flg=0;
  
      sopsSalir2.sem_num=1;
      sopsSalir2.sem_op=1;
      sopsSalir2.sem_flg=0;
  
      if((sem = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600))==-1){
          perror(&quot;Error al crear semaforos\n\n&quot;); 
          fflush(stdout);
          exit -1;
      }
  
      semn1.val=1;
  
      if(semctl(sem, 0, SETVAL, semn1)==-1){
          perror(&quot;Error inicializando sem 1\n\n&quot;);
          fflush(stdout);
  
          return -1;
  
      }
      
      if(semctl(sem, 1, SETVAL, semn1)==-1){
          perror(&quot;Error inicializando sem 2\n\n&quot;);
          fflush(stdout);
  
          return -1;
  
      }
  
      if((msgqueue = msgget(IPC_PRIVATE, IPC_CREAT | 0600))==-1){
          perror(&quot;Error al crear buzon\n\n&quot;); 
          fflush(stdout);
          exit -1;
      }
      int i;
      for(i=0;i&lt;alive;i++){
          switch(fork()){
              case -1:
                  perror(&quot;Error al hacer el fork.\n\n&quot;);
                  fflush(stdout);
  
                  return -1;
                  break;
  
              case 0:
                  childFunction(i);
  
                  return 0;
                  break;
              default:
  
  /*In the actual program it does something, here i'll make it sleep a few seconds 
  to give the child processes some time before ending*/
              if(i=(alive-1)){
                  sleep(10);
                  if(semctl(sem, 0, IPC_RMID)==-1){
                      
                      printf(&quot;Error liberando semáforo\n&quot;);
                      fflush(stdout);
                     
                  }else{
                      printf(&quot;\n\n\nSemaforo liberado.\n\n&quot;);
                      fflush(stdout);
                  }
                  if(msgctl(msgqueue, IPC_RMID, NULL)==-1){
                      
                      printf(&quot;Error liberando buzon\n&quot;);
                      fflush(stdout);
  
                  }else{
                      printf(&quot;\n\n\nBuzon liberado.\n\n&quot;);
                      fflush(stdout);
                  }
                  return 1;
                 
            }
            break;
      }
  
  
      
      
  }
  }
  void childFunction(int i){
      
      td.tipo=2;
      /*In the actual program the way of choosing who goes in the if and who in 
      the else, here the only relevant thing is that only one of the children 
      processes go in the int*/
      if(i == 1){
          
          /*What shoud happen is that this process waits for the rest to send a
          message, one msgrcv per process that's supposed to send a message*/
          int j;
          for(j=1;j&lt;alive;j++){
              if(msgrcv(msgqueue,&amp;td, sizeof(td)-sizeof(long),2,0)){
                  perror(&quot;error recibiendo mensajes&quot;);
                  fflush(stdout);
             
                  exit -1;
              }
              
          }
       /*After going through every message, frees the other processes,
       meaning every process alive but itself*/
      semop(sem, &amp;sopsSalir2, (alive-1));
      }else{
          strcpy(td.dispara, &quot;SI&quot;);
          if(msgsnd(msgqueue,&amp;td, sizeof(td)-sizeof(long),0)){
             perror(&quot;error enviando mensaje&quot;);
             fflush(stdout);
             
             exit (-1);
          }
          /*After sending the message indicating they've arrived they wait for
          the &quot;main&quot; process to receive every message and free them*/
          semop(sem, &amp;sopsEntrar2, 1);
      }
      printf(&quot;\nI'm process %d\n\n&quot;, i);
  
  }
      
  
  </code></pre>