The code before isnt working for me cause the children are correctly started but not refreshed after they died. So keep in mind to use this instead and use the signal handler to know when a child exits to know when you have to start a new one. I added a few lines to the posting from {andy at cbeyond dot net} cause his post wasnt working for me as well (PHP5.1). Same effect like the one below.
<?php
declare(ticks = 1);
$max=5;
$child=0;
function sig_handler($signo) {
global $child;
switch ($signo) {
case SIGCHLD:
echo "SIGCHLD received\n";
$child--;
}
}
pcntl_signal(SIGCHLD, "sig_handler");
while (1){
$child++;
$pid=pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
if ( $child >= $max ){
pcntl_wait($status);
$child++;
}
} else {
echo "\t Starting new child | now we de have $child child processes\n";
sleep(rand(3,5));
exit;
}
}
?>