Reputation: 43
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.
Upvotes: 3
Views: 1289
Reputation: 129524
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!)
Upvotes: 4
Reputation: 18268
You will have to use OS specific facilities to create separate pipes for both stdout and stdin (and stderr, if you want to). On POSIX platforms you can use dup2()
to place appropriate pipe ends to stdout and stdin (and stderr). You will have to restore the original descriptors after you fork()
, so do not forget to save them before your place the new ones.
Upvotes: 1
Reputation: 2401
Pipes do not work that way. You can only use a read or write pipe.
Additionally, standard input and output cannot be the same end point due to direction. It comes from "pipeline work". Someone starts and puts something to stdout which can be used by someone else as stdin and which an again put it ot stdout for a third one...
For bi-directional communication you need to find another way of inter process communication. What it is depends on your implementation.
Upvotes: 0