note 59441 deleted from function.pcntl-fork by nlopess
| From: | nlopess@php.net | Date: | Sat, 22 Jul 2006 11:40:09 +0000 |
| Subject: | note 59441 deleted from function.pcntl-fork by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115044@lists.php.net to get a copy of this message | ||
Note Submitter: erm at the-erm dot com
----
This creates children, and grand children processes.
<?php
declare(ticks=1);
echo "I'm going to be a Dad.\n";
if (spawn_child('child_function')) {
echo 'Parent pid:'.posix_getpid()."\n";
}
echo "I'm going to be a Dad again!.\n";
if (spawn_child('child_function',1,2,3,4)) {
echo 'Child - 2 Parent pid:'.posix_getpid()."\n";
}
echo "What you're pregnant again!?.\n";
if (spawn_child('grand_children')) {
echo 'Child - 3 Parent pid:'.posix_getpid()."\n";
}
function grand_children() {
echo "Dad I'm going to have a baby.\n";
spawn_child('child_function','Joe');
echo "I'm so proud of my kids.\n";
}
function spawn_child($function) {
$original_pid = posix_getpid();
$pid = pcntl_fork();
if ($pid == -1) {
die ("Unable to spawn child\n");
}
$new_pid = posix_getpid();
if ($new_pid == $original_pid) {
return true;
}
if (function_exists($function)) {
$numargs = func_num_args();
if ($numargs > 1) {
$args = func_get_args();
//print_r ($args);
unset($args[0]);
call_user_func($function,$args);
} else {
call_user_func($function);
}
echo "Done with child ".$new_pid." they moved out.\n";
exit;
} else {
die ("$function does not exist\n");
}
}
function child_function() {
echo 'Child pid:'.posix_getpid()."\n";
$args = func_get_args();
if (!empty($args))
print_r ($args);
}
pcntl_wait( $status);
?>