Why does this shell pipeline exit?
Piping works by connection the output of one process A to the input of B. The connection can be broken, when
- A closes its output. B will get EOF.
- B closes its input. A will get an error that the output is no longer available when it tries to write the next byte.
Since these two cases are so common, the handling has been moved into the C standard lib.
head
closes the input file after reading the required amount. when a pipe is closed from one side, the other side gets write errors; this causes base64
to close, which in turn causes cat
to close.
After base64
outputs 10 bytes, head
gets enough inputs and exits. When the former attempts to output more bytes, it will receive SIGPIPE signal and hence exit too。For the same reason, cat
will exit in turn.