note 59441 added to function.pcntl-fork

From: Date: Wed, 07 Dec 2005 03:52:55 +0000
Subject: note 59441 added to function.pcntl-fork
Groups: php.notes 
Request: Send a blank email to php-notes+get-99664@lists.php.net to get a copy of this message
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); ?> ---- Manual Page -- http://www.php.net/manual/en/function.pcntl-fork.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59441 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59441&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59441&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99664) next »