title: why can't I pipe output from both execl calls?
body: <p>Possible duplicates:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12596839/how-to-call-execl-in-c-with-the-proper-arguments">How to call execl() in C with the proper arguments?</a></li>
  <li><a href="https://stackoverflow.com/questions/7292642/grabbing-output-from-exec">Grabbing output from exec</a></li>
  <li><a href="https://stackoverflow.com/questions/1734932/linux-pipes-as-input-and-output">Linux Pipes as Input and Output</a></li>
  <li><a href="https://stackoverflow.com/questions/3642732/using-dup2-for-piping">Using dup2 for piping</a></li>
  <li><a href="https://stackoverflow.com/questions/17508626/piping-for-input-output">Piping for input/output</a></li>
  </ul>
  
  <p>I've been trying to learn piping in Linux using dup/dup2 and fork the last 3 days. I think I got the hang of it, but when I call two different programs from the child process, it seems that I am only capturing output from the first one called. <strong><em>I don't understand why that is and/or what I'm doing wrong. This is my primary question.</em></strong> </p>
  
  <p>Edit: I think a possible solution is to fork another child and set up pipes with dup2, but I'm more just wondering why the code below doesn't work. What I mean is, I would expect to capture stderr from the first execl call and stdout from the second. This doesn't seem to be happening.</p>
  
  <p>My second question is if I am opening and closing the pipes correctly. If not, I would like to know what I need to add/remove/change.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/wait.h&gt;
  
  #define READ_END 0
  #define WRITE_END 1
  
  void parentProc(int* stdoutpipe, int* stderrpipe);
  void childProc(int* stdoutpipe, int* stderrpipe);
  
  int main(){
      pid_t pid;
      int status;
  
      int stdoutpipe[2]; // pipe
      int stderrpipe[2]; // pipe
  
      // create a pipe
      if (pipe(stdoutpipe) || pipe(stderrpipe)){
          std::cerr &lt;&lt; "Pipe failed." &lt;&lt; std::endl;
          return EXIT_FAILURE;
      }
  
      // fork a child
      pid = fork();
      if (pid &lt; 0) {
          std::cerr &lt;&lt; "Fork failed." &lt;&lt; std::endl;
          return EXIT_FAILURE;
      } 
      // child process
      else if (pid == 0){
          childProc(stdoutpipe, stderrpipe);  
  
      }
      // parent process
      else {
          std::cout&lt;&lt; "waitpid: " &lt;&lt; waitpid(pid, &amp;status, 0)
               &lt;&lt;'\n'&lt;&lt;std::endl;
          parentProc(stdoutpipe, stderrpipe);
      }
  
      return 0;
  }
  
  
  
  void childProc(int* stdoutpipe, int* stderrpipe){
      dup2(stdoutpipe[WRITE_END], STDOUT_FILENO);
      close(stdoutpipe[READ_END]);
  
      dup2(stderrpipe[WRITE_END], STDERR_FILENO);
      close(stderrpipe[READ_END]);
  
      execl("/bin/bash", "/bin/bash", "foo", NULL);
      execl("/bin/ls", "ls", "-1", (char *)0);
  
      //  execl("/home/me/outerr", "outerr", "-1", (char *)0);
  
      //char * msg = "Hello from stdout";
      //std::cout &lt;&lt; msg;
  
      //msg = "Hello from stderr!";
      //std::cerr &lt;&lt; msg &lt;&lt; std::endl;
  
      // close write end now?
  }
  
  void parentProc(int* stdoutpipe, int* stderrpipe){
      close(stdoutpipe[WRITE_END]);
      close(stderrpipe[WRITE_END]);
  
      char buffer[256];
      char buffer2[256];
      read(stdoutpipe[READ_END], buffer, sizeof(buffer));
      std::cout &lt;&lt; "stdout: " &lt;&lt; buffer &lt;&lt; std::endl;
  
      read(stderrpipe[READ_END], buffer2, sizeof(buffer));
      std::cout &lt;&lt; "stderr: " &lt;&lt; buffer2 &lt;&lt; std::endl;
  
      // close read end now?
  }
  </code></pre>
  
  <p>When I run this, I get the following output:</p>
  
  <pre><code>yfp&gt; g++ selectTest3.cpp; ./a.out 
  waitpid: 21423
  
  stdout: hB�(6
  stderr: foo: line 1: -bash:: command not found
  </code></pre>
  
  <p>The source code for the "outerr" binary (commented out above) is simply:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main(){
      std::cout &lt;&lt; "Hello from stdout" &lt;&lt; std::endl;
      std::cerr &lt;&lt; "Hello from stderr!" &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>When I call "outerr," instead of ls or "foo" I get the following output, which I would expect:</p>
  
  <pre><code>yfp&gt; g++ selectTest3.cpp; ./a.out 
  waitpid: 21439
  
  stdout: Hello from stdout
  
  stderr: Hello from stderr!
  </code></pre>