title: "Ping pong" game with unix processes and pipes
body: <p>Can someone please help me fix this problem?</p>
  
  <p>I wrote the complete problem below.</p>
  
  <p>Ping-pong. Two processes will play the ping-pong game.
  The first process will generate a random number between 5000 and 15000 that will be send to the other process.
  This process will subtract a random value (between 50 and 1000) and will send the number back,
  The chat between the processes will be implemented using pipe channels.
  The game ends when the value is below zero.
  Each process will print the received value. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int main ()
  {
  
    int  p2c[2], c2p[2], number, n;
  
    pipe(p2c);    //parent to child pipe
    pipe(c2p);    //child to parent pipe
  
    number = rand() %10000 + 5000;
    printf("Generated number: %d\n", number);
  
    if (fork () == 0 ) 
    { //child process
        while(number &gt; 0)
        {
           printf("in child process\n");
           read(p2c[0], &amp;number, sizeof(int));
           printf("(F).received number: %d\n", number);
           n = rand() %1000 + 50;
           number = number - n;
           write(c2p[1], &amp;number, sizeof(int));
         } 
        close(p2c[0]); close(p2c[1]);
        close(c2p[0]); close(c2p[1]);
        exit(0);
  
    } else {
      while(number &gt; 0) 
      {
        printf("in parent process\n");
        read(c2p[0], &amp;number, sizeof(int));
        printf("(P).received number: %d\n", number);
        number = number - n;
        write(p2c[1], &amp;number, sizeof(int));
      }
     close(p2c[0]); close(p2c[1]);
     close(c2p[0]); close(c2p[1]);
     wait();
   }
  printf ("The final number is: %d\n", number);
  }
  </code></pre>
  
  <p>It does not work and I don't understand why. All that it prints is:</p>
  
  <pre><code>Generated number: (a random number...)
  in parent process
  in child process
  </code></pre>
  
  <p>Also I don't understand why it first goes to the parent process and no to the child process. Can someone please help me fix this?</p>