1

I'm considering using pcntl_alarm() for something, so to test it out I ran a variation of the example you find everywhere when you google pcntl_alarm:

function signal_handler()
{
    echo "Caught SIGALRM\n";
    exit;
}
pcntl_signal('SIGALRM', 'signal_handler');

pcntl_alarm(3);

for($i = 1; $i <= 6; $i++) {
    echo "$i ";
    sleep(1);
}
echo "Normal exit\n";

Expected output:

1 2 3 Caught SIGALRM

Actual output:

1 2 3 Alarm Clock

I tried not quoting SIGALRM in the pcntl_signal() call (since it expects an int), but then the alarm doesn't seem to fire:

1 2 3 4 5 6 Normal exit

I'm assuming the Alarm Clock output is due to a default handler and my signal_handler() is not getting called. Is SIGALRM the correct constant? What am I missing? I'm running via CLI under linux (centos), have tried both php 5.5 and 5.6.

Mark H.
  • 549
  • 6
  • 16

1 Answers1

2

SIGALRM is a constant as you can see by doing:

echo SIGALRM; //14

Also you need to check for pending signals using pcntl_signal_dispatch inside your loop.

function signal_handler()
{
    echo "Caught SIGALRM\n";
    exit;
}
pcntl_signal(SIGALRM, 'signal_handler');

pcntl_alarm(3);

for($i = 1; $i <= 6; $i++) {
    echo "$i ";
    sleep(1);
    pcntl_signal_dispatch();
}
echo "Normal exit\n";

Goodluck!

cirus600
  • 46
  • 4