There are a couple more options.
You could just ignore the CHLD signal with -
$SIG{CHLD} = 'IGNORE'
And let the init process clean up the defunct child. Obviously if you need the exit status of the child or if you need to keep track of which children have finished, this won't work.
Another option is to reap the children when you are ready.
use strict;
use POSIX ":sys_wait_h";
while (1) {
for(1..10) {spawn(\&foo)} # fork off 10 &foo()'s
my $s = sleep (2); # $s will hold the number of se
+conds actually slept
# clean up our children
1 while ((my $child = waitpid(-1, WNOHANG)) > 0);
print "---";
if ($s) { print "Slept for $s seconds\n"; }# If we slept at all
else { print "Did not sleep at all\n"; }# If we didn't
}
sub spawn {
return if fork(); # Fork and return the parent
exit shift->(); # exec the coderef passed as ar
+g
}
sub foo {
print "*" for (1..5);
}
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.