加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – pcntl_wait没有被SIGTERM打断

发布时间:2020-12-13 16:44:56 所属栏目:PHP教程 来源:网络整理
导读:根据 pcntl_wait的PHP文档, 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. 但是,当我
根据 pcntl_wait的PHP文档,

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.

但是,当我运行以下代码并使用kill -s SIGTERM [pid]将SIGTERM发送到父进程时,仅在子进程退出后调用信号处理程序(即我必须等待睡眠完成.不应该是pcntl_wait( )被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);
  }
?>

输出(SIGTERM仅在等待30秒后出现):

$php fork_test.php
child
parent
SIGTERM

PHP版本=> 5.3.3

解决方法

您对pcntl_signal的调用指定应重新启动调用.检查 docs,默认情况下restart_syscalls为true.因此,在孩子终止之前,您对pcntl_signal的调用不会返回.

你没有调用flush.所以PHP可以在缓冲区中保存echo的输出.

因此,您看到的行为正是您要求的行为.重新启动系统调用并缓冲输出.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读