Hello guys,

I think that perl 5.8 on winXP has a problem with file descripter tables. It fails to 
properly handle parents STDOUT when I dup2 in the child. Here is my script that runs 
fine on my FreeBSD box. It properly redirects childs STDOUT to a file , while leaving 
parents STDOUT to the screen. On an XP box, it redirects both parents and childs 
STDOUT to a file. Any ideas on how to work around it ??

_____cut script______



#!/usr/bin/perl -w

# $? child error
# $$ pid


use POSIX qw ( :sys_wait_h :unistd_h);
use IO::Handle;


my $CHILD_SPAWNED=0;
my $CHILD_TO_CREATE=5;
my $MAX_CHILD=3;
my $CHILD_CREATED=0;
$|=1;



#open a file for childs redirection
open(FD,"> ./output") || myerrno();




while( $CHILD_SPAWNED < $CHILD_TO_CREATE ){

print  "-------\t\t RUNING $CHILD_CREATED------------\n";

if( $CHILD_CREATED < $MAX_CHILD ){
 

 die "couldnt not spawn: $!" unless defined ( my $PID=fork() );

 $CHILD_CREATED++; 
 $CHILD_SPAWNED++;


 unless( $PID ){
  $|=1;
  dup2(fileno(FD) , fileno(STDOUT) ) || myerrno();
  close(FD);
  $^F=3; #make the redirection get past exec
  print "I am child $$...about to exec\n";

 { exec ("perl count.pl") }; print STDERR "could not exec: $!";
 
 }


}


$status=waitpid(-1,WNOHANG);

if( $status != 0 ){ #some child has exited
 $CHILD_CREATED--;

 print "child $status has exited \n";
}




}


print  "TOTAL CHILD SPAWNED IS $CHILD_SPAWNED\n";


sub myerrno{
 foreach $Val (keys( %!) ){
  print "$Val $!{$Val}\n" if $!{$Val}; 
 
 }
exit(1);
}


____paste script_____

Reply via email to