The `pcntl_alarm` function in PHP is used to set an alarm clock for the process. This function will deliver a SIGALRM signal to the process after the specified number of seconds have passed.
**Syntax:**
“`php
int pcntl_alarm ( int $seconds )
“`
**Parameters:**
– `$seconds`: This must be a non-negative integer value representing the number of seconds before the alarm signal is sent.
**Return values:**
– This function returns the time in seconds that any previously scheduled alarm had remaining before it was due to be delivered, or 0 if there was no previously scheduled alarm.
On receiving the SIGALRM signal, the script will terminate unless you’ve installed a signal handler using `pcntl_signal()`.
Please note that this function is not available on Windows platforms.
**Example:**
“`php
declare(ticks=1);
// Signal handler function
function sig_handler($signo) {
switch ($signo) {
case SIGALRM:
echo “Caught SIGALRM\n”;
break;
}
}
echo “Installing signal handler…\n”;
// setup signal handler
pcntl_signal(SIGALRM, “sig_handler”);
echo “Generating alarm in 2 seconds\n”;
// generate an alarm signal in 2 seconds
pcntl_alarm(2);
sleep(5);
echo “Done\n”;
“`
In this example, `pcntl_alarm(2)` means the process will receive a SIGALRM signal 2 seconds after calling this function.
暂无评论内容