How do I implement open(FOO, "foo |") with fork()?

Discussion in 'Perl Misc' started by fishfry, Jul 2, 2005.

  1. fishfry

    fishfry Guest

    What I mean is, when I call fork(), what is the sequence of fd
    manipulations to get the output of the child process redirected so that
    the parent can read it?
    fishfry, Jul 2, 2005
    #1
    1. Advertising

  2. fishfry

    Anno Siegel Guest

    fishfry <> wrote in comp.lang.perl.misc:
    > What I mean is, when I call fork(), what is the sequence of fd
    > manipulations to get the output of the child process redirected so that
    > the parent can read it?


    Open a pipe with READHANDLE and WRITEHANDLE before you fork. Then
    fork and -- very important -- close READHANDLE in the child and
    WRITEHANDLE in the parent. Now the parent can read from READHANDLE
    everything the child writes to WRITEHANDLE. If you want to capture
    the child's STDOUT, open STDOUT as a duplicate to WRITEHANDLE.

    pipe R, W;
    defined( my $pid = fork) or die "fork: $!";
    if ( $pid ) {
    close W;
    print while <R>;
    } else {
    close R;
    open STDOUT, '>&', fileno W;
    print "$_\n" for qw( gaga gigi gugu);
    }

    Anno
    Anno Siegel, Jul 2, 2005
    #2
    1. Advertising

  3. fishfry

    Paul Lalli Guest

    Anno Siegel wrote:
    > pipe R, W;
    > defined( my $pid = fork) or die "fork: $!";
    > if ( $pid ) {
    > close W;
    > print while <R>;
    > } else {
    > close R;
    > open STDOUT, '>&', fileno W;
    > print "$_\n" for qw( gaga gigi gugu);
    > }


    Unknown open() mode '>&' at ./fork.pl line 12.

    How is that duplication of STDOUT supposed to be formed?

    Paul Lalli
    Paul Lalli, Jul 2, 2005
    #3
  4. fishfry

    Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > pipe R, W;
    > > defined( my $pid = fork) or die "fork: $!";
    > > if ( $pid ) {
    > > close W;
    > > print while <R>;
    > > } else {
    > > close R;
    > > open STDOUT, '>&', fileno W;
    > > print "$_\n" for qw( gaga gigi gugu);
    > > }

    >
    > Unknown open() mode '>&' at ./fork.pl line 12.


    Works with 5.8.6. Alternatively, use

    open STDOUT, '>&' . fileno W;

    > How is that duplication of STDOUT supposed to be formed?


    It's the write end of the pipe that's duplicated, STDOUT becomes the
    duplicate. Otherwise, duplication of filehandles is a standard feature
    of open(), but you know that. I'm not quite sure how you mean your
    question.

    Anno
    Anno Siegel, Jul 2, 2005
    #4
  5. fishfry

    Anno Siegel Guest

    Christian Winter <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > pipe R, W;
    > > defined( my $pid = fork) or die "fork: $!";
    > > if ( $pid ) {
    > > close W;
    > > print while <R>;
    > > } else {
    > > close R;

    >
    > You accidentially left out the vital
    > close STDOUT;
    > here.


    Opening a file handle implies closing of the current one if it is open.
    When would explicit close of STDOUT make a difference?

    >
    > > open STDOUT, '>&', fileno W;
    > > print "$_\n" for qw( gaga gigi gugu);
    > > }


    Anno
    Anno Siegel, Jul 2, 2005
    #5
  6. fishfry

    Paul Lalli Guest

    Anno Siegel wrote:
    > Paul Lalli <> wrote in comp.lang.perl.misc:
    > > Anno Siegel wrote:
    > > > pipe R, W;
    > > > defined( my $pid = fork) or die "fork: $!";
    > > > if ( $pid ) {
    > > > close W;
    > > > print while <R>;
    > > > } else {
    > > > close R;
    > > > open STDOUT, '>&', fileno W;
    > > > print "$_\n" for qw( gaga gigi gugu);
    > > > }

    > >
    > > Unknown open() mode '>&' at ./fork.pl line 12.

    >
    > Works with 5.8.6. Alternatively, use
    >
    > open STDOUT, '>&' . fileno W;


    Huh. Interesting. 5.8.0 gives that error. Another reason to convince
    TPTB to upgrade.

    > > How is that duplication of STDOUT supposed to be formed?

    >
    > It's the write end of the pipe that's duplicated, STDOUT becomes the
    > duplicate. Otherwise, duplication of filehandles is a standard feature
    > of open(), but you know that. I'm not quite sure how you mean your
    > question.


    I just meant I thought there was a syntax error in your code, and was
    asking for a correction. (Well, there _was_.... if you were using
    5.8.0, but you weren't, so there wasn't...) :)

    Paul Lalli
    Paul Lalli, Jul 2, 2005
    #6
  7. fishfry

    Anno Siegel Guest

    Christian Winter <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Christian Winter <> wrote in comp.lang.perl.misc:
    > >
    > >>Anno Siegel wrote:
    > >>
    > >>> pipe R, W;
    > >>> defined( my $pid = fork) or die "fork: $!";
    > >>> if ( $pid ) {
    > >>> close W;
    > >>> print while <R>;
    > >>> } else {
    > >>> close R;
    > >>
    > >>You accidentially left out the vital
    > >> close STDOUT;
    > >>here.

    > >
    > >
    > > Opening a file handle implies closing of the current one if it is open.
    > > When would explicit close of STDOUT make a difference?

    >
    > Even though "perldoc perlopen" only mentions that for


    ITYM "perldoc -f open".

    > in-memory files (i.e. scalars) i have experienced that
    > this also applies to pipes on my platform (testet here
    > on ActiveState, v5.8.0 built for MSWin32-x86-multi-thread).
    > If I omit the explicit close, the program will run into
    > a deadlock.


    That would be a bug in Perl then. Closing a filehandle before
    re-opening it is a fundamental property of Perl's open (under
    Unix, it maps directly to the corresponding property of freopen()).

    I never noticed the exception with in-memory files.

    Anno
    Anno Siegel, Jul 4, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    379
    JaredGrubb
    Sep 24, 2008
  3. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
  4. Lucky Nl
    Replies:
    2
    Views:
    83
    Matthew Bloch
    Jun 10, 2010
  5. Replies:
    4
    Views:
    131
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page