I need to execute an external program and access its stdin and stdout alternatively, just like console terminal itself. I have used popen(), but it does not provide a bidirectional pipe. Using pipe() and fork() also does not work interactively, since the write pipe must be closed to access the read pipe.
Please give me some help to come up with it.
You need to open two pipes, one that you connect to stdin of the child process, one that you connect to stdout. You probably also need some way to multiplex input/output in your process.
Another option may be to use a pseudo-terminal, which will give you a two-way communication with the client software that has the pseudoterminal as it's I/O channel - although I'm not quite sure exactly the steps you go through to do this, I'm just suggesting it as I know other programs, such as xterm and ssh uses that method.
The same question has been asked before, and the answer is pretty much what I've described in the first paragraph: popen simultaneous read and write (This answer includes some code that looks OK!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With