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 — Belli bir süre sonra SIGALRM sinyali
gönderir
Açıklama
int pcntl_alarm
( int
$saniye
)
saniye ile belirtilen sürenin sonunda sürece bir
SIGALRM sinyali gönderir. Yapılan her
pcntl_alarm() çağrısı bir öncekini geçersiz kılar.
Değiştirgeler
-
saniye -
Sinyal gönderilene kadar beklenecek süre. Sıfır belirtilirse hiçbir sinyal gönderilmez.
Dönen Değerler
Devreye girmeden geçersiz hale getirilmiş önceki çağrının süresini döndürür. Böyle bir çağrı yoksa 0 döner.
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(;;) {
}
?>
