title: fork() and STDOUT/STDERR to the console from child processes
body: <p>I'm writing a program that forks multiple child processes and I'd like for all of these child processes to be able to write lines to <code>STDERR</code> and  <code>STDOUT</code> without the output being garbled. I'm not doing anything fancy, just emitting lines that end with a new line (that, at least in my understanding would be an atomic operation for Linux). From perlfaq it says:</p>
  
  <blockquote>
    <p>Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with opening a pipe (see open) but on some systems this means that the child process cannot outlive the parent.</p>
  </blockquote>
  
  <p>It says I should "close or reopen" these filehandles for the child. Closing is simple, but what does it mean by "reopen"? I've tried something like this from within my child processes and it doesn't work (the output still gets garbled):</p>
  
  <pre><code>open(SAVED_STDERR, '&gt;&amp;', \*STDERR) or die "Could not create copy of STDERR: $!";
  close(STDERR);
  
  # re-open STDERR
  open(STDERR, '&gt;&amp;SAVED_STDERR') or die "Could not re-open STDERR: $!";
  </code></pre>
  
  <p>So, what am I doing wrong with this? What would the pipe example it alludes to look like? Is there a better way to coordinate output from multiple processes together to the console?</p>