phplinuxsetitimer

why php using ITIMER_PROF but not ITIMER_REAL in setitimer?


PHP under Linux implements ini(max_execution_time) and set_time_limit by using ITIMER_PROF in setitimer, but not ITIMER_REAL. by google it, i found a thread in php.net about this question https://bugs.php.net/bug.php?id=65596. Related to PHP Manual http://php.net/manual/en/function.set-time-limit.php note

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real. PHP seems designed to use ITIMER_PROF.

I want to know why PHP timeout mechanism designed that way, and if i change ITIMER_PROF to ITIMER_REAL in Zend/zend_execute_API.c , what side-effect would show ?


Solution

  • They do it for portability.

    ITIMER_REAL breaks on some SAPI's and unices due to ITIMER_REAL sending a SIGALRM signal instead of a SIGVALRM or SIGPROF signal.

    Side effects I believe would be premature script termination and incompatibility with some SAPI's such as Apache + mod_php.

    Some unices also use SIGALRM on sleep(2) which php's sleep() function uses so that would be another side effect.