Skip to content

Instantly share code, notes, and snippets.

@axgle
Created October 21, 2009 07:17
Show Gist options
  • Save axgle/214946 to your computer and use it in GitHub Desktop.
Save axgle/214946 to your computer and use it in GitHub Desktop.
<?php
declare(ticks = 1);
$max=5;
$GLOBALS[__FILE__]['_child']=0;
//print_r($GLOBALS);exit;
// function for signal handler
function sig_handler($signo) {
switch ($signo) {
case SIGCHLD:
echo "SIGCHLD received\n";
$GLOBALS[__FILE__]['_child']--;
}
}
// install signal handler for dead kids
pcntl_signal(SIGCHLD, "sig_handler");
for($i=0;$i<100;$i++){
$GLOBALS[__FILE__]['_child']++;
// we are the parent
$child=$GLOBALS[__FILE__]['_child'];
if ( $child > $max ){
pcntl_wait($status);
}
$pid=pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
} else {
// we are the child
$child=$GLOBALS[__FILE__]['_child'];
echo "\t Starting new child \n";
// presumably doing something interesting
sleep(rand(3,5));
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment