develooper Front page | perl.perl5.porters | Postings from March 2003

[perl #21776] dup2 on XP

From:
mark
Date:
March 31, 2003 18:30
Subject:
[perl #21776] dup2 on XP
Message ID:
rt-21776-54411.17.9690865471908@bugs6.perl.org
# New Ticket Created by  mark 
# Please include the string:  [perl #21776]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21776 >


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_____





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About