(PHP 4 >= 4.3.0, PHP 5)
pcntl_alarm — 为进程设置一个alarm闹钟信号
$seconds
)
创建一个计时器,在指定的秒数后向进程发送一个SIGALRM
信号。每次对
pcntl_alarm()的调用都会取消之前设置的alarm信号。
seconds
等待的秒数。如果seconds
设置为0,将不会创建alarm信号。
返回上次alarm调度(离alarm信号发送)剩余的秒数,或者之前没有alarm调度(译注:或者之前调度已完成) 时返回0。
thessoro at gmail dot com (2011-04-20 09:05:40)
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);
}
}
?>
j at ukr-info dot net (2005-10-20 04:51:06)
<?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(;;) {
}
?>