Jump to content

pcntl_signal - and trouble with calling object method


cescabil

Recommended Posts

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:

[code]
<?

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

}
}

?>
[/code]

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

[code]pcntl_signal(SIGCHLD, array(&$this, "sigHandler"));[/code]

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

[quote]PHP Warning:  pcntl_signal: Array is not a callable function name error[/quote]

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

Thanks in advance for the help.
-Christina

Link to comment
Share on other sites

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:

[code]
pcntl_signal(SIGCHLD, array('myClass', 'sigHandler'));
[/code]

where myClass is the class name.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.