Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Process Control Functions : pcntl_wait

pcntl_wait

Waits on or returns the status of a forked child (PHP 5)
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.

Note:

This function is equivalent to calling pcntl_waitpid() with a -1 pid and no options.

Parameters

status

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().

options

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:

Table 240. Possible values for options

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


Return Values

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.

Code Examples / Notes » pcntl_wait

federico

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;
}
}
?>


thomas dot nicolai

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;
      }
}
?>


thisisroot

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";

}
?>


Change Language


Follow Navioo On Twitter
pcntl_alarm
pcntl_exec
pcntl_fork
pcntl_getpriority
pcntl_setpriority
pcntl_signal
pcntl_wait
pcntl_waitpid
pcntl_wexitstatus
pcntl_wifexited
pcntl_wifsignaled
pcntl_wifstopped
pcntl_wstopsig
pcntl_wtermsig
eXTReMe Tracker