title: Where the switching of parent process and child process is happening here?
body: <p>What the code actually does is takes input from parent process, sends it to child process through pipe. Child process reverses it then sends it back to parent through another pipe. There is no <code>waitpid()</code> or <code>wait()</code> function in the code. </p>
  
  <p>The question is: How the process switching is working here? How <code>write()</code> and <code>read()</code> functions are working here?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;iostream&gt;
  
  #define li long int
  
  using namespace std;
  
  void ReverseAString(char input[])
  {
      li length = strlen(input),i;
      char hold;
  
      for(i=0;i&lt;length/2;i++)
      {
          hold = input[i];
          input[i] = input[length-(i+1)];
          input[length-(i+1)] = hold;
      }
  }
  
  int main()
  {
      pid_t ChildOrParentId;
      int fifoParent[2],fifoChild[2],in;
  
  
      if(pipe(fifoParent)==-1)
      {
          cout&lt;&lt;"Problem in creating Parent's Pipe"&lt;&lt;endl;
          perror("Parent's Pipe");
          exit(1);
      }
  
      if(pipe(fifoChild)==-1)
      {
          cout&lt;&lt;"Problem in creating Child's Pipe"&lt;&lt;endl;
          perror("Child's Pipe");
          exit(1);
      }
  
      ChildOrParentId = fork();
      if(ChildOrParentId==0)
      {
          char buf[100],collected[100];
  
          close(fifoParent[0]);
          close(fifoChild[1]);
          in = 0;
          while(read(fifoChild[0],buf,1)&gt;0)
          {
              collected[in]=buf[0];
              in++;
          }
  
          collected[in]=0;
          cout&lt;&lt;"Read from Child "&lt;&lt;collected&lt;&lt;endl;
          ReverseAString(collected);
          cout&lt;&lt;"After Reversing: "&lt;&lt;collected&lt;&lt;endl;
  
          write(fifoParent[1],collected,sizeof(collected));
          close(fifoParent[1]);
      }
      else
      {
          char buf[100],collected[100];
  
          close(fifoParent[1]);
          close(fifoChild[0]);
          in = 0;
          cout&lt;&lt;"Enter a string: ";
          gets(buf);
  
          write(fifoChild[1],buf,sizeof(buf));
          close(fifoChild[1]);
          while(read(fifoParent[0],buf,1)&gt;0)
          {
              collected[in] = buf[0];
              in++;
          }
  
          collected[in] = 0;
          cout&lt;&lt;"Read from Parent "&lt;&lt;collected&lt;&lt;endl;
  
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>Output window looks like this:</p>
  
  <pre><code>Enter a string: abc // abc input given
  Read from child abc
  After reversing: cba
  Read from parent cba
  </code></pre>