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);
}
}
?>
pcntl_alarm
(PHP 4 >= 4.3.0, PHP 5)
pcntl_alarm — シグナルを送信するアラームを設定する
説明
int pcntl_alarm
( int
$seconds
)プロセスに対して、 指定した秒数後に SIGALRM シグナルを送信するタイマーを作成します。 pcntl_alarm() をコールすると、 それまでに設定されていたアラームはすべて取り消されます。
パラメータ
-
seconds -
待機する秒数。
secondsがゼロの場合は、 新しいアラームは作成されません。
返り値
それまでに予定されていたアラームの予定時刻までの秒数を返します。 事前に予定されていたアラームがなかった場合には 0 を返します。
thessoro at gmail dot com
20-Apr-2011 09:05
j at ukr-info dot net
20-Oct-2005 04:51
<?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(;;) {
}
?>
