#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11123851
use warnings;
use IO::Pipe;
use IO::Handle;
use IO::Select;
my $pipe = IO::Pipe->new();
my $pid = fork();
die "failed to fork: $!" unless defined $pid;
if ($pid) {
$pipe->reader();
my $select = IO::Select->new();
$select->add($pipe);
my $line = '';
while (1) {
my @ready = $select->can_read(1);
foreach my $h (@ready) {
# my $line = <$h>;
if( sysread $h, $line, 8192, length $line )
{
# chomp $line;
while( $line =~ s/(.*)\n// )
{
print "PARENT sees <$1>\n";
}
}
else { close $pipe };
}
}
}
else {
$pipe->writer();
$pipe->autoflush(1);
my $c = 0;
while (1) {
$c++;
print "CHILD writing <$c>\n";
$pipe->print("$c\n") or die;
sleep 1 unless $c % 5; # pause after each fifth line
}
}
CHILD writing <1>
CHILD writing <2>
CHILD writing <3>
CHILD writing <4>
CHILD writing <5>
PARENT sees <1>
PARENT sees <2>
PARENT sees <3>
PARENT sees <4>
PARENT sees <5>
CHILD writing <6>
CHILD writing <7>
CHILD writing <8>
CHILD writing <9>
CHILD writing <10>
PARENT sees <6>
PARENT sees <7>
PARENT sees <8>
PARENT sees <9>
PARENT sees <10>
CHILD writing <11>
CHILD writing <12>
CHILD writing <13>
CHILD writing <14>
CHILD writing <15>
PARENT sees <11>
PARENT sees <12>
PARENT sees <13>
PARENT sees <14>
PARENT sees <15>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|