Need a 2-way alternative to popen()
Jonathan I. Kamens
jik at athena.mit.edu
Mon Feb 26 18:16:30 AEST 1990
(Note, I removed an unnecessary "usa" distribution on this thread. I
don't see why this discussion would only be interesting in the United States.)
I seem to recall this question being discussed in this newsgroup, or
in comp.unix.wizards, fairly recently, like within the past month.
Popen() is specifically designed only to do one-way communication. If
you want to do two-way communication with a process, the solution is to
do all of the pipe manipulation yourself. You use two calls to pipe()
(or one call to socketpair()) to create a pair of pipes, and then you
fork. In the child, you modify your stdin, stdout and stderr in such a
way that the pipes you just created are hooked up to it, and then you
exec the process to which you want to talk. The parent can then read
from/write to the pipes in order to talk to the process.
As others pointed out the last time this question was asked, you need
to be careful about the parent waiting for the output from the child and
vice versa, where buffering might occur, so the parent might be hung
waiting for output from the child, and the child hung waiting for output
from the parent. If you use stdio, make sure to flush the output sent
to the child, and the output from the child sent to the parent. Of
course, if you don't have the sources to the process you are executing
in the child, you can't make the child flush....
If you have sources to your C library, I suggest you take a look at
what it does; you may be able to figure out for yourself how to expand
popen() to deal with both reading and writing to a process.
See the pipe() and socketpair() man pages for more information.
Jonathan Kamens USnail:
MIT Project Athena 11 Ashford Terrace
jik at Athena.MIT.EDU Allston, MA 02134
Office: 617-253-8495 Home: 617-782-0710
More information about the Comp.unix.questions
mailing list