title: How to properly close a pipe
body: <p>I have a piece of code I am trying to alter. The code is simply 2 pipes that communicate between two processes (parent and child process). I am trying to create a "clean termination" of the program so that whenever a 0 is sent from parent to the child the respective process shuts down their end of all pipes.</p>
  
  <p>The code is something like:</p>
  
  <pre><code>int main
  {
     int pipe1[2], pipe2[2], value, request;
     if(pipe(pipe1) &lt; 0 || pipe(pipe2) &lt; 0)
          printf("Failed to create pipes.\n");
  
     switch(fork())
     {
         case -1:
              perror("Cannot create fork.\n");
              break;
  
         case 0:
             close (pipe1[WRITE]);
             close (pipe2[READ]);
             close (0);
             close (1);
  
           for(;;) {
             read(pipe1[READ], &amp;value, sizeof value);
  
             //If user enters 0.
             if(value == 0)
             {
                 printf("Terminating child process!\n");
                 close(pipe1[READ]);
                 //write a 0 back to the parent so that it can close its end.
                 write(pipe2[WRITE], 0, sizeof (int));
                 close(pipe2[WRITE]);
                 exit(0);
             }
             else
             {
                 /*Enter function to do something on the number here*/
                 write(pipe2[WRITE], &amp;result, sizeof result);
             }
          }
          break;
  
          default: /* PARENT PROCESS - PRODUCER */
               close(pipe1[READ]);
               close(pipe2[WRITE]);
               for(;;) {
                   printf("Ange ett tal: ");
                   scanf("%d", &amp;value);
                   if(write(pipe1[WRITE], &amp;value, sizeof value) != sizeof value)
                   {
                       perror("Cannot write thru pipe.\n");
                       return 1;
                   }
  
               //Clean shutdown if value == 0
                if(value == 0)
               {
                   close(pipe2[WRITE]);
                   read(pipe2[READ],&amp;result,sizeof(result));
                   if(result == 0)
                   {
                       close(pipe2[READ]);
                       printf("Pipe2[Read] closed!\n");
                       wait(0);
                   }
               }
  
               else
               {
                   read(pipe2[READ],&amp;result,sizeof(result));
                   printf("Result: %d.\n",result);
               }
  
       }
       break;
    }
  
  
  }
  </code></pre>
  
  <p>I have been at this for hours now and I do not understand why my code is acting all crazy. When I enter <code>1</code> it says "Pipe closed and if i enter <code>0</code> it tries to accept another input before giving an error <code>Cannot create pipes!</code>.</p>
  
  <p>I think I am missing something very fundamental here. Any help on the subject is very much appreciated.</p>