title: How do I synchronize father and child processes using pipes?
body: <p>I believe there's a way to synchronize two processes using pipes, but I'm unsure on how to implement it. In my code both the child and father process execute its code at the same time, I would like for one process to wait for the other. For example, using a pipe to block one process until the other process is done.</p>
  
  <p>My code:</p>
  
  <pre><code>int main()
  {
  
  int one[2];
  int two[2];
  int x=0;
  char messageRead[256], messageRead2[256], messageWrite[256], messageWrite2[256];
  
  pipe(one);
  pipe(two);
  
  pid_t pid = fork();
  
  if(pid == 0)  //child process
  {
  
  while (x==0)   //loop until condition is met (didn't write the condition yet, just testing)
  {
  std::cout &lt;&lt; "Child process.\n\n";
  std::cin &gt;&gt; messageWrite;
  write(one[1], messageWrite, 256);
  read(two[0], messageRead2, 256 );
  }
  }
  
  else if(pid&gt;0)  //father process
  {
  while(x==0)
  {
  std::cout &lt;&lt; "Father process.\n\n";
  std::cin &gt;&gt; messageWrite;
  write(two[1], messageWrite2, 256);
  read(one[0], messageRead, 256);
  }
  }
  }
  </code></pre>
  
  <p>Right now, both processes go back and forth, like I want but they both execute at the same time like this:</p>
  
  <pre><code>Father process.
  
  Child process.
  
  user input here
  user input here
  Father process.
  
  Child process.
  user input here
  user input here
  Child process.
  
  Father process.
  user input here
  user input here
  
  etc...
  </code></pre>