title: Redirecting STDOUT in child process
body: <p>Have a Parent process which spawns multipe child process via fork. I want the log files by  the parent and child process  to be separate.  The Problem is child process STDOUT gets redirected into the parent log file as well as the child log file.  Not sure what i need to change to avoid child process log message to get into the parent log file. Also i dont understand in the below setEnvironment function the purpose of  creating OUT and ERR  file handle.  This is a existing code so i kept as it is.  In the parent process  and child process i set the variable $g_LOGFILE  to contain different file name so that separate log files are created. Also i call setEnvironment function in both parent and child process. I tried by closing STDOUT,STDERR,STDIN in the child process and calling the setenvironment but it was not working properly.  </p>
  
  <pre><code>sub setEnvironment()
  {   
  
    unless ( open(OUT, "&gt;&amp;STDOUT") )
     {
            print "Cannot redirect STDOUT";
            return 2;
      }
      unless ( open(ERR, "&gt;&amp;STDERR") )
      {
            print "Cannot redirect STDERR";
            return 2;
      }
  
  
    unless ( open(STDOUT, "|tee -ai $g_LOGPATH/$g_LOGFILE") )
    {
            print "Cannot open log file $g_LOGPATH/$g_LOGFILE");
            return 2;
     }
     unless ( open(STDERR, "&gt;&amp;STDOUT") )
     {
                  print  "Cannot redirect STDERR");
                  return 2 ;
      }
      STDOUT-&gt;autoflush(1);
  
  } 
  
  
  ####################### Main Program ######################################
  
      $g_LOGFILE="parent.log";
  
    while ($file = readdir(DIR))
   {  
       my $pid = fork;
       if ( $pid ) {
  
          setEnvironment();
          #parent process code goes here
          printf "%s\n", "parent";
          next;
       }
       $g_LOGFILE="child.log";
       setEnvironment();
       #child code goes here
       printf "%s\n", "child";
       exit;
   }
  
  wait for @pids
  </code></pre>