note 44937 added to ref.pcntl

From: Date: Fri, 20 Aug 2004 16:26:17 +0000
Subject: note 44937 added to ref.pcntl
Groups: php.notes 
Request: Send a blank email to php-notes+get-75002@lists.php.net to get a copy of this message
Suppose you want to fork off children to handle a few hundred different targets (like, say, SNMP polling, but that's just one example). Since you don't want to fork-bomb yourself, here's one method of limiting the number of children you have in play at any one time: #!/usr/bin/php -q <?php declare(ticks = 1); $max=10; $child=0; // function for signal handler function sig_handler($signo) { global $child; switch ($signo) { case SIGCHLD: echo "SIGCHLD received\n"; $child -= 1; } } // install signal handler for dead kids pcntl_signal(SIGCHLD, "sig_handler"); for ($i=1;$i<=20;$i++) { while ($child >= $max) { sleep(5); echo "\t Maximum children allowed\n"; } $child++; $pid=pcntl_fork(); if ($pid == -1) { die("could not fork"); } else if ($pid) { // we are the parent } else { // we are the child echo "child number $i\n"; // presumably doing something interesting sleep(15); exit; } } ---- Manual Page -- http://www.php.net/manual/en/ref.pcntl.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44937 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44937&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44937&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75002) next »