title: How to capture the stdout of a child process using fork/exec/pipe/dup2 only?
body: <p>so I'm trying to capture the output of a child process using fork/exec/pipe/dup2 only but the output keeps printing to terminal instead. This is my code right now:</p>
  <pre><code>import os
  import sys
  
  pipeIn, pipeOut = os.pipe()
  processid = os.fork()
  if processid == 0:
      try:
          os.close(pipeIn)
          os.dup2(pipeOut, 0)
          os.execv(sys.executable, [sys.executable, 'helloWorld.py'])
          sys.exit(0)
      except FileNotFoundError:
          print(&quot;ERROR: File not found.&quot;)
          sys.exit(1)
  elif processid == -1:
      print(&quot;ERROR: Child was unable to run.&quot;)
      sys.exit(1)
  else:
      wait = os.wait()
      if wait[1] == 0:
          os.close(pipeOut)
          output = os.read(pipeIn, 100)
      else:
          output = &quot;ERROR&quot;
  </code></pre>
  <p>Can anyone tell me what's going wrong? And how I can capture the output of the exec command instead of printing it to terminal. (No temp files are allowed either).</p>