proc_open() and pipes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jlevitt
Forum Newbie
Posts: 4
Joined: Thu Jul 10, 2008 10:28 am

proc_open() and pipes

Post by jlevitt »

Does anyone know how to do inter process communication using proc_open and pipes, but without using the stdin and stdout pipes?
What I want to do is have parent and child process communication, but I still want to be able to see the child's echos. The manual mentions that you don't have to use only the file descriptors 0, 1, and 2 when opening the pipes, but it does not mention how to access any other pipes.

I want to do something like:

Code: Select all

 
$descriptorspec = array(
   3 => array("pipe", "r"),  //  a pipe that the child will read from...3 is a non-standard file descriptor
   4 => array("pipe", "w"),  //  a pipe that the child will write to... 4 is a non-standard file descriptor
);
 
Instead of doing:

Code: Select all

 
$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("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
 
But I don't know how to access these pipes once they are opened. Can anyone shed light on this issue for me? Thanks in advance
-Jake Levitt
jlevitt
Forum Newbie
Posts: 4
Joined: Thu Jul 10, 2008 10:28 am

Re: proc_open() and pipes

Post by jlevitt »

I'm starting to think that what I want to do can't be done. Can anyone confirm this? Is there any other way to achieve the same functionality?

Thanks,
-Jake Levitt
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: proc_open() and pipes

Post by Benjamin »

proc_open returns a file pointer. Isn't that what your looking for? Have you been looking at the manual?

http://us2.php.net/manual/en/function.proc-open.php
jlevitt
Forum Newbie
Posts: 4
Joined: Thu Jul 10, 2008 10:28 am

Re: proc_open() and pipes

Post by jlevitt »

astions wrote:proc_open returns a file pointer. Isn't that what your looking for? Have you been looking at the manual?
proc_open returns a process resource and opens pipes which act like file pointers. What I'm asking is how I access these pipes in the opened process if the pipes are not 0, 1, or 2 (stdin, stdout, stderr). If I do use stdin/stdout, I can access them by doing $file = fopen("php://stdin", "r"), but doing it that way means that the child process's echos are no longer printed to the terminal; instead they go back to the parent process. I want a way for the parent and child to talk to each while still allowing the child to echo text to the terminal.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: proc_open() and pipes

Post by WebbieDave »

You've definitely been reading the manual :) However, the manual says you are not limited to 0, 1 and 2. You should take that to mean you can use more than just those (rather than instead of those). Also, the child process must have a means for you to send any higher file descriptor numbers to it (perhaps on the command line).

The parent could echo the child's output if you need to see it. If the child needs to stdout directly to the terminal, then proc_open is not what you need.
jlevitt
Forum Newbie
Posts: 4
Joined: Thu Jul 10, 2008 10:28 am

Re: proc_open() and pipes

Post by jlevitt »

WebbieDave wrote:Also, the child process must have a means for you to send any higher file descriptor numbers to it (perhaps on the command line).
Thanks for the reply, thats more like the answer I'm looking for. So even if I could send the higher file descriptor numbers, how would I open them? I can't just do

Code: Select all

$file = fopen(3, "r")
Its starting to look to me like I'll just have to live with communicating through stdin and stdout, but if anyone has any other suggestions, I'd love to hear them.

Thanks,
Jake Levitt
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: proc_open() and pipes

Post by WebbieDave »

I did not realize you were writing the child process program in PHP as well. Otherwise, I would have shared the bad news with you up front! I only know of accessing the fd in this manner through C. You will want to investigate to see if it is now possible through PHP Direct IO functions as it has, to my knowledge, not been possible to do. Also, perhaps there are third party extensions that help you accomplish this with PHP.

But, since you're also writing the parent, you'll be able to accomplish much with stdin/out.
Post Reply