title: How many file descriptors created/involved after running this program - pipe
body: <p>From the below program,</p>
  
  <pre><code>/***************************************************************************** 
   MODULE: popen.c
   *****************************************************************************/
  #include &lt;stdio.h&gt;
  
  int main(void)
  {
          FILE *pipein_fp, *pipeout_fp;
          char readbuf[80];
  
          /* Create one way pipe line with call to popen() */
          if (( pipein_fp = popen("ls", "r")) == NULL)
          {
                  perror("popen");
                  exit(1);
          }
  
          /* Create one way pipe line with call to popen() */
          if (( pipeout_fp = popen("sort", "w")) == NULL)
          {
                  perror("popen");
                  exit(1);
          }
  
          /* Processing loop */
          while(fgets(readbuf, 80, pipein_fp))
                  fputs(readbuf, pipeout_fp);
  
          /* Close the pipes */
          pclose(pipein_fp);
          pclose(pipeout_fp);
  
          return(0);
  }
  </code></pre>
  
  <hr>
  
  <p><code>popen.c</code> is compiled to <code>my_program</code></p>
  
  <p>Here is my understanding of the file descriptors created/involved after executing <code>my_program</code>, <code>popen()</code> forks &amp; execs the <code>my_program</code> process but child processes does not inherit the pipe file descriptors of <code>my_program</code>.</p>
  
  <p>So, After exec, </p>
  
  <p>1) write file descriptor is only created for <code>ls</code></p>
  
  <p>2) read file descriptor is only created for <code>sort</code></p>
  
  <p>3) read and write files descriptors are created in <code>my_program</code>, because <code>ls</code> writes to <code>my_program</code> &amp; <code>sort</code> reads from <code>my_program</code></p>
  
  <p><a href="https://i.stack.imgur.com/iBcmR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iBcmR.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>As shown above, Are these the only file descriptors involved/created?</p>
  
  <p><em>Note: 'in' &amp; 'out' are just naming conventions used here</em></p>