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

F

fishfry

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?
 
A

Anno Siegel

fishfry said:
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
 
P

Paul Lalli

Anno said:
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
 
A

Anno Siegel

Paul Lalli said:
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
 
A

Anno Siegel

Christian Winter said:
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?

Anno
 
P

Paul Lalli

Anno said:
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.
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
 
A

Anno Siegel

Christian Winter said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top