Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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?

In reply to Forks, Pipes and Exec by diabelek

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2024-07-24 09:59 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.