Detailed explanation of signal processing operation examples in PHP

Time:2021-5-8

This paper introduces the signal processing operation in PHP. The details are as follows:

First we need to know a few functions

pcntl_ signal    Install the signal processor, that is, when the specified signal occurs, call the function.
pcntl_ alarm    Send sigalrm signal to the process after the specified number of seconds.
posix_ Getpid returns the current process ID
posix_ kill        Sends a signal to the specified process

Some common signal descriptions

SIGHUP
This signal is sent at the end of the user terminal connection (normal or abnormal), usually at the end of the terminal control process,
Inform each job in the same session that they are no longer associated with the control terminal.

SIGINT
The interrupt signal is sent when the user types the intr character (usually ctrl-c).

SIGQUIT
Similar to SIGINT, but controlled by the quit character (usually Ctrl – /); When the process exits due to sigquit, it will generate a core file,
In this sense, it is similar to a program error signal.

SIGKILL
Used to end a program immediately. This signal cannot be blocked, processed or ignored. If the administrator finds that a process cannot be terminated, he can try to send this signal.

SIGTERM
Different from sigkill, it can be blocked and processed. It is usually used to ask the program to exit normally,
The shell command kill generates this signal by default. If the process cannot be terminated, we will try sigkill.

SIGUSR1
Leave it to the user

SIGUSR2
Leave it to the user

SIGALRM
The clock timing signal calculates the actual time or clock time. The alarm function uses the signal.

SIGCHLD
When the child process ends, the parent process receives this signal.

Example 1:

<?php
declare(ticks = 1);
//Signal processing function
function sig_func() {
  echo "child exit \r\n";
}
//Set up signal processor
pcntl_signal(SIGCHLD, 'sig_func');
$pid = pcntl_fork();
if($pid == -1) {
  die('fork error');
} else if ($pid) {
  pcntl_wait($status);
} else {
  echo "child... \r\n";
  exit;
}

When the child process exits, it will send sigchld signal to the parent process. We set the signal processor to process the signal successfully.

Example 2

<?php
declare(ticks = 1);
//Signal processing function
function sig_func($signo) {
  switch($signo) {
    case SIGCHLD: {
      echo "child SIGCHLD \r\n";
      break;
    }
    case SIGTERM: {
      echo "child SIGTERM \r\n";
      break;
    }
    default:
      //Process all other signals
      break;
  }
}
//Set up signal processor
pcntl_signal(SIGCHLD, 'sig_func');
//Set up signal processor
pcntl_signal(SIGTERM, 'sig_func');
$pid = pcntl_fork();
if($pid == -1) {
  die('fork error');
} else if ($pid) {
  pcntl_wait($status);
} else {
  sleep(3);
  echo "child \r\n";
  sleep(3);
  posix_kill(getmypid(), SIGTERM);
  exit;
}

The parent process waits for the child process to exit, the child process outputs the child after waiting for 3 seconds, and then sends the end signal to itself after waiting for 3 seconds.

Example 3

<?php
declare(ticks = 1);
//Signal processing function
function sig_func($signo) {
  switch($signo) {
    case SIGCHLD: {
      echo "child SIGCHLD \r\n";
      break;
    }
    /*Here we need to comment out the code for processing sigtrem signal
    case SIGTERM: {
      echo "child SIGTERM \r\n";
      break;
    }*/
    default:
      //Process all other signals
      break;
  }
}
//Set up signal processor
pcntl_signal(SIGCHLD, 'sig_func');
//Set up signal processor,也注释掉
//Otherwise, when the parent process sends SIGTERM signal to the child process, the child process will not exit and will continue to execute
//Our signal processing function ignores SIGTERM
//pcntl_signal(SIGTERM, 'sig_func');
$pid = pcntl_fork();
if($pid == -1) {
  die('fork error');
} else if ($pid) {
  sleep(30);
  posix_kill($pid, SIGTERM);
} else {
  $cnt = 0;
  for(;;) {
    sleep(3);
    echo $cnt, '-';
    ++$cnt;
  }
  exit;
}

After waiting for 30 seconds, the parent process sends SIGTERM end signal to the child process. If we set the processor of sigtrem signal and do not kill the process in the custom signal processor, the child process will run continuously.

pcntl_ The signal() function only registers the signal and its processing method. It is pcntl that receives the signal and calls its processing method_ signal_ The dispatch() function.

Example 4

<?php
//Using ticks requires PHP 4.3.0 or above
//declare(ticks = 1);
function sig_func() {
  echo "SIGALRM \r\n";
}
//Set up signal processor
pcntl_signal(SIGALRM, 'sig_func');
pcntl_alarm(3);

Through the function pcntl_ Alarm() sends sigalrm signal to the process after 3 seconds, but the signal processing function is not called.
The reason is that we annotate declare (ticks = 1); This code does not call pcntl_ signal_ The dispatch() function.

declare(ticks = 1); It means that every low-level instruction is executed, the signal is checked once. If the registered signal is detected, the signal processor is called. However, the efficiency of this method is very low. It is recommended to use pcntl in the code loop_ signal_ Dispatch () to process the signal.

<?php
//Using ticks requires PHP 4.3.0 or above
//declare(ticks = 1);
function sig_func() {
  echo "SIGALRM \r\n";
}
//Set up signal processor
pcntl_signal(SIGALRM, 'sig_func');
pcntl_alarm(3);
//Because three seconds later, pcntl_ Alarm function will send sigalrm signal to the process
//So we wait 3 seconds through the sleep function and call pcntl_ signal_ Dispatch() to process the signal
sleep(3);
pcntl_signal_dispatch();

pcntl_ signal_ This function is only supported by PHP 5.3 or above. If your PHP version is greater than 5.3, it is recommended to use this method to call the signal processor.
For the version below 5.3, you need to add: declare (ticks = 1) before the registration signal;

For more information about PHP, readers interested in this site can see the following topics: summary of PHP process and thread operation skills, summary of PHP network programming skills, introductory course of PHP basic syntax, complete collection of PHP array operation skills, summary of PHP string usage “PHP + MySQL database operation tutorial” and “PHP common database operation skills summary”

I hope this article is helpful for PHP programming.