使用PHP监视分叉的孩子 [英] Monitoring Children Forked using PHP

查看:110
本文介绍了使用PHP监视分叉的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我使用pcntl_fork在PHP中派生了一个进程,

I have a problem, I use pcntl_fork to fork a process in PHP,

$pid = pcntl_fork();
if ($pid == -1) {
 die('could not fork');
} else if ($pid) {
 // we are the parent
 pcntl_wait($status); //Protect against Zombie children
} else {
 pcntl_exec("/path/to/php/script");
 echo "Could not Execute...";
}

我试图找到一种方法来监视在父fork中作为Child执行的PHP脚本的状态.我们有什么办法可以知道子进程是否仍在运行,或者在执行子脚本期间是否出现任何致命错误,并使用以下方法捕获子进程到父进程的所有消息;

I am trying to figure out a way to monitor the status of the PHP script executed as the Child in the parent fork. Is there any way we can know if the child is still running or if there was any fatal errors raised during the execution of the child script and to catch all the messages from the child to parent process using;

pcntl_signal(SIGUSR1, "signal_handler");

感谢&问候,
阿伦·香克·普拉萨德(Arun Shanker Prasad).

Thanks & Regards,
Arun Shanker Prasad.

推荐答案

您绝对可以监视子进程:

You can definitely monitor the child process:

$pid = pcntl_fork();
if ($pid == -1) {
 die('could not fork');
} else if ($pid) {
 // we are the parent
 pcntl_waitpid($pid, $status, WUNTRACED); //Protect against Zombie children
 if (pcntl_wifexited($status)) {
   echo "Child exited normally";
 } else if (pcntl_wifstopped($status)) {
   echo "Signal: ", pcntl_wstopsig($status), " caused this child to stop.";
 } else if (pcntl_wifsignaled($status)) {
   echo "Signal: ",pcntl_wtermsig($status)," caused this child to exit with return code: ", pcntl_wexitstatus($status);
 }
} else {
 pcntl_exec("/path/to/php/script");
 echo "Could not Execute...";
}

  • pcntl_wifexited()-检查状态码是否代表正常退出
  • pcntl_wifstopped()-检查子进程当前是否已停止
  • pcntl_wifsignaled()-检查状态码是否表示由于信号而终止
  • pcntl_wexitstatus()-返回终止子级的返回码
  • pcntl_wtermsig()-返回导致子项终止的信号
  • pcntl_wstopsig()-返回导致孩子停止的信号
    • pcntl_wifexited() - Checks if status code represents a normal exit
    • pcntl_wifstopped() - Checks whether the child process is currently stopped
    • pcntl_wifsignaled() - Checks whether the status code represents a termination due to a signal
    • pcntl_wexitstatus() - Returns the return code of a terminated child
    • pcntl_wtermsig() - Returns the signal which caused the child to terminate
    • pcntl_wstopsig() - Returns the signal which caused the child to stop
    • 澄清有关父子进程之间的消息传递;您绝对不能跨进程捕获异常.就消息传递而言,仅使用PCNTL库,您还仅限于处理信号和退出代码.

      To clarify regarding messaging between parent and child process; you definitely cannot catch Exceptions across processes. As far as messaging, using only the PCNTL library you are also limited to process signals and exit codes.

      不知道您到底在做什么.您还有其他多种选择.我建议使用以下异步消息传递解决方案之一,因为它们可能满足您的需求.

      Not knowing what, exactly, you are doing. You have a variety of other options. I'd suggest one of the following asynchronous messaging solutions, as they could possibly cover your needs.

      基于文件

      您的子进程可以将消息写入文件,该文件将由父进程轮询.

      Your child processes could write messages to a file, which would be polled by the parent.

      基于内存缓存

      与上述相同,但使用memcached作为通信介质.

      Same as above, but using memcached as the communications medium.

      基于数据库

      与上述相同,但使用db表作为通信介质.

      Same as above, but using a db table as the communications medium.

      PHP的信号灯/IPC库

      http://us3.php.net/manual/en/book. sem.php

      这允许您使用msg_send()和msg_receive()之类的方法来跨进程进行通信.

      This allows you to use methods like msg_send() and msg_receive() to communicate across processes.

      我有信心其中之一将为您提供所需的解决方案.不过,详细介绍如何使用这些方法中的任何一种方法可能都超出了此问题的范围,但是如果您需要选择使用哪种方法的帮助,请随时提出新的问题.

      I'm confident one of these will provide the solution you are looking for. Going into the specifics of how to use any of these methods, is probably beyond the scope of this question though, but feel free to ask a new question if you need help with whichever method you choose to utilize.

      这篇关于使用PHP监视分叉的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆