Using the always-recommended use strict; use warnings; reveals one problem with your code:
Bareword "WNOHANG" not allowed while "strict subs" in use
Another is that only the parent process should wait for the child process; if doesn't make any sense to wait for anything in the child.
In addition, doing a loop with non-blocking waitpid is useless; a single call to a blocking waitpid makes more sense. Or if you have only one child process, wait is even simpler.
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:
#!/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";
}
-
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.
|