2

我使用 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 将使我的硬盘已满。所以我需要删除文件并重新启动计算机。我的问题是如何处理这样的事情?

谢谢 :)

4

1 回答 1

0

你唯一能做的就是在不影响使用的情况下杀死有问题的进程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
}

之后不要忘记清理你手中的任何把手。

于 2012-03-19T08:57:34.453 回答