Pipes on Unix have a buffer, therefore, even if the right process (RSP) does not consume any data, the left process (LSP) is able to issue several kilobytes before locking.
Then, if the buffer is full, the LSP is blocked. When the RSP reads the data, it frees up part or all of the buffer space, and the LSP resumes work.
If instead of 2 processes you have 3, the situation is more or less the same: a faster producer is blocked by a slower consumer. And, obviously, a faster consumer is blocked by a slower producer if the channel becomes empty: just think about the interactive shell, waiting for the slowest producer of everything: the user.
For example, the following command:
$ yes | cat | more
Since more blocked when the screen is full until the user presses a key, the cat process will fill its output buffer and stop, then the yes process will fill its buffer and also stop. Everything that awaits the user, as it should be.
PS: I wonder what happens when the more process ends? well, the right side of this pipe is closed, so the cat process will receive a SIGPIPE signal (if it is ever recorded again in the pipe, and it will) and die. The same thing will happen with the yes process. All processes die, as it should be.
rodrigo
source share