Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive pipe in C++

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.

like image 433
user2029269 Avatar asked Feb 25 '26 21:02

user2029269


1 Answers

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!)

like image 52
Mats Petersson Avatar answered Feb 28 '26 10:02

Mats Petersson