Need a 2-way alternative to popen()

Michael Frank mpf at csli.Stanford.EDU
Mon Feb 26 19:52:27 AEST 1990


Jonathan Kamens at MIT writes:
>   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.

Yeah, I was afraid it would be something like that.  I just hoped
there would be an easier way.  Oh well, does anyone have example
source code that does this kind of thing?  Especially the hard parts?

>   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....

Such is the case.  Might there be another way to have output flushed
automatically, like with ioctl or something?  I really only need the
csh process to flush its output just as it does on a regular tty.  So
I just need the pipe to send the csh process's output through and not
wait for its buffer to fill.  Can do?  Yeah, I know; "RTFM".  I will.

>   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.

I don't have them; does anyone else out there care to send them to me?

Thanks for the info, Jonathan.

Mike Frank
bugboy at portia.stanford.edu



More information about the Comp.unix.questions mailing list