Interactive pipe in C ++

I need to execute an external program and access its stdin and stdout alternatively like the console terminal itself. I have used popen (), but it does not provide bi-directional pipe. Using pipe () and fork () also does not work interactively, as the write pipe must be closed to access the read pipe.

Please help me come up with.

+3


source to share


3 answers


You need to open two pipes that you are connecting to the stdin of the child process to which you are connecting to stdout. You may also need a way to multiplex the I / O in your process.

Another option might be to use a pseudo-terminal which will give you two-way communication with the client software having the pseudo-terminal as an I / O pipe, although I'm not entirely sure what steps you take to do this, I just suggest that as I know other programs like xterm and ssh uses this method.



The same question has been asked before, and the answer is pretty much what I described in the first paragraph: simultaneous reading and writing by date (This answer contains some code that looks ok!)

+4


source


You will need to use OS-specific means to create separate pipes for stdout and stdin (and stderr, if you like). On POSIX platforms, you can use dup2()

to place the corresponding pipe ends on stdout and stdin (and stderr). You will have to restore the original descriptors after fork()

, so be sure to save them before placing new locations.



+1


source


The pipes don't work. You can use read-only or write-only.

Also, standard input and output cannot be the same endpoint due to direction. It comes from "pipeline work". Someone starts up and puts something on stdout, which can be used by someone else as stdin, and which puts it back on stdout for a third ...

For bi-directional communication, you need to find another way to communicate between processes. What it does depends on your implementation.

0


source







All Articles