"Signals are a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. A signal is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred. Signals have been around since the 1970s Bell Labs Unix and have been more recently specified in the POSIX standard."
"When a signal is sent, the operating system interrupts the target process's normal flow of execution to deliver the signal. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise, the default signal handler is executed."
can be described as events that can happen between the normal execution of the code
"A tick is an event that occurs for every N low-level tickable statements executed by the parser within the declare block. The value for N is specified using ticks=N within the declare block's directive section.
"Not all statements are tickable. Typically, condition expressions and argument expressions are not tickable."
ticks set to a low value gives you much control but has a performance impact
ticks set to a high value give you less control but impact performance less
You can manually set in your code when the signals can be handled
1 <?php 2 3 declare(ticks = 1); 4 5 pcntl_signal(SIGINT, 'signalhandler'); 6 7 function signalhandler($signal) 8 { 9 echo 'Caught signal ' . $signal . PHP_EOL; 10 return; 11 } 12 13 // keep on running so we can actually send a signal ;) 14 while (true) { 15 }
1 <?php 2 3 pcntl_signal(SIGINT, 'signalhandler'); 4 5 function signalhandler($signal) 6 { 7 echo 'Caught signal ' . $signal . PHP_EOL; 8 return; 9 } 10 11 // keep on running so we can actually send a signal ;) 12 while (true) { 13 pcntl_signal_dispatch(); 14 }
We can send the SIGALRM signal from within our script
Could be a very nice trick to show progress
1 <?php 2 3 declare(ticks = 1); 4 5 pcntl_signal(SIGALRM, 'alarmhandler'); 6 pcntl_alarm(5); // SIGALRM after 5 seconds 7 8 function alarmhandler($signal) 9 { 10 echo "Alarm !" . PHP_EOL; 11 echo "Periodic progress output ?" . PHP_EOL; 12 pcntl_alarm(5); // we want it every 5 seconds so set again 13 return; 14 } 15 16 // keep on running so we can actually send a signal ;) 17 while (true) { 18 }
@BlackIkeEagle
Senior Webdeveloper - Studio Emma
Archlinux Trusted User