note 102765 modified in function.pcntl-signal by danbrown

From: Date: Fri, 04 Mar 2011 16:01:46 +0000
Subject: note 102765 modified in function.pcntl-signal by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177567@lists.php.net to get a copy of this message
I was having some issues with processing a signal using an object method I use for something else as well. In particular, I wanted to handle SIGCHLD with my own method "do_reap()" which I also call after a stream_select timeout and that uses a non-blocking pcntl_waitpid function. The method was called when the signal was received but it couldn't reap the child. The only way it worked was by creating a new handler that itself called do_reap(). So in other words, the following does not work: <?php class Parent { /* ... */ private function do_reap(){ $p = pcntl_waitpid(-1,$status,WNOHANG); if($p > 0){ echo "\nReaped zombie child " . $p; } public function run(){ /* ... */ pcntl_signal(SIGCHLD,array(&$this,"do_reap")); $readable = @stream_select($read,$null,$null,5); // 5 sec timeout if($readable === 0){ // we got a timeout $this->do_reap(); } } ?> But this work: <?php class Parent { /* ... */ private function do_reap(){ $p = pcntl_waitpid(-1,$status,WNOHANG); if($p > 0){ echo "\nReaped zombie child " . $p; } public function run(){ /* ... */ pcntl_signal(SIGCHLD,array(&$this,"child_died")); $readable = @stream_select($read,$null,$null,5); // 5 sec timeout if($readable === 0){ // we got a timeout $this->do_reap(); } private function child_died(){ $this->do_reap(); } } ?> --was-- I was having some issues with processing a signal using an object method I use for something else as well. In particular, I wanted to handle SIGCHLD with my own method "do_reap()" which I also call after a stream_select timeout and that uses a non-blocking pcntl_waitpid function. The method was called when the signal was received but it couldn't reap the child. The only way it worked was by creating a new handler that itself called do_reap(). So in other words, the following does not work: class Parent { /* ... */ private function do_reap(){ $p = pcntl_waitpid(-1,$status,WNOHANG); if($p > 0){ echo "\nReaped zombie child " . $p; } public function run(){ /* ... */ pcntl_signal(SIGCHLD,array(&$this,"do_reap")); $readable = @stream_select($read,$null,$null,5); // 5 sec timeout if($readable === 0){ // we got a timeout $this->do_reap(); } } But this work: class Parent { /* ... */ private function do_reap(){ $p = pcntl_waitpid(-1,$status,WNOHANG); if($p > 0){ echo "\nReaped zombie child " . $p; } public function run(){ /* ... */ pcntl_signal(SIGCHLD,array(&$this,"child_died")); $readable = @stream_select($read,$null,$null,5); // 5 sec timeout if($readable === 0){ // we got a timeout $this->do_reap(); } private function child_died(){ $this->do_reap(); } } http://php.net/manual/en/function.pcntl-signal.php

« previous php.notes (#177567) next »