Search code examples
clinuxpopen

Two way pipe in Linux


So if I use popen() in the gcc stdio.h, I can pipe a parent's stdout to its child's stdin, or I could pipe the child's stdout to the parent's stdin. I want to do both at the same time. Is there a way to do this using popen, or do I need to make this work using lower level tools (like shared memory)?

NOTE: It looks like in FreeBSD popen can be used read-write with "r+". I do not understand exactly how the child's stdout is both piped to the parent and the parent's stdout. Anyay, this would probably work for me, but it does not appear to be the case in Linux. If I am mistaken on this point, that would answer my question. Here is a link to where I am getting this note: https://www.freebsd.org/cgi/man.cgi?query=popen&sektion=3

NOTE 2: I would prefer a solution that is portable accross just about every Linux distro, FreeBSD, and OSX, in order of priority. That being said, I'd be happy to at least make it work in Ubuntu 14.04.


Solution

  • You're absolutely correct.

    Some environments support an "enhanced" popen() with bi-directional pipes - but it's not "standard".

    If you want to be portable to all versions of 'nix (Linux, BSD, OSX, etc), you should use "pipe/fork/dup2":