<?php
declare(ticks = 1);
function signal_handler($signal) {
print "Caught SIGALRM\n";
pcntl_alarm(5);
}
pcntl_signal(SIGALRM, "signal_handler", true);
pcntl_alarm(5);
for(;;) {
}
?>
pcntl_alarm
(PHP 4 >= 4.3.0, PHP 5)
pcntl_alarm — Set an alarm clock for delivery of a signal
Description
int pcntl_alarm
( int
$seconds
)
Creates a timer that will send a SIGALRM signal to
the process after the given number of seconds.
Any call to pcntl_alarm() will cancel any previously
set alarm.
Parameters
-
seconds -
The number of seconds to wait. If
secondsis zero, no new alarm is created.
Return Values
Returns the time in seconds that any previously scheduled alarm had remaining before it was to be delivered, or 0 if there was no previously scheduled alarm.
j at ukr-info dot net ¶
7 years ago
thessoro at gmail dot com ¶
2 years ago
If your process uses SIGALRM and sleep() at the same time, the alarm set could make sleep() to return prematurely.
To avoid this and ensure your process waits a number of seconds you could use a function or class similar to this one:
<?php
class SleepWorkaroundForSIGALRM {
private $time;
function __construct($seconds) {
$this->time = time() + $seconds;
while ($this->time >= time()) {
sleep(1);
}
}
?>
