Linux – pipe any two processes to each other

bsdforklinuxpipeprocess

In this page from The Design and Implementation of the 4.4BSD Operating System, it is said that:

A major difference between pipes and sockets is that pipes require a
common parent process to set up the communications channel

However, if I record correctly, the only way to create a new process is to fork an existing one. So I can’t really see how 2 processes could not have a common ancestor. Am I then right to think that any pair of processes can be piped to each other?

Best Answer

Am I then right to think that any pair of processes can be piped to each other?

Not really.

The pipes need to be set up by the parent process before the child or children are forked. Once the child process is forked, its file descriptors cannot be manipulated "from the outside" (ignoring things like debuggers), the parent (or any other process) can't do the "set up the comms. channel" part after the fact.

So if you take two random processes that are already running, you can't set up a pipe between them directly. You need to use some form of socket (or another IPC mechanism) to get them to communicate. (But note that some operating systems, FreeBSD among them, allow you to send file descriptors on Unix-domain sockets.)

Related Question