downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pcntl_exec> <PCNTL 函数
[edit] Last updated: Fri, 25 May 2012

view this page in

pcntl_alarm

(PHP 4 >= 4.3.0, PHP 5)

pcntl_alarm为进程设置一个alarm闹钟信号

说明

int pcntl_alarm ( int $seconds )

创建一个计时器,在指定的秒数后向进程发送一个SIGALRM信号。每次对 pcntl_alarm()的调用都会取消之前设置的alarm信号。

参数

seconds

等待的秒数。如果seconds设置为0,将不会创建alarm信号。

返回值

返回上次alarm调度(离alarm信号发送)剩余的秒数,或者之前没有alarm调度(译注:或者之前调度已完成) 时返回0



add a note add a note User Contributed Notes pcntl_alarm
thessoro at gmail dot com 20-Apr-2011 03:05
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 19-Oct-2005 10: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(;;) {
    }

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites