I'm trying to get some C++ code running on Linux that a colleague wrote for OS X. It's mostly portable, but there's one part I don't know how to handle. He uses popen(CMD, "r+")
to open a bidirectional pipe and communicates with the process through fget
and fprint
.
On Linux the type r+
is not available as pipes can only be unidirectional. I guess usually the way would be to handle bidirectional communication through a socket but the called CMD
is an interactive program over which we have only limited control.
After some searching I found some snippets for a popen2
implementation in C. It works kinda but it would be much easier if there would be a real "C++" way to do it using streams instead of low-level functions like write
and read
. Also I have some problems where a lot of communication gets missing.
Anyway, my question is: What is the best way to port code that uses bidirectional popen()
to POSIX (or at least OS X + Linux)?
socketpair(2) creates a pair of sockets that are endpoints of a bidirectional pipe. This will directly replace popen()
. I do not see why CMD
being or not being an interactive program has anything to do with it.
It is possible that the program might need to have an actual terminal device (unlikely, since popen
() doesn't really emulate one). If so, the pty(7) man page provides additional documentation for creating a bi-directional pipe that drives a pseudo-tty device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.