Date: Mon, 16 Jul 2001 18:57:21 -0400 From: Klaus Steden <klaus@compt.com> To: Gabriel Ambuehl <gabriel_ambuehl@buz.ch> Cc: questions@freebsd.org Subject: Re: popen() with bidirectional pipes? Message-ID: <20010716185721.C73359@cthulu.compt.com> In-Reply-To: <194127057398.20010716230711@buz.ch>; from gabriel_ambuehl@buz.ch on Mon, Jul 16, 2001 at 11:07:11PM %2B0200 References: <64111861237.20010716185355@buz.ch> <20010716160341.S73359@cthulu.compt.com> <194127057398.20010716230711@buz.ch>
next in thread | previous in thread | raw e-mail | index | archive | help
> > Hello Klaus, > > Monday, July 16, 2001, 10:03:41 PM, you wrote: > > > popen() doesn't do bi-directional communication. Pipes (in the > > POSIX spec) only work in one direction. > > What about: > Since popen() is now implemented using a bidirectional pipe, the > type argument may request a bidirectional data flow. > The type argument is a pointer to a null-terminated string which > must be > `r' for reading, `w' for writing, or `r+' for reading and > writing. > I really hope you weren't the person to ask the question in the first place since I spent time writing both the first reply and this one to a question that could have been answered by R-ingTFM in the first place. However ... that said ... bi-directional pipes may exist on FreeBSD, but they aren't a guarantee on any other *nix flavour. Hence, my suggestion to try socketpair(), which is bi-directional by design. If the author intends to port outside FreeBSD, then he's stuck using either socketpair() or a pair of pipe() calls. From the online POSIX reference manual page for popen() ... -- cut -- The mode argument to popen() is a string that specifies I/O mode: 1.If mode is r, when the child process is started its file descriptor STDOUT_FILENO will be the writable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), will be the readable end of the pipe. 2.If mode is w, when the child process is started its file descriptor STDIN_FILENO will be the readable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), will be the writable end of the pipe. 3.If mode is any other value, the result is undefined. -- cut -- (http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html) Using vendor-supplied extras that fall outside the bounds of the spec, while convenient, usually result in poor portability. Poor portability, IMO, is a hallmark of bad programming. To portably create an IPC channel with a single function call, you must use socketpair(). A Unix-domain socket offers similar functionality as a bi-directional named pipe. cheers, Klaus To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010716185721.C73359>