Pcntl_alarm in PHP set time-out for function

Source: Internet
Author: User

To the official PHP view this function description

Pcntl_alarm (PHP 4 >= 4.3.0, PHP 5)

pcntl_alarm-set a alarm alarm signal for the process

Description ¶int pcntl_alarm (int $seconds)

Creates a timer that sends a SIGALRM signal to the process after the specified number of seconds. Each call to Pcntl_alarm () cancels the previously set alarm signal.

Parameter ¶seconds the number of seconds to wait. If the seconds is set to 0, the alarm signal will not be created.

The return value ¶ Returns the number of seconds remaining in the last alarm dispatch (from the alarm signal), or 0 when there is no alarm schedule (or previous dispatch completed).


Instance

The code is as follows Copy Code

<?php
DECLARE (ticks = 1);
function A ()
{
Sleep (10);
echo "a Finishin";
}
Function B ()
{
echo "STOPN";
}
Function C ()
{
Usleep (100000);
}

function sig ()
{
throw new Exception;
}

Try
{
Set an alarm signal to be executed once a second
Pcntl_alarm (1);
Install alarm signal, and bind callback
Pcntl_signal (SIGALRM, "sig");
A ();
Cancel the alarm signal.
Pcntl_alarm (0);
}
catch (Exception $e)
{
echo "Timeoutn";
}

b ();
A ();
b ();


attached, PCNTL extended installation

PCNTL extensions can support multithreaded operations in PHP.

The original need to recompile PHP back configrue hint plus--enable-pcntl

In order to save trouble directly compiled bird.

The code is as follows Copy Code
# Cd/usr/local/src/php-5.2.6/ext/pcntl
# phpize
#./configure--with-php-config=/usr/local/php/bin/php-config
# Make && make install

pcntl.so Add to PHP.ini OK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.