What is the relationship between declaration (ticks) and signal handler in php

I have code like this, and I want to understand how fork works, but I'm confused with declare(ticks=1) . when I put it on the first line, after the completion of the child process, the signal handler will be called, and this is what I want; but when I delete it, the signal handler will never be called! So, I want to know how ticks affect signal processing.

 <?php declare(ticks=1); function sigHandler($signal) { echo "a child exited\n"; } pcntl_signal(SIGCHLD, sigHandler, false); echo "this is " . posix_getpid() . PHP_EOL; for($i=0; $i<3; $i++) { $pid = pcntl_fork(); if($pid == -1) { echo 'fork failed ' . PHP_EOL; } else if($pid) { } else { $pid = posix_getpid(); echo 'child ' . $pid . ' ' . time() . PHP_EOL; sleep(rand(2,5)); echo 'child ' . $pid . ' done ' . time() . PHP_EOL; exit(0); } } do { $pid = pcntl_wait($status); echo 'child quit ' . $pid . PHP_EOL; }while($pid > 0); echo 'parent done' . PHP_EOL; ?> 
+7
php pcntl
source share
1 answer

Minor observation (provide the name of the PLS function.):

 pcntl_signal(SIGCHLD, 'sigHandler', false); 

There are two different APIs.

  • The pcntl_wait () call is blocked until it receives a notification from the kernel.
  • Interrupt handling is an event loop inside the PHP interpreter. This is a hacker function, and with PHP5.3 there is a better way to do this ~ pcntl_signal_dispatch ().
    • To answer the question, declaring tics is to enable the inclusion of a mobile phone, otherwise you will never notice incoming calls.
    • The PHP5.3 method is much better developed and has more manageability.
    • The default signal handler for most signals is a signal that receives an interrupt and does nothing. Since you registered the user exit, I doubt that this is being used.
    • I could never find out the default value for ticks unless it is set directly.
    • Setting ticks to a small value makes abit scripts run slower, but you need to do heavy processing and monitoring to notice this. This makes a less predictable cost of execution, I think, due to copying things on the stack.
    • Without ticks or pcntl_signal_dispatch (), the signal never rises. If you write simple web pages that end quickly; this may be the most sensible policy.
    • Ticks must be checked carefully, as this confuses the rules of the area. The safest way to put it at the head of your first script, for example, use strictly in Perl.
+9
source share

All Articles