Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Some programs can have an infinite runtime and produce infinite output. When used in a pipe with a command that finish, how does the pipeline knows when to stop ?

For instance, take yes (infinite output) with head:

 yes | head -n 5

It will only yield 5 y.

Is the first command that end the one that stop the other processes input/output ?
How does it works ?

share|improve this question

marked as duplicate by Gilles, Stephen Kitt, garethTheRed, Anthon, Jeff Schaller Mar 8 at 15:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
up vote 14 down vote accepted

The commands are connected with a pipe (I'm talking the system primitive here—obviously, they're connected with a |). When the read end of the pipe (the stdin of head) becomes closed (== when head either explicitly (close) or implicitly (exit) closes it), attempts to write to the write end (stdout of yes) will fail.

By default, this is not just a regular errno failure, but a failure that results in the writing process receiving the SIGPIPE signal. The default handler action of the SIGPIPE signal is to terminate.

In short—if you write to a broken pipe, the system will send you a SIGPIPE and by default, a SIGPIPE will kill you. That is why yes terminates when head has ended (and thereby broken the pipe).


If you ignore SIGPIPE in the parent shell, then the commands will inherit this disposition and a write to a broken pipe will simply cause an errno error of EPIPE. As it appears, yes stringifies this error an prints it:

$ (trap "" SIGPIPE; yes | head -n 5)  
y
y
y
y
yes: standard output: Broken pipe
yes: write error
share|improve this answer
1  
That was exaclty the answer I was looking for. So a part of a pipe can terminate but the other continue (if SIGPIPE is ignored) ? Have you an example in mind ? – Cyrbil Mar 8 at 9:58
    
@Cyrbil In the example with the trap, yes exits purely voluntarily. Consider a pipeline where the writer is a while loop: (trap "" SIGPIPE; while : ; do echo a; echo b >&2; sleep 0.2; done | head -n 5)—it goes happily on except echo a; will only succeed 5 times. Similarly, the reader end can close its stdin explicitly (in C by doing close(0);, in shell by doing exec 0>&-)(probably causing the writer process to die from SIGPIPE) rather than implicitly (=by exitting) and continue . – PSkocik Mar 8 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.