note
moritz
<p>Using the always-recommended <c>use strict; use warnings;</c> reveals one problem with your code:
<code>
Bareword "WNOHANG" not allowed while "strict subs" in use
</code>
<p>Another is that <b>only the parent process should wait for the child process</b>; if doesn't make any sense to wait for anything in the child.</p>
<p>In addition, doing a loop with non-blocking waitpid is useless; a single call to a blocking [doc://waitpid] makes more sense. Or if you have only one child process, [doc://wait] is even simpler.</p>
<p>Finally, afer the fork there are two writers; one in the child, one in the parent process. You need to close the one in the writer too:
<code>
#!/usr/bin/perl
# always uses these;
use strict;
use warnings;
# and detect errors
use autodie;
pipe(READER, WRITE);
my $pid =fork();
if ($pid)
{
print "In parent\n";
close WRITE;
my $val1 = 100;
while(my $num = <READER>)
{
print "in while; $num\n";
}
close(READER);
wait;
}
else
{
print "In child\n";
my $val2 = 150;
print WRITE "$val2\n";
close(WRITE);
print "Child closed write\n";
}
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-616540">
[http://perl6.org/|Perl 6 - the future is here, just unevenly distributed]
</div></div>
1086159
1086159