How to do a pipe & fork?
a.v.reed
avr at mtgzz.att.com
Fri Nov 4 07:56:45 AEST 1988
In article <Nov.2.14.51.36.1988.8260 at zztop.rutgers.edu>, pratt at zztop.rutgers.edu (Lorien Y. Pratt) writes:
> I have two processes that I want to communicate. I want the parent to
> be able to fprintf to the child's stdin and to fscanf from the child's
> stdout. I know that I need to fork and then in the child do an execlp,
> but I can't figure out how to set up the file descriptors so I can do
> what I want. The Sun IPC guide does not help me here. I know that I
> need to do a dup and a fork and a pipe, but I'm not sure in what order
> and with what parameters.
The most legible, general, portable (at least to other Sun and AT&T
derived systems), easy to maintain and modify, and only slightly less
then optimally efficient solution is to start up your process from a
shell script after creating a named pipe, and passing its name as an
argument to the process:
if /etc/mknod /usr/tmp/pipe$$ p
then exec $LIB/yourprocess -p /usr/tmp/pipe$$
fi
echo "yourprocess: can't make named pipe /usr/tmp/pipe$$; exiting"
exit 5 # EIO
In the executable $LIB/yourprocess, read the pipe name with getopt(),
fopen it for "r+", then fork and exec, write in the parent and read in
the child. When the child is finished fclose() the pipe fd, and,
in the parent, fclose() and unlink the named pipe when an attempt to
write sets errno to EPIPE.
Adam Reed (avr at mtgzz.ATT.COM)
More information about the Comp.unix.wizards
mailing list