Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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"

In reply to Re: Forks, Pipes and Exec (file descriptors) by almut
in thread Forks, Pipes and Exec by diabelek

Title:
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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2019-07-17 15:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?