Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Getting a return code from waitpid in Windows

by huskerben (Initiate)
on Jan 20, 2014 at 18:15 UTC ( [id://1071358]=note: print w/replies, xml ) Need Help??


in reply to Re: Getting a return code from waitpid in Windows
in thread Getting a return code from waitpid in Windows

As stated in the original post $? works when using system("cmd...") but when using system(1,"cmd...") with waitpid it always returns -1 whether the system command was successful or not. It seems that it's returning the return code of the waitpid command itself and not the process I was waiting on.

Here is the full code example in case I'm just doing something stupid:
my $wait = 0; my $pid = system(1,"cmd..."); do { $wait = waitpid($pid,WNOHANG); } while ($wait != -1); $rc = $?; print "Command completed with exit code $wait with return code $rc.\n" +;
The output is "Command completed with exit code -1 with return code -1".

If I use the same (purposely bad) command with system("cmd...") then $? returns a value of 256 which is what I would expect when it failed and what I would like to get out of the system(1,"cmd...") call.

Thanks again.

Replies are listed 'Best First'.
Re^3: Getting a return code from waitpid in Windows
by ikegami (Patriarch) on Jan 20, 2014 at 20:11 UTC

    You call waitpid after you've reaped the process, causing causing an error ($? = -1;, error message in $!).

    You gotta check $? after the successful call to waitpid.

    use POSIX qw( WNOHANG ); my $pid = system(1,'perl -e"sleep(4); exit(5);"'); while (1) { my $wait = waitpid($pid, WNOHANG); die $! if $wait < 0; last if $wait > 0; } print "Command completed with exit code $?.\n";

    I presume you actually do something else in the loop? If not, there's no reason to use WHOHANG.

    my $pid = system(1,'perl -e"sleep(4); exit(5);"'); waitpid($pid, 0); die $! if $? < 0; print "Command completed with exit code $?.\n";
Re^3: Getting a return code from waitpid in Windows
by huskerben (Initiate) on Jan 20, 2014 at 18:46 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1071358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found