The signal processing php

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/mrtwenty/article/details/98728750

        A soft interrupt signal is a ipc communication, asynchronous communication is the only way. In linux, we can use all the signals inside the kill -l to view the system of support, sending a signal to a process using kill - signal type pid to the specified pid. In php, we use posxi_kill to achieve transmit signals.

several roles posix_kill transmitting a signal to the process:
        1, transmits a signal to a specified process
            posix_kill ( "process number", 9);
        2, the detection process is alive, the process returns true if present, otherwise returns to false
            $ posix_kill RES = ( " process number ", 0);
        3, transmits signals to the current process group, forced logout 9, the SIGKILL constant of course be used, a special process is 0, indicating that all the processes inside the process group.
            posix_kill (0,9);

Php use in signal processing, in php5.3 before using statement to declare a detection signal, we look demo1:

vim demo1
#!/usr/local/php/bin/php
<?php

declare(ticks = 1); 
pcntl_signal(SIGINT, function(){
	echo "信号触发\n";
});

$i=1;
while(true){
  $i++;
  echo $i,"\n";
  sleep(1);    
}

Save and exit, setting script executable and execute it

chmod +x demo1 && ./demo1

Then the program will continue output value of $ i, we use ctrl + c, it will send a signal, the output signal to the current process is triggered, since taken over the signal, so there is no exit. Use ctrl + \ Force Quit.

Let's look at the second way demo2, use pcntl_signal_dispatch function to achieve,

#!/usr/local/php/bin/php
<?php
pcntl_signal(SIGINT, function(){
    echo "信号触发\n";
});

$i=0;
while(true){
  pcntl_signal_dispatch();
  $i++;
  echo $i,"\n";
  sleep(1);    
}

As a test model, which is ctrl + c, ctrl + \, use pcntl_signal_dispatch will consume less than cpu tick.

The third way, after php7.1.0, to achieve added functions pcntl_async_signals processing a signal, Demo3

#!/usr/local/php/bin/php
<?php

pcntl_async_signals(true);

pcntl_signal(SIGINT, function(){
    echo "信号触发\n";
});


$i=0;
while(true){
  $i++;
  echo $i,"\n";
  sleep(1);    
}

As test mode is ctrl + c, ctrl + \, asynchronous signals manner. Since php7.1.0 still relatively new, in order to make compatible, so we can write demo4 :

#!/usr/local/php/bin/php
<?php

if(version_compare(PHP_VERSION, '7.1.0')>=0){
    pcntl_async_signals(true);
}

pcntl_signal(SIGINT, function(){
   echo "信号触发\n";
});


$i=0;
while(true){
    if(version_compare(PHP_VERSION, '7.1.0')<0){
        pcntl_signal_dispatch();
    }
    $i++;
    echo $i,"\n";
    sleep(1);    
}

Examples of the plurality of signal register 1:

#!/usr/local/php/bin/php
<?php

class Test
{
    public function handler($sig)
    {
        echo "信号值:" . $sig . "信号触发\n";
    }
    public function run()
    {
        pcntl_async_signals(true);
        pcntl_signal(SIGINT, [ & $this, "handler"]);
        pcntl_signal(SIGUSR1, [ & $this, "handler"]);
        pcntl_signal(SIGUSR2, [ & $this, "handler"]);
    }
}
$p1 = new Test();
$p1->run();

$i = 0;
while (true) {
    $i++;
    echo $i, "\n";
    sleep(1);
}

 Of course, also be a static class:

#!/usr/local/php/bin/php
<?php
class Test
{
    public static function handler($sig)
    {
        echo "信号值:" . $sig . "信号触发\n";
    }

    public static function run()
    {
        pcntl_async_signals(true);
        pcntl_signal(SIGINT, ['Test', "handler"]);
        pcntl_signal(SIGUSR1, ['Test', "handler"]);
        pcntl_signal(SIGUSR2, ['Test', "handler"]);
    }
}

Test::run();

$i = 0;
while (true) {
    $i++;
    echo $i, "\n";
    sleep(1);
}

 

Guess you like

Origin blog.csdn.net/mrtwenty/article/details/98728750