php pcntl signal,php – 后续的pcntl_signal信号没有启动处理程序

博客内容涉及到一个关于进程管理的PHP脚本,其中父进程创建子进程,并在接收到SIGHUP信号时重启子进程。问题在于,第一次信号处理能成功终止子进程,但第二次则无法通过SIGINT信号杀死子进程,而SIGKILL可以。作者寻求理解为什么SIGINT在第二次不工作,以及如何避免使用SIGKILL。
摘要由CSDN通过智能技术生成

Lemme首先给出了我所拥有的代码的基本描述.我从一个主要的父进程开始(注意:为了简单起见,我没有显示所有函数.如果你需要我在任何时候进行扩展,请告诉我):

declare(ticks=1);

pcntl_signal(SIGHUP,array('forker','restartSignalHandler'));

if(forker_is_not_running()){

new Forker();

}

class Forker {

private $active_forks = array();

private $parent_pid = null;

public function __construct(){

$this->parent_pid = getmypid();

$this->create_fork();

$this->wait_for_active();

}

public function wait_for_active(){

while(!empty($this->active_forks)){

foreach($this->active_forks as $k=>$fork){

if($this->fork_no_longer_running($fork)){

unset($this->active_forks[$k]);

}

}

}

}

// Pseudo code

public function fork_no_longer_running($pid){

// return true if 'ps -elf | grep $pid' doesn't returns only the grep command

// else return false (aka the fork is still running)

}

public function create_fork(){

$pid = pcntl_fork();

if($pid == -1){

posix_kill($this->parent_pid,SIGTERM);

} else if($pid){

// add the pid to the current fork

$this->active_forks[] = $pid;

} else {

// Run our process

pcntl_exec('/usr/bin/PHP',array('/domain/dev/www/index.PHP','holder','process'));

exit(0);

}

}

public function restartSignalHandler(){

$forks = $this->active_forks;

foreach($forks as $pid){

$this->create_fork();

posix_kill($pid,SIGINT);

}

}

}

class holder {

public function process(){

$x = new Processor();

}

}

class Processor {

public function __construct(){

pcntl_signal(SIGINT,array($this,"shutdownSignalHandler"));

}

public function shutdownSignalHandler(){

echo "Shutting down";

exit;

}

}

以下是发生的事情:

>我开始编写脚本并正确获取进程(例如Parentpid:2,childpid:3)

>然后我发送父母一个SIGHUP信号,它正确杀死和

开始一个新的子进程(例如Parentpid:2,childpid:4)

>然后我向父母发送第二个SIGHUP信号并正确尝试并添加一个新的子进程,但它拒绝杀死第二个childpid. (例如,Parentpid:2,undyingchildpid:4,newchildpid:5)

Lemme知道是否需要更多细节/没有意义.我无法弄清楚为什么第一次它会正确地杀死孩子,但第二次它没有.

甚至WEIRDER部分是当我更改它以便我更改我的重新启动处理程序以便它继续尝试使用SIGINT杀死子项时,它每次都失败,但是当我发送一个SIGKILL命令它会杀死子进程:

if($time_passed > 60){

posix_kill($pid,SIGKILL);

}

我需要孩子能够被SIGINT杀死才能正确处理它.我不想只是SIGKILL它.是否有任何理由为什么第二次围绕SIGINT不起作用,但SIGKILL会?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值