So do modern Linux pipes work bi-directionally? Of course the shell doesn't use them like that. But that does not necessarily mean that the kernel wouldn't support it. I vaguely remember that a colleague used a pipe bi-directionally between 2 C programs he wrote. To my surprise it mostly worked. IIRC there were some minor issues make him giving up the approach. The big surprise for me was that it worked at all. Or is it just racy beyond all control that you read back your own data if the other end has happened not to empty the buffer?
No, they do not (nor on the modern BSD kernels, as far as I can tell). The Linux pipe(7) manpage says (under "Portability notes"):
«On some systems (but not Linux), pipes are bidirectional:
data can be transmitted in both directions between the
pipe ends. POSIX.1 requires only unidirectional pipes.
Portable applications should avoid reliance on
bidirectional pipe semantics.»
I believe the systems that supported bidirectional pipes were SysV kernels that implemented pipes using STREAMS and 4(?)BSD kernels that implemented it using socketpair.
That said, it is not based on socketpair any more. sys/kern/sys_pipe.c says:
/*
* This file contains a high-performance replacement for the socket-based
* pipes scheme originally used in FreeBSD/4.4Lite. It does not support
* all features of sockets, but does do everything that pipes normally
* do.
*/