2

Well the child process may exit with error but pcntl_wifexited always return true

<?php

//Sample code :
$child_pid = pcntl_fork();

if ($child_pid === 0) 
{
    //This is child process

    $my_var = rand(1,5);

    if($my_var == 2)
    {
        //2 is not allowed
        exit(1); //exit with error
    }

    if($my_var == 4)
    {
        //4 is unknown
        i_crash_now(); //crash
    }

    echo 'end of child' . "\n";
    exit(0); //exit standard

}
else
{
    sleep(1); //waiting for child with ninja code (don't do that at home, this was made by professional ninjas)
    pcntl_waitpid($child_pid, $status, WNOHANG);

    var_dump(pcntl_wstopsig($status));  //in case 2 > 1, in case 4 > 255, else 0
    var_dump(pcntl_wifexited($status)); //always true;
}

exit(0);

I may use the signal to find errors, but I don't get what's wrong with pcntl_wifexited().

Is this related to WNOHANG option?

hakre
  • 193,403
  • 52
  • 435
  • 836
DEY
  • 1,770
  • 2
  • 16
  • 14

1 Answers1

0

I imagine the pcntl_waitpid behaves similarly to the normal waitpid call.

WNOHANG forces waitpid to return 0 if no process is terminated [it's akin to a zero timeout].

Hence, exit code will appear to be normal.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • I suppose this is right. So with `WNOHANG` I should use `pcntl_wstopsig($status)`. – DEY Feb 18 '11 at 08:29