J
john
Hello everyone,
I have a program which forks multiple children and needs to have
bidirectional communication with each child. Each child needs to use
STDIN and STDOUT to talk back to the parent.
The problem is that I'm having trouble connecting the pipe to STDIN of
the child. I've tried various approaches. A simple testcase is below.
In this case, the child seems to read the number (I think) of the pipe
file descriptor rather than the string I send down the pipe.
perl -v shows...
This is perl, v5.8.2 built for aix-thread-multi
Can anyone tell me where I'm going wrong ?
Thanks in advance.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;
use IO:
ipe;
my $cr = IO::Handle->new(); # child reader
my $pw = IO::Handle->new(); # child writer
new IO:
ipe($cr, $pw) or die 'pr/cw pipe';
my $pid = fork();
if ($pid > 0) {
# Parent
print {$pw} "hello!\n"; # sending to the child
close($pw);
} else {
# Child
# Connect stdin to pipe
close($pw);
my $fd = fileno($cr);
open(my $stdin, "<", \$fd) or die "STDIN open: $!";
*STDIN = $stdin;
my $line = <STDIN>; # hopefully reading from the parent
chomp $line;
print "child received: ($line)\n";
close($cr);
exit(0);
}
wait;
I have a program which forks multiple children and needs to have
bidirectional communication with each child. Each child needs to use
STDIN and STDOUT to talk back to the parent.
The problem is that I'm having trouble connecting the pipe to STDIN of
the child. I've tried various approaches. A simple testcase is below.
In this case, the child seems to read the number (I think) of the pipe
file descriptor rather than the string I send down the pipe.
perl -v shows...
This is perl, v5.8.2 built for aix-thread-multi
Can anyone tell me where I'm going wrong ?
Thanks in advance.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;
use IO:
my $cr = IO::Handle->new(); # child reader
my $pw = IO::Handle->new(); # child writer
new IO:
my $pid = fork();
if ($pid > 0) {
# Parent
print {$pw} "hello!\n"; # sending to the child
close($pw);
} else {
# Child
# Connect stdin to pipe
close($pw);
my $fd = fileno($cr);
open(my $stdin, "<", \$fd) or die "STDIN open: $!";
*STDIN = $stdin;
my $line = <STDIN>; # hopefully reading from the parent
chomp $line;
print "child received: ($line)\n";
close($cr);
exit(0);
}
wait;