4

I was wondering why the relations between the processes which can be used by pipes are different in Linux API and Bash.

  • In Linux API, unnamed pipes (pipe()) can be used only between parent-child processes.

  • In Bash, pipes can be used between two processes which have a shell process as their common parent.

Are pipes in Bash implemented in terms of unnamed pipes in Linux API? Thanks.

From APUE 3ed:

15.2 Pipes

Pipes are the oldest form of UNIX System IPC and are provided by all UNIX systems. Pipes have two limitations.

  1. Historically, they have been half duplex (i.e., data flows in only one direction). Some systems now provide full-duplex pipes, but for maximum portability, we should never assume that this is the case.

  2. Pipes can be used only between processes that have a common ancestor. Normally, a pipe is created by a process, that process calls fork, and the pipe is used between the parent and the child.

We’ll see that FIFOs (Section 15.5) get around the second limitation, and that UNIX domain sockets (Section 17.2) get around both limitations.

...

15.5 FIFOs

FIFOs are sometimes called named pipes. Unnamed pipes can be used only between related processes when a common ancestor has created the pipe. With FIFOs, however, unrelated processes can exchange data.

Community
  • 1
  • 1
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

7
  • In Linux API, unnamed pipes (pipe()) can be used only between parent-child processes.

Not true. They can be used by any process that has either of the file descriptors: the parent process, any child process, any process that has received them via domain sockets, etc.

Pipes in bash are simply a specific case of child-to-child communcation.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks. The sentence I quote comes from Advanced Programming Unix Environment (APUE). What does it mean actually? – Tim Aug 19 '17 at 02:15
  • 1
    @Tim: can you give a 'chapter and verse' on the APUE quote? That's Stevens, presumably? If what you quote is an accurate quote, then it is an inaccurate statement. Ignoring file descriptor transfers, there must be a common ancestor that created the pipe for two processes to be able to talk via an unnamed pipe. – Jonathan Leffler Aug 19 '17 at 02:30
  • @Tim: Well, what you quote is accurate and doesn't say 'parent-child'; it just says they must have a common ancestor (and doesn't quite say that the common ancestor must have created the pipe). I'm not even sure that 'normally used between parent and child' is necessarily valid, though it is not too far off — I'd go with 'often' rather than 'normally'. – Jonathan Leffler Aug 19 '17 at 02:37
  • @JonathanLeffler I am not sure. The book sometimes say that pipes are used between processes with common ancestor, and sometimes say between parent and child processes. What does that mean? – Tim Aug 19 '17 at 02:38
  • 1
    @Tim: Common ancestor: `int fd[2]; pipe(fd); int pid1 = fork(); if (pid1 == 0) { …use pipe in child 1… } else if ((pid2 = fork()) == 0) { …use pipe in child 2… } else { close(fd[0]); close(fd[1]); int corpse, status; while ((corpse = wait(&status)) > 0) { …report death of a child?… } }`. The parent closes the pipe, but the two children have the pipe open. They share a common ancestor. – Jonathan Leffler Aug 19 '17 at 02:40
  • Thanks. @JonathanLeffler – Tim Aug 19 '17 at 02:47
  • 1
    @JonathanLeffler Is the case which Ignacio said " any process that has received them via domain sockets", do the processes also have a common ancestor? – Tim Aug 19 '17 at 02:49
  • @Tim: Domain sockets get around all that "needing to be related" stuff. The only requirement is that the processes be running under the same kernel. – Ignacio Vazquez-Abrams Aug 19 '17 at 02:50
  • @Tim: yes, they have a common ancestor in the sense that all processes have PID 1 as a common ancestor. However, with the file descriptor transfers, the common ancestor doesn't need to have created the pipe, which is just as well. The normal way of thinking about it, therefore, is "No, they don't need to have a common ancestor that created the pipe". – Jonathan Leffler Aug 19 '17 at 02:50
  • @Tim one thing to note is that **usually** if you can use sockets, then you'd use them for the actual communication **instead** of just using them to transfer open file descriptors to *pipes*. – Antti Haapala -- Слава Україні Aug 19 '17 at 08:11