(no commit message)
[paste/151.git] / forktest.php
1 <?php
2 $pid = pcntl_fork();
3 if ($pid == -1) {
4     die('could not fork');
5 } else if ($pid) {
6     echo "forked\n";
7     // we are the parent
8     $start = time();
9     $exited = false;
10     do {
11         sleep(1);
12         echo "check child status pid $pid\n";
13         $ret = pcntl_waitpid($pid, $status, WNOHANG | WUNTRACED);
14         echo "status:\n";
15         if ($ret > 0) {
16             $exitcode = pcntl_wexitstatus($status);
17             echo " just exited with status $exitcode\n";
18             $exited = true;
19             break;
20         } else if ($ret == 0) {
21             echo " not exited\n";
22         } else {
23             //error
24             echo " error\n";
25             break;
26         }
27     } while (time() < $start + 3);
28     echo "child wait done\n";
29     if (!$exited) {
30         echo "killing\n";
31         posix_kill($pid, 9);
32     }
33 } else {
34     // we are the child
35     echo "hi this is the child\n";
36     sleep(5);
37     echo "foo\n";
38     exit(2);
39 }
40
41 ?>