Jump to content


Photo

pcntl_signal - and trouble with calling object method


  • This topic is locked This topic is locked
1 reply to this topic

#1 cescabil

cescabil
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 August 2006 - 03:19 PM

Hello,

I've been having difficulties trying to use the pcntl_signal() method to install my own signal handler, so that I can control the maximum number of children processes allowed at a time.  Basically I am doing the following:

<?

declare(ticks = 1);

$max=10;
$child=0;

class myClass {

	function sigHandler($signo) {
		global $child;
		switch ($signo) {
			case SIGCHLD:
				echo "SIGCHLD received\n";
				$child -= 1;
		}
	}

	function startForking() {
                global $child;

		// install signal handler for dead children
		pcntl_signal(SIGCHLD, array(&$this, "sigHandler"));

		//START FORKING HERE
		for ( $i=0; $i<=100000; $i+=25 ){
			while ($child >= $maxChildren) {
				sleep(5); echo "\t Maximum children allowed\n";
			}

			$child++;

			$pids[$i] = pcntl_fork();

			if (!$pids[$i]) {
				// I am the child, do something
				exit;
			}
		}

		// get rid of zombies
		for ( $i=0; $i<=100000; $i+=25 ){
			pcntl_waitpid($pids[$i], $status, WUNTRACED);
		}
		
		// this is now the parent, so do parent code here

	}
}

?>

My issue is not the forking, but attempting to install the signal handler, at this line of code:

pcntl_signal(SIGCHLD, array(&$this, "sigHandler"));

When I run my php script, I get a message:

PHP Warning:  pcntl_signal: Array is not a callable function name error


Has anyone been able to call an object method using pcntl_signal() successfully?

Thanks in advance for the help.
-Christina



#2 cescabil

cescabil
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 August 2006 - 10:15 PM

Hello all.

I managed to figure out the issue.  The documentation everywhere (including php.net) is wrong.  When we use the pcntl_signal(), we DON'T pass in the object and the object method name (although imho, it seems like the logical thing to do).  Instead you need to do the following:

pcntl_signal(SIGCHLD, array('myClass', 'sigHandler'));

where myClass is the class name.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users