Created
October 21, 2009 07:17
-
-
Save axgle/214946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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