PHP进程间通信-信号

  • 2019 年 11 月 10 日
  • 筆記

信号

信号是一种系统调用。通常我们用的kill命令就是发送某个信号给某个进程的。具体有哪些信号可以在liunx/mac中运行 kill -l 查看。下面这个例子中,父进程等待5秒钟,向子进程发送sigint信号。子进程捕获信号,调信号处理函数处理。

代码演示

<?php  $childList = [];  $parentId = posix_getpid();    //信号处理函数  function signHandler($sign){      $pid = posix_getpid();      exit("process:{$pid},is killed,signal is {$sign}n");  }    $pid = pcntl_fork();  if ($pid == -1){      // 创建子进程失败      exit("fork fail,exit!n");  }elseif ($pid == 0){      //子进程执行程序      //注册信号处理函数      declare(ticks = 10);      pcntl_signal(SIGINT,"signHandler");//注册SIGINT信号处理函数      $pid = posix_getpid();      while (true){          echo "child process {$pid},is running.......n";          sleep(1);      }  }else{      $childList[$pid] = 1;      sleep(5);      posix_kill($pid,SIGINT);//向指定进程发送一个信号  }    // 等待子进程结束  while(!empty($childList)){      $pid = pcntl_wait($status);      if ($pid > 0){          unset($childList[$pid]);      }  }    echo "The child process is killed by parent process {$parentId}n";

运行结果

当父进程没有发送信号的时候,子进程会一直循环输出‘child process is running…’,父进程发送信号后,子进程在检查到有信号进来的时候调用对应的回调函数处理退出了子进程。

declare(ticks = 10)

这里的ticks=10,可以理解为程序执行10条低级语句后,检查看有没有未执行的信号,有的话就去处理。

关于 declare(ticks = n) 的详细讲解可以参考 (https://my.oschina.net/Jacker/blog/32936)