Home  >  Article  >  Backend Development  >  Share a small example of signal processing in php

Share a small example of signal processing in php

藏色散人
藏色散人forward
2021-11-26 15:32:264224browse

Our commonly used signals

  • kill sigterm sigkill【kill command】

  • ctrl c sigint [issued by keyboard]

  • reload sinhub [generally issued from terminal]

  • ctrl z sigstop [issued by keyboard]

  • Timer sigalarm [A process can only have one timing time, more will be overwritten by new values]

sigkill and sinstop are performing signal processing When, it cannot be ignored (signal processing can be ignored, and user-specified processing is executed by default)

php signal small example

<?php
        function sighandler($signo){
                echo 'just for sigint',"\n";

        }

        function sighandler2($signo){

                echo 'just for sigquit',"\n";
        }
        declare(ticks=1);

        pcntl_signal(SIGINT,"sighandler");

        pcntl_signal(SIGQUIT,"sighandler2");
        for($i=1;$i<30;$i++){
                file_put_contents('/home/tbtest/out.txt',"$i"."秒\n");
                sleep(1);
        }

~

Execution result

root@lyh:/home/tbtest# php sigint.php 
^Cjust for sigint
^Cjust for sigint
^Cjust for sigint
just for sigquit
^Cjust for sigint
^Cjust for sigint
^Z
[1]+  Stopped                 php sigint.php
root@lyh:/home/tbtest# bg
[1]+ php sigint.php &
root@lyh:/home/tbtest# fg
php sigint.php
root@lyh:/home/tbtest# cat out.txt 
29秒
root@lyh:/home/tbtest#

About capturing sigquit

I captured jsut for sigquit above because I set up another terminal,

root@lyh:~# ps -aux |grep php                                    
root     16385  0.5  1.9 377720 19468 pts/2    S+   15:09   0:00 php sigint.php
root     16390  0.0  0.0  11744   932 pts/0    S+   15:09   0:00 grep --color=auto php
root@lyh:~# kill -s sigquit 16385

ps: pcntl_signal_dispatch is more efficient than ticks

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Share a small example of signal processing in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete