title: Linux pass value between parent process and child process using pipe in c?
body: <p>A Process creates a child process, increase x by 1, then send x value to the child process, the child process can then multiply x by 10, and pass x back to the parent process and so on. You should at least do this 5 times.</p>
  
  <p>the output should look like this:</p>
  
  <p>initial value 0</p>
  
  <p>Parent:</p>
  
  <p>x value after operation:1</p>
  
  <p>Child:</p>
  
  <p>x value after operation:10</p>
  
  <p>Parent:</p>
  
  <p>x value after operation:11</p>
  
  <p>Child:</p>
  
  <p>x value after operation:110</p>
  
  <p>Parent:</p>
  
  <p>x value after operation:111</p>
  
  <p>Child</p>
  
  <p>x value after operation:1110</p>
  
  <p>What i have is the following....... </p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/syscall.h&gt;
  
  #define READ 0
  #define WRITE 1
  
  int main()
  {
    pid_t pid;
    int mypipefd[2];
    int ret;
    int i;
    int x=0;
    int result,result2;
  
    ret = pipe(mypipefd);
  
    if(ret ==-1)                    //test for pipe
      {
        perror("pipe");             //show error and exit  
        exit(1);
      }
  
    printf("initial value %d\n", x);   
  
    pid = fork();
  
    for(i=0;i&lt;5;i++)
  
  {
  
    if(pid == 0)
      {
          /*Child process*/
  
    result2=result*10;
    write(mypipefd[WRITE],&amp;result2,sizeof(result2));
    printf("Child:\nx value after operation: %d\n", x);
    close(mypipefd[WRITE]);
    read(mypipefd[READ],&amp;result2,sizeof(result2));
  
    printf("=======================================\n");
    close(mypipefd[READ]);
  
  }
  
    else if(pid &gt; 0) 
  {
     /*Parent process*/
  
   result=x++;
   write(mypipefd[READ],&amp;result,sizeof(result));   
   printf("Parent:\nx value after operation: %d\n", x); 
   close(mypipefd[WRITE]);
   read(mypipefd[WRITE],&amp;result,sizeof(result)); 
  
   printf("=======================================\n");
   close(mypipefd[READ]);
   exit(0);
  
  }
  
   else
  {
    perror("fork");
    exit(1);
      }
  
    }
  
  }
  </code></pre>
  
  <p>The question is, what's wrong with my code? I tried to READ and WRITE back the the pipe but seems not working...</p>
  
  <p>What's the output of my code right now:</p>
  
  <p>initial value 0</p>
  
  <p>Parent:</p>
  
  <p>x value after operation:1</p>
  
  <p>Child:</p>
  
  <p>x value after operation:0</p>
  
  <p>Child:</p>
  
  <p>x value after operation:0</p>
  
  <p>Child:</p>
  
  <p>x value after operation:0</p>
  
  <p>Child:</p>
  
  <p>x value after operation:0</p>
  
  <p>Child:</p>
  
  <p>x value after operation:0</p>