If you are checking whether pid is defined, you should
do more with it, otherwise you can just say
if ($pid=fork)
A fork command can return three possible values:
- A positive integer, representing the PID (process ID),
in other words, the name of the child. This means that the fork was successful, and
you are the parent.
- A zero. This means the fork was successful, and you are the child. You can get the
name of your parent with the getppid command.
- Undefined, which means that the fork failed, and therefore, you are the
"parent" (who may have other children, but this attempt failed). When this happens,
you can look to see what is in the $! variable to try and figure out what went wrong.
Usually, it's because A) You can't fork (e.g. an MS-DOS shell) or B) you have no more
processes available. This is a bad thing. Not fatal, but bad. You could sleep
and try again (see page 167 of Programming Perl) but best to
just 'die' and figure out what's going on.
To further the previous poster's comment, you can not only get rid of the
return, but the whole sub as well:
#!/usr/bin/perl
$loops = shift || 200;
use LWP::Simple qw(get);
print "Testing with $loops children...\n";
for ($i=0;$i<$loops;$i++) {
if ($pid = fork) { next; } ## Parent gets the credit...
if (defined $pid) { ## ...children do the work
get("http://www.blahblah.com/blah.php3?xxx");
exit;
}
else { ## Uh-oh something is really wrong
## The parent is saying this...
die "Fork failed at number $i: $!\n";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.