Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would waitpid in Perl return wrong exit code?

I get wrong exit code from waitpid and I can't figure out why. Could someone give me some ideas?

Here what I do:

  1. I start my child process with open2
  2. then I wait for it to finish with waitpid
  3. get exit code using $?

It always returns with -1 no mater what I return from child process. I check with VS debugger that my program returns an exit code of 0. VS says something like this:

The program '[3256] Test.exe: Native' has exited with code 0 (0x0).

I made sure that pids match.

Any ideas?

like image 790
Paulius Liekis Avatar asked Nov 20 '25 09:11

Paulius Liekis


1 Answers

I just figured it out. waitpid has 3 stages:

 1. process is running:    waitpid returns 0;   $? is -1
 2. process is exiting:    waitpid returns pid; $? is actual exit code
 3. process doesn't exist: waitpid returns -1;  $? is -1

so when doing something like while(waitpid($pid, WNOHANG) >= 0) exit code must be retrieved once cycle before that.

like image 86
Paulius Liekis Avatar answered Nov 22 '25 00:11

Paulius Liekis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!