http://qs1969.pair.com?node_id=720360


in reply to Forks, Pipes and Exec

The problem presumably is the following: the way you're connecting STDOUT to the pipe is changing the file descriptor number so that it's no longer 1. However, file descriptor 1 is what your exec'ed child process is assuming to be stdout (i.e. the default).

printf STDERR "before: fileno(STDOUT)=%d\n", fileno(STDOUT); open $old_stdout, ">&STDOUT" or die "open: $!"; close( STDOUT ); pipe( $smash_stdout, STDOUT ); printf STDERR "after: fileno(STDOUT)=%d\n", fileno(STDOUT);

prints

before: fileno(STDOUT)=1 after: fileno(STDOUT)=6

Note that fileno changed from 1 to 6.  In other words, you now have fileno 6 connected to the pipe (something an exec'ed child would know nothing about...)

Something like this might work better

printf STDERR "before: fileno(STDOUT)=%d\n", fileno(STDOUT); open $old_stdout, ">&STDOUT" or die "open: $!"; # _don't_ explicitly close STDOUT here local(*RH, *WH); pipe RH, WH; open STDOUT, ">&WH" or die "open: $!"; printf STDERR "after: fileno(STDOUT)=%d\n", fileno(STDOUT);

because this way, STDOUT keeps being associated with file descriptor 1:

before: fileno(STDOUT)=1 after: fileno(STDOUT)=1

Update: a simple demo:

open $old_stdout, ">&STDOUT" or die "open: $!"; local(*RH, *WH); pipe RH, WH; open STDOUT, ">&WH" or die "open: $!"; # write something to the pipe print "foo"; # and have some fork/exec'ed process write to the pipe (via stdout) system('echo bar'); close WH; my $out = <RH>; print STDERR "got from pipe: $out"; # $out is "foobar\n"

Replies are listed 'Best First'.
Re^2: Forks, Pipes and Exec (file descriptors)
by diabelek (Beadle) on Oct 30, 2008 at 19:05 UTC

    Thanks, that seems to have done it. I guess I didn't think of the filenumber changing since the print("foobar") worked before I execed the other process.

    almut++

      Update...

      This works great in Linux but now I'm trying to get it working in Windows. The problem is that Windows doesn't seem to have the process print to the pipe that's attached to stdout. The only thing I can gather from testing variations is that STDOUT is the same for both "processes" in Windows. If I sleep and let the child run first or don't restore STDOUT, the pipe works as designed. As soon as I restore STDOUT, the pipe is broken even though they are separate processes.

      Is there any way of doing this in Windows?

      print "before the pipe\n"; open $old_stdout, ">&STDOUT" or die "open: $!"; local(*WH); pipe $rh, WH; open STDOUT, ">&WH" or die "open: $!"; STDOUT->blocking(0); # write something to the pipe # and have some fork/exec'ed process write to the pipe (via stdout) if( fork() ) { open( STDOUT, ">&", $old_stdout ) or die "open: $!"; print STDERR "I'm the parent\n"; sleep 3; } else { exec( 'echo fubar' ); exit( 0 ); } close WH; my $out = <$rh>; print "got from pipe: $out"; # $out is empty since fubar printed to +the screen