PHP: pcntl_wait - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pcntl_waitpid pcntl_signal

Last updated: Fri, 26 Jan 2007

view this page in

# pcntl_wait

(PHP 5)

pcntl_wait -- Waits on or returns the status of a forked child

## Description

int pcntl_wait ( int &status [, int options] )

The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child has already exited by the time of the call (a so-called "zombie" process), the function returns immediately. Any system resources used by the child are freed. Please see your system's wait(2) man page for specific details as to how wait works on your system.

pcntl_wait() returns the process ID of the child which exited, -1 on error or zero if WNOHANG was provided as an option (on wait3-available systems) and no child was available.

If wait3 is available on your system (mostly BSD-style systems), you can provide the optional `options` parameter. If this parameter is not provided, wait will be used for the system call. If wait3 is not available, providing a value for `options` will have no effect. The value of `options` is the value of zero or more of the following two constants `OR`'ed together:

Таблица 1. Possible values for `options` if wait3 is available

`WNOHANG` | Return immediately if no child has exited.
`WUNTRACED` | Return for children which are stopped, and whose status has not been reported.

pcntl_wait() will store status information in the `status` parameter which can be evaluated using the following functions: pcntl_wifexited(), pcntl_wifstopped(), pcntl_wifsignaled(), pcntl_wexitstatus(), pcntl_wtermsig() and pcntl_wstopsig().

> Замечание: This function is equivalent to calling pcntl_waitpid() with a `-1` `pid` and no `options`.

See also pcntl_fork(), pcntl_signal(), pcntl_wifexited(), pcntl_wifstopped(), pcntl_wifsignaled(), pcntl_wexitstatus(), pcntl_wtermsig(), pcntl_wstopsig() and pcntl_waitpid().

add a note User Contributed Notes
pcntl_wait
thomas dot nicolai at unisg dot ch
24-Mar-2006 11:19
`The code before isnt working for me cause the children are correctly started but not refreshed after they died. So keep in mind to use this instead and use the signal handler to know when a child exits to know when you have to start a new one. I added a few lines to the posting from {andy at cbeyond dot net} cause his post wasnt working for me as well (PHP5.1). Same effect like the one below.
<?php
declare(ticks = 1);
$max=5;
$child=0;
// function for signal handler
function sig_handler($signo) {
global $child;
switch ($signo) {
case SIGCHLD:
echo "SIGCHLD received\n";
$child--;
}
}
// install signal handler for dead kids
pcntl_signal(SIGCHLD, "sig_handler");
while (1){
$child++;
$pid=pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
// we are the parent
if ( $child >= $max ){
pcntl_wait($status);
$child++;
}
} else {
// we are the child
echo "\t Starting new child | now we de have $child child processes\n";
// presumably doing something interesting
sleep(rand(3,5));
exit;
}
}
?>`
federico at nextware dot it
19-Feb-2006 04:39
`This a simple multi process application where you can choose
the maximun process that can run at the same time.
This is useful when you need to limit the fork of process.
When the MAXPROCESS is reached the program wait on pcntl_wait()
<?
DEFINE(MAXPROCESS,25);
for ($i=0;$i<100;$i++){
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} elseif ($pid) {
echo "I'm the Parent $i\n";
$execute++;
if ($execute>=MAXPROCESS){
pcntl_wait($status);
$execute--;
}
} else {
echo "I am the child, $i pid = $pid \n";
sleep(rand(1,3));
echo "Bye Bye from $i\n";
exit;
}
}
?>`
thisisroot at gmail dot com
19-Oct-2005 07:14
`Below is a simple example of forking some children and timing the total duration (useful for stress tests).
<?php
$isParent = true;
$children = array();
$start = microtime( true);
/* Fork you!
* (Sorry, I had to)
*/
$ceiling = $CONCURRENCY - 1;
for ( $i = 0; (( $i < $ceiling) && ( $isParent)); $i++) {
$pid = pcntl_fork();
if ( $pid === 0) {
$isParent = false;
} elseif ( $pid != -1) {
$children[] = $pid;
}
}
/* Process body */
echo "Do stuff here\n";
/* Cleanup */
if ( $isParent) {
$status = null;
while ( count( $children)) {
pcntl_wait( $status);
array_pop( $children);
}
echo "Completed in " . ( microtime( true) - $start) . " seconds.\n";
}
?>`
add a note
pcntl_waitpid pcntl_signal

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST