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

Share a small example of signal processing in php

藏色散人
Release: 2023-04-10 20:00:01
forward
4263 people have browsed it

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);
        }
Copy after login

~

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#
Copy after login

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
Copy after login

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template