使用proc_open时从STDIN管道读取

dah*_*hui 7 php proc-open

我正在尝试创建一个人们可以在线编译和运行代码的网站,因此我们需要找到一种用户发送指令的交互方式.

实际上,首先想到的是exec()或者system(),但是当用户想要输入时,这种方式将不起作用.所以我们必须使用proc_open().

例如,以下代码

int main()
{
    int a;
    printf("please input a integer\n");
    scanf("%d", &a);
    printf("Hello World %d!\n", a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我使用时proc_open(),就像这样

$descriptorspec = array(      
0 => array( 'pipe' , 'r' ) ,  
    1 => array( 'pipe' , 'w' ) ,  
    2 => array( 'file' , 'errors' , 'w' ) 
);  
$run_string = "cd ".$addr_base."; ./a.out 2>&1";
$process = proc_open($run_string, $descriptorspec, $pipes);
if (is_resource($process)) {
    //echo fgets($pipes[1])."<br/>";
    fwrite($pipes[0], '12');
    fclose($pipes[0]);
    while (!feof($pipes[1]))
        echo fgets($pipes[1])."<br/>";
    fclose($pipes[1]);
    proc_close($process);
}
Run Code Online (Sandbox Code Playgroud)

运行C代码时,我想获取第一个STDOUT流,然后输入数字,然后获取第二个STDOUT流.但如果我将注释行取消注释,该页面将被阻止.

有办法解决问题吗?如何在没有放入所有数据的情况下从管道中读取数据?或者有更好的方法来编写这种交互式程序吗?

hek*_*mgl 20

这更像是C一个glibc问题.你必须使用fflush(stdout).

为什么?a.out在终端中运行和从PHP调用它有什么区别?

答:如果你a.out在一个终端(stdin tty)中运行,那么glibc将使用行缓冲IO.但是如果你从另一个程序运行它(在这种情况下为PHP)并且它的stdin是一个管道(或者其他什么但不是tty),而glibc将使用内部IO缓冲.这就是为什么第一个fgets()阻止如果没有注释.有关详细信息,请查看此文章.

好消息:您可以使用该stdbuf命令控制此缓冲.更改$run_string到:

$run_string = "cd ".$addr_base.";stdbuf -o0 ./a.out 2>&1";
Run Code Online (Sandbox Code Playgroud)

这是一个有效的例子.即使C代码不关心,fflush()因为它正在使用stdbuf命令:

启动子流程

$cmd = 'stdbuf -o0 ./a.out 2>&1';

// what pipes should be used for STDIN, STDOUT and STDERR of the child
$descriptorspec = array (
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w")
 );

// open the child
$proc = proc_open (
    $cmd, $descriptorspec, $pipes, getcwd()
);
Run Code Online (Sandbox Code Playgroud)

将所有流设置为非阻塞模式

// set all streams to non blockin mode
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking(STDIN, 0);

// check if opening has succeed
if($proc === FALSE){
    throw new Exception('Cannot execute child process');
}
Run Code Online (Sandbox Code Playgroud)

得到孩子的pid.我们以后需要它

// get PID via get_status call
$status = proc_get_status($proc);
if($status === FALSE) {
    throw new Exception (sprintf(
        'Failed to obtain status information '
    ));
}
$pid = $status['pid'];
Run Code Online (Sandbox Code Playgroud)

轮询直到孩子终止

// now, poll for childs termination
while(true) {
    // detect if the child has terminated - the php way
    $status = proc_get_status($proc);
    // check retval
    if($status === FALSE) {
        throw new Exception ("Failed to obtain status information for $pid");
    }
    if($status['running'] === FALSE) {
        $exitcode = $status['exitcode'];
        $pid = -1;
        echo "child exited with code: $exitcode\n";
        exit($exitcode);
    }

    // read from childs stdout and stderr
    // avoid *forever* blocking through using a time out (50000usec)
    foreach(array(1, 2) as $desc) {
        // check stdout for data
        $read = array($pipes[$desc]);
        $write = NULL;
        $except = NULL;
        $tv = 0;
        $utv = 50000;

        $n = stream_select($read, $write, $except, $tv, $utv);
        if($n > 0) {
            do {
                $data = fread($pipes[$desc], 8092);
                fwrite(STDOUT, $data);
            } while (strlen($data) > 0);
        }
    }


    $read = array(STDIN);
    $n = stream_select($read, $write, $except, $tv, $utv);
    if($n > 0) {
        $input = fread(STDIN, 8092);
        // inpput to program
        fwrite($pipes[0], $input);
    }
}
Run Code Online (Sandbox Code Playgroud)