pcntl_wait not interrupted by SIGTERM

限于喜欢 提交于 2019-12-10 17:26:24

问题


According to the PHP docs for pcntl_wait,

The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function.

However, when I run the following code and send SIGTERM to the parent process with kill -s SIGTERM [pid] the signal handler is only called after the child exits (i.e. I have to wait for the sleep to finish. Shouldn't pcntl_wait() be interrupted by SIGTERM?

fork_test.php:

<?php
  declare(ticks = 1);

  function sig_handler($signo) {
    switch ($signo) {
      case SIGTERM:
        echo 'SIGTERM' . PHP_EOL;
        break;
      default:
    }
  }

  pcntl_signal(SIGTERM, 'sig_handler');

  $pid = pcntl_fork();

  if ($pid == -1) {
     die('could not fork');
  }
  else if ($pid) {
    echo 'parent' . PHP_EOL;

    pcntl_wait($status);
  }
  else {
    echo 'child' . PHP_EOL;
    sleep(30);
  }
?>

Output (SIGTERM only appears after waiting 30 seconds):

$ php fork_test.php
child
parent
SIGTERM

PHP Version => 5.3.3


回答1:


Your call to pcntl_signal specifies that the call should be restarted. Check the docs, restart_syscalls is true by default. So your call to pcntl_signal doesn't return until the child has terminated.

You have no call to flush. So PHP can hold the output of echo in a buffer.

So the behavior you are seeing is precisely the behavior you are requesting. The system call is restarted and the output is buffered.



来源:https://stackoverflow.com/questions/10290366/pcntl-wait-not-interrupted-by-sigterm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!