title: Not understanding how the pipe works. (Shared memory between processes UNIX)
body: <p>I am not understanding how the pipe works in UNIX, I did this code and I stumbled in a strange fact. The trace of the exercise can be found on the top of the code. Ill'explain what I can't get. In the father process when I want to print the values from the pipe, the value from where the &quot;i&quot; variable begins can be whatever number. I put &quot;4&quot; but it works for every number 2, 3 ,4 excetera.
  How can it work every time?</p>
  <pre><code>/*****************************************************************
      The candidate should complete the program provided, implementing
      the main.
      The program creates a child process; the child process reads
      from the keyboard an integer N &gt;= 0, and transmits the values N, N-1, N-2, N-3, ..., 0
      (inclusive) to the parent process via a pipe.
      The father process reads from the pipe the values transmitted by the child process
      and prints them, until it receives the value 0; then the father process
      process waits for the termination of the child process and terminates.
      
      Example:
      I am the child process. Enter a number &gt;=0: 4
      I am the father process. I have received: 4
      I am the father process. I have received: 3
      I am the father process. I have received: 2
      I am the father process. I have received: 1
      I am the father process. I have received: 0
      I am the father process. The son has finished.
      
  ******************************************************************/
      
      
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
      
  int main(int argc, char *argv[]) 
  {
      
      int fd[2];
      int num, i;
      //Pipe creation
      if(pipe(fd)&lt;0)
      {
          printf(&quot;Pipe creation failed\n&quot;);
          return 1;
      }
      //Creating a child process
      pid_t pid=fork();
      //Fork check
      if(pid&lt;0)
      {
          printf(&quot;Fork failed\n&quot;);
          return 1;
      }
      //Entering the child process
      else if(pid==0){
          close (fd[0]);  // Not interested in reading
          printf(&quot;I am the child process\n&quot;);
          //Acquiring a number from input
          printf(&quot;Give me a number: &quot;);
          scanf(&quot;%d&quot;, &amp;num);
          //Sending the numbers trough a pipe
          for(i=num; i&gt;=0; i--)
          {
              int sent=write(fd[1], &amp;i, sizeof(num));
              //Check on the number of bytes the function wrote
              if(sent&lt;0 || sent&lt;sizeof(num))
              {
                  printf(&quot;Error when sending\n&quot;);
                  return 1;
              }
          }
          close (fd[1]);
          return 0;
      }
      //Entering the father process
      if(pid&gt;0)
      {
          //Father process
          wait(NULL);
          close (fd[1]);// Not interested in writing
          for(i=4;i&gt;=0;i--)//4 is a random number and it still works
          {
              int ricevuti=read(fd[0], &amp;i, sizeof(num));
              //Check on the number of bytes the function read
              if(ricevuti&lt;0 || ricevuti&lt;(sizeof(num)))
              {
                  printf(&quot;Error when receiving\n&quot;);
                  return 1;
              }
              //Printing the values read by the function
              else
              {
                  printf(&quot;I am the father process and i received: %d\n&quot;, i);
              }
          }
          printf(&quot;The child process has terminated\n&quot;);
      }
      close (fd[0]);
      return 0;
  }
  </code></pre>