(no commit message) master
authorChristian Weiske <cweiske@cweiske.de>
Thu, 6 Nov 2014 20:16:51 +0000 (21:16 +0100)
committerwww-cweiske <www-cweiske@localhost.localdomain>
Thu, 6 Nov 2014 20:16:51 +0000 (21:16 +0100)
forktest.php [new file with mode: 0644]

diff --git a/forktest.php b/forktest.php
new file mode 100644 (file)
index 0000000..f873c4d
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+$pid = pcntl_fork();
+if ($pid == -1) {
+    die('could not fork');
+} else if ($pid) {
+    echo "forked\n";
+    // we are the parent
+    $start = time();
+    $exited = false;
+    do {
+        sleep(1);
+        echo "check child status pid $pid\n";
+        $ret = pcntl_waitpid($pid, $status, WNOHANG | WUNTRACED);
+        echo "status:\n";
+        if ($ret > 0) {
+            $exitcode = pcntl_wexitstatus($status);
+            echo " just exited with status $exitcode\n";
+            $exited = true;
+            break;
+        } else if ($ret == 0) {
+            echo " not exited\n";
+        } else {
+            //error
+            echo " error\n";
+            break;
+        }
+    } while (time() < $start + 3);
+    echo "child wait done\n";
+    if (!$exited) {
+        echo "killing\n";
+        posix_kill($pid, 9);
+    }
+} else {
+    // we are the child
+    echo "hi this is the child\n";
+    sleep(5);
+    echo "foo\n";
+    exit(2);
+}
+
+?>