A while back I first started working with using
fork in a script. After a number of
PM posts, I had some working code. The problem now, I guess is that it works too well. I'm curious to know how to set an upper limit on the number of forked child processes. In some instances, my script would be able to break out over 2000 that could tax my server quite a bit. I'd like to be able to have the script have no more than 50 processes at a time.
I'm currently working with the following code base:
for my $i(@nodes) {
if ($fork) {
if ($pid = fork) {
next;
} elsif (defined($pid)) {
&do_something;
exit;
} else {
warn "Problem found in forking\n";
}
} else {
&do_something;
}
}
while (wait() != -1) {
return 1 if ($?);
}
I'm not really sure at all how to achieve want I want in this case. Every solution that I have tried to come up with ends up only forking the first 50 processes and then ends up skipping the remaining in my list as my for loop just continues on its way. Can someone offer a suggestion on how I can accomplish this?
thanks -c
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.