【问题标题】:how to handle infinite loop process when using proc_open使用proc_open时如何处理无限循环过程
【发布时间】:2012-03-19 08:54:02
【问题描述】:

我使用proc_open来执行一个c语言创建的程序。

我使用文件作为“stdout”。

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("file", "/tmp/example.output"),
   2 => array("file", "/tmp/example.error", "a")
);

当我执行好的程序时一切都很好,但是当我执行无限循环程序时出现问题,如下面的代码:

#include "stdio.h"

int main(){
    while(1){
        printf("Example");
    }
    return 0
}

文件 example.output 将使我的硬盘满。所以我需要删除文件并重新启动计算机。我的问题是如何处理这样的事情?

谢谢:)

【问题讨论】:

  • 有时间限制怎么样? (如果你真的不想删除你的<infy loop>
  • 当运行这样的无限循环时,您期望会发生什么?
  • 如果任何程序中存在无限循环,请先修复它。
  • hjpotter92:我也设置了时间限制,所以当超过时间限制时,系统会杀死 proc_open 进程 cillosis:我希望我能杀死 proc_open 进程并节省我的硬盘空间:D Shiplu:实际上是无限循环程序是用户的输入。所以我需要处理这件事

标签: php proc-open


【解决方案1】:

您唯一能做的就是使用proc_terminate 在不带偏见的情况下杀死有问题的进程(但您可以礼貌地让它先运行一段时间,实际上是对其完成施加时间限制)。

例如:

$proc = proc_open(...);
sleep(20); // give the process some time to run
$status = proc_get_status($proc); // see what it's doing
if($status['running']) {
    proc_terminate($proc); // kill it forcefully
}

之后别忘了清理你手中的任何把手。

【讨论】:

  • 我以前尝试过类似的东西,比如“jerhee”在id.php.net/manual/en/function.proc-terminate.php#81353 上说。此外,我使用 system() 删除了 example.output 文件。但我的硬盘还是满了。有什么想法吗?
  • @SunuPinasthikaFajar:不,你需要自己调试,抱歉。
  • 好的,谢谢,但我会尝试您的建议 :)。也许我会找到一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 2012-04-07
  • 2021-08-21
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多