Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!think!snorkelwacker!bloom-beacon!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.unix.questions Subject: Re: Need a 2-way alternative to popen() Message-ID: <1990Feb26.071630.10948@athena.mit.edu> Date: 26 Feb 90 07:16:30 GMT References: <9539@portia.Stanford.EDU> Sender: news@athena.mit.edu (News system) Reply-To: jik@athena.mit.edu (Jonathan I. Kamens) Organization: Massachusetts Institute of Technology Lines: 35 (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@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8495 Home: 617-782-0710