I may have answered my own question... I still can't figure out how to get it to work with waitpid but it does work properly when using just plain "wait". The reason I wasn't doing that before is that it is a blocking call so it prevents me from looping through checking each PID I kicked off previously. However, it seems that it blocks until any of the processes returns. The wait call itself returns the PID that just terminated and just after $? returns the status. So presumably I can collect all my PIDs as I kick them off then loop through on a blocking wait one by one waiting for each of those PIDs to terminate and collect the status as they do.
The following seems to do what I want:
my $pid1Complete = 0;
my $pid2Complete = 0;
my $pid3Complete = 0;
my $pid1 = system(1,"cmd...");
my $pid2 = system(1,"cmd...");
my $pid3 = system(1,"cmd...");
my $finishingPID = 0;
do {
$finishingPID = wait();
$rc = $?;
if ($finishingPID == $pid1)
{
$pid1Complete = 1;
print "PID1 finished with return code $rc\n";
}
elsif ($finishingPID == $pid2)
{
$pid2Complete = 1;
print "PID2 finished with return code $rc\n";
}
elsif ($finishingPID == $pid3)
{
$pid3Complete = 1;
print "PID3 finished with return code $rc\n";
}
} while (($pid1Complete == 0) || ($pid2Complete == 0) || ($pid3Complet
+e == 0));
If anybody has any suggestions for improvements or other ideas I'd be happy to hear them.
Thanks again.
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.