/
sandbox.php
70 lines (60 loc) · 1.65 KB
/
sandbox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
* This file is part of the Morker package.
*
* (c) Aurimas Niekis <aurimas.niekis@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
$workers = 0;
$task = function() {
$count = 5;
while ($count-- > 0) {
echo "Running " . $count . "\n";
sleep(1);
}
exit;
};
$exit = false;
while(!$exit) {
if ($workers == 0) {
$workers++;
$pid = pcntl_fork();
if (-1 === $pid) {
echo "Fork failed\n";
exit;
} elseif (0 === $pid) {
echo "Spawned child\n";
$task();
}
}
while(true) {
$pid = pcntl_waitpid(-1, $status, WNOHANG | WUNTRACED);
if ($pid === 0) {
echo "No children exited\n";
break;
} elseif ($pid === -1) {
echo "Failed something\n";
echo "Status '".$status."'\n";
if (pcntl_wifexited($status)) {
echo "pcntl_wifexited\n";
} elseif (pcntl_wifstopped($status)) {
echo "pcntl_wifstopped\n";
} elseif (pcntl_wifsignaled($status)) {
echo "pcntl_wifsignaled\n";
} elseif ( pcntl_wexitstatus($status)) {
echo " pcntl_wexitstatus\n";
} elseif ( pcntl_wtermsig($status)) {
echo " pcntl_wtermsig\n";
} elseif ( pcntl_wstopsig($status)) {
echo " pcntl_wstopsig\n";
}
exit;
} else {
echo "Child #" . $pid . " exited\n";
$exit = true;
}
}
sleep(1);
}