need to close stdin pipe from proc_open() before reading stdout pipe??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernhard Kuemel

    need to close stdin pipe from proc_open() before reading stdout pipe??

    Hi!

    I want to read/write commands and program input to/from /bin/bash
    several times before I close the stdin pipe. However, reading
    from cat hangs unless I first close the stdin pipe.


    <?php
    $descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child
    will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child
    will write to
    2 => array("pipe", "w") // stderr
    );
    $process = proc_open("/bin/bash", $descriptorspec , $pipes);
    if (is_resource($p rocess)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // 2 => readable handle connected to child stderr

    echo rand()," cat...<br>\n";
    flush();
    fwrite($pipes[0], "cat\n");
    #
    sleep(1);

    echo rand()," hallo...<br>\n" ;
    flush();
    fwrite($pipes[0], "hallo<br>\ n");
    fflush($pipes[0]); # fflush() doesn't help

    # ********* I don't want to fclose stdin here
    fclose($pipes[0]);

    echo rand()," reading stdout...<br>\n ";
    flush();
    while(!feof($pi pes[1])) {
    echo fgets($pipes[1], 1024);
    }

    echo rand()," reading stderr...<br>\n ";
    flush();
    while(!feof($pi pes[2])) {
    echo fgets($pipes[2], 1024);
    }

    # ********* I want to fclose stdin here, but reading from stdin
    above will hang then
    #
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($pro cess);

    echo "command returned $return_value\n ";
    }
    ?>

    --
    Webspace; Low end Serverhousing ab 15 e, etc.: http://www.bksys.at
    Linux Admin/Programmierer: http://bksys.at/bernhard/services.html

Working...