gpt4 book ai didi

php - 在 Linux 中使用 PHP 的 posix_kill() 导致奇怪的行为

转载 作者:IT王子 更新时间:2023-10-29 00:36:52 26 4
gpt4 key购买 nike

我正在创建一个网页,用于监视和控制一些自定义 C 程序。我创建了一个页面,它将启动/停止一个 C 程序“启动器”(漂亮的通用名称),然后 fork 并创建许多子进程。开始工作正常 - exec("cd launcher_dir; nohup ./launcher > outfile 2>&1 &");

停止是有问题的地方。单击停止按钮后,两件事中的一件会随机发生。要么存在浏览器错误页面(101 Connection Reset 或 324 Empty Response),要么页面加载了两次,但您只看到了第二次。我知道它加载两次的原因是因为代码中的调试消息。在这两种情况下,启动器进程都会被终止(发送 SIGTERM)。但是如果页面加载两次,第一次它会杀死启动器(这部分页面上没有加载任何东西),第二次它检查并发现没有启动器进程正在运行并显示一条消息:“启动器未运行”。

我正在将调试消息写入一个文件,发现重新加载发生在 php 代码中一个有点可变的行(有时会打印某个调试消息,其他时候不会。)此外,还设置了 php 错误报告到 ALL 并且没有给出错误。

Launcher 捕获 SIGTERM,依次将 SIGTERM 发送给它的子进程,然后调用 exit(0)。

有趣的是,如果使用 SIGKILL 来终止启动器,php 可以正常工作并且符合预期,但是这不允许启动器正常关闭。这里会发生什么?

相关的php代码如下:

function stop_launcher(){
$pid = get_launcher_pid(); // Definitely returns the correct pid
debug_message("stop_launcher called, pid = ".$pid);
if ($pid == "") {
// If no 'connection reset' error occurs this is displayed
// after first executing the else branch. Why is the php being run twice?
print "Launcher doesn't seem to be running.. <br />";
exit;
} else {
debug_message("killing");
posix_kill(intval($pid), 15); //SIGTERM
debug_message("kill finished"); // Sometimes this message is written, sometimes not
if (ps_exists($pid)) { // Definitely works. This never gets displayed
print "Shutdown failed. Try again</br>";
exit;
}
}
}

function debug_message($message){
$fh = fopen(".debug", 'a') or die("can't open file");
fwrite($fh, date("-r").": ".$message."\n");
fclose($fh);
}

如有任何建议,我们将不胜感激!

最佳答案

事实证明,这是让您感到愚蠢的错误之一!启动程序不小心杀死了它的进程组...

关于php - 在 Linux 中使用 PHP 的 posix_kill() 导致奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com