Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I implement open(FOO, "foo |") with fork()?

Reply
Thread Tools

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

 
 
fishfry
Guest
Posts: n/a
 
      07-02-2005
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?
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-02-2005
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-02-2005
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-02-2005
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-02-2005
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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-02-2005
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-04-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Column: Implement WPA2-Personal wireless security =?Utf-8?B?a2g=?= Wireless Networking 37 05-18-2006 09:04 PM
How to implement PEAP-EAP-TLD authentication? Edward W. Ray Wireless Networking 4 05-26-2005 10:14 PM
can we implement LIFO using SRL16 ??? Oleg VHDL 5 02-18-2004 10:29 PM
how to implement gated clock and gated partial circuit in VHDL? walala VHDL 3 09-23-2003 10:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57