Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Forks, Pipes and Exec

by diabelek (Beadle)
on Oct 29, 2008 at 22:16 UTC ( #720352=perlquestion: print w/ replies, xml ) Need Help??
diabelek has asked for the wisdom of the Perl Monks concerning the following question:

I can't seem to get an exec call to print to a redefined STDOUT that is actually a pipe. Here's the code where I create the pipe and the child process


# dup stdout so we can restore it later
   if( !open( $old_stdout, ">&STDOUT" ) ) {
      return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
   }
   close( STDOUT );

   # create the piped stdout
   pipe( $smash_stdout, STDOUT );

   # set non blocking and unbuffered
   my $flags = fcntl( STDOUT, F_GETFL, 0 );
   fcntl( STDOUT, F_SETFL, $flags | O_NONBLOCK );
   select((select(STDOUT), $|=1)[0]);

   if( ($pid = fork()) ) {
      # parent process
      # reset out STDOUT
      if( !open( STDOUT, ">&", $old_stdout ) ) {
         return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
      }
      close( $old_stdout );
      $target->{stdout} = $smash_stdout;
      $target->{buffer} = "";
   } else {
      exec ($cmd);
      exit( -1 );
   }

And then later on in the parent thread, I run the following


      foreach $target (@{$self->{TARGETS}}) {
         if( defined( $target->{stdout} ) ) {
            my $smash_fh = $target->{stdout};
            $target->{buffer} .= <$smash_fh>;
         }
      }

If I print something to STDOUT before I exec, I read that through the pipe but the exec routine doesn't seem to send it through the pipe. I've deleted the code to redo stdout and the exec will print to the screen so the exec'ed program is printing, just not to my new STDOUT. Is there something different that I need to do?

Comment on Forks, Pipes and Exec
Re: Forks, Pipes and Exec (file descriptors)
by almut (Canon) on Oct 29, 2008 at 23:14 UTC

    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"

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://720352]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-16 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (6 votes), past polls