title: pipe stuck in read (C - system calls)
body: <p>This the first time for me using C and so it has been very difficult so far. 
  My program reads a file.txt.
  In the first line there is the number of childs that i have to create(var: NPROC) and in the remaining lines i have to read the child's process id, two integers and a character that is the operation that i have to calculate:
  ex:</p>
  
  <pre><code>NPROC
  (id) (n1) (op) (n2)
  2
  2 3 + 1
  1 5 / 2
  2 9 * 3
  </code></pre>
  
  <p>I have to send the operations (line by line) to the child using a message queue (which works perfectly) and then the child should send back the results using a pipe.</p>
  
  <p>this is the code:</p>
  
  <p>(father)</p>
  
  <pre><code>//other code
  for(i=0; i &lt; NPROC; i++) {
  
      if(pipe(p) == -1) {
          perror("pipe");
          exit(1);
      }
  
      switch(currentPid = fork()){
          case 0:
  
              execl("./child", "./child", NULL);
  
              return 0;
          case -1:
              printf("Error when forking\n");
              break;
          default:
  
              // in the father
              childrenPids[i] = currentPid; // store current child pid
              wait(NULL);
  
              printf("\nParentpid:  %d, Childpid: %i \n", getpid(), currentPid);
  
              //close(p[1]);
  
              if(read(p[0], &amp;val, sizeof(val) == -1){
                  perror("read()\n");
                  exit(1);
              }
  
              printf("Parent(%d) received value: %d\n", getpid(), val);
  
              break;         
      }
  
     //other code
  
  
  (child)
   //other code
   while (1) {
  
          long msgtype = m-&gt;mtype;
          if(msgrcv(msgid, m, sizeof(message) - sizeof(m-&gt;mtype), msgtype, IPC_NOWAIT) == -1) {
              // if(close (p[1]) == -1){
              //     perror("close p[1] of the child\n");
              // } 
              //perror("\nmsgrcv");
              exit(1);
          }
          result[i] = calcolate_results(m);
          // printf("%i %i %c %i", m-&gt;id, m-&gt;num1, m-&gt;op, m-&gt;num2);
          printf("\nresult: %i\n", result[i]);
  
          val = result[i];
  
          if(write (p[1], &amp;val, sizeof(val)) == -1) { 
              perror("write\n");
              exit(1);
          }
          printf("Child(%d) send val: %d\n", getpid(), val);
      }
   //other code
  </code></pre>
  
  <p>Now my problem is that the read in the father seems not to get the message.
  When i execute the program doens't exit, it loops (stays stuch) at the read in the father.
  I tried to put the read outside the for (so it should at least read the last message) but the program still stays stuck in the read.
  So i would be very thankful if someone could help me.</p>