PHP
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, 05 Sep 2008

view this page in

pcntl_wait

(PHP 5)

pcntl_waitAttend ou retourne le statut d'un processus fils

Description

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

pcntl_wait() suspend l'exécution du processus courant jusqu'à ce qu'un des processus fils soit terminé, ou qu'un signal soit envoyé pour terminer le processus courant ou pour appeler un gestionnaire. Si le processus est déjà terminé au moment de l'appel de la fonction, c'est-à-dire si le processus est un zombie, alors la fonction se termine immédiatement. Toutes les ressources système utilisées par le processus fils sont libérées. Lisez le manuel de votre système à wait(2) pour avoir des détails spécifiques sur le fonctionnement de wait() sur celui-ci.

Note: Cette fonction est équivalente à appeler la fonction pcntl_waitpid() avec un pid valant -1 et aucune option.

Liste de paramètres

status

pcntl_wait() va stocker les informations de statut dans le paramètre status qui peut être lu avec les fonctions suivantes : pcntl_wifexited(), pcntl_wifstopped(), pcntl_wifsignaled(), pcntl_wexitstatus(), pcntl_wtermsig() et pcntl_wstopsig().

options

Si wait3 est disponible sur votre système (c'est le cas de la plupart des systèmes BSD-), vous pouvez ajouter le paramètre optionnel options . S'il n'est pas fourni, wait() sera utilisé pour l'appel système. Si wait3 n'est pas disponible, le paramètre options n'aura pas d'effet. La valeur de options est la combinaison de zéro ou plusieurs des constantes suivantes :

Valeurs possibles pour options
WNOHANG Termine immédiatement si aucun processus ne s'est terminé.
WUNTRACED Termine pour les processus qui sont stoppés, et pour ceux dont le résultat n'a pas été rapporté.

Valeurs de retour

pcntl_wait() retourne l'identifiant de processus qui s'est terminé, -1 en cas d'erreur ou zéro si WNOHANG a été fourni comme option (disponible sur les systèmes wait3), et qu'aucun processus fils n'était disponible.



pcntl_waitpid> <pcntl_signal
Last updated: Fri, 05 Sep 2008
 
add a note 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()

<?php

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

?>

pcntl_waitpid> <pcntl_signal
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites