How fast are Linux pipes anyway? (2022)
1–10 of 116 posts
Re: How fast are Linux pipes anyway? (2022)
#2Re: How fast are Linux pipes anyway? (2022)
#3Re: How fast are Linux pipes anyway? (2022)
#4https://www.gibney.org/the_output_of_linux_pipes_can_be_inde...
Re: How fast are Linux pipes anyway? (2022)
#5One surprising fact about Linux pipes I stumbled across 4 years ago is that using a pipe can create indeterministic behavior: https://www.gibney.org/the_output_of_linux_pipes_can_be_inde...
(echo red; echo green 1>&2) | echo blue
This creates two subshells separated by the pipe | symbol.
A subshell is a child process of the current shell, and as such it inherits important properties of the current shell, notably including the open file descriptor table.Since they are child processes, both subshells run concurrently, while their parent shell will simply wait() for all child processes to terminate. The order in which the childs get to run is to a large extent unpredictable, on a multi-core system they may run literally at the same time.
Now, before the subshells get to process their actual tasks, file redirections have to be performed. The left subshell gets its stdout redirected to the write end of the kernel pipe object that is "created" by the pipe symbol. Likewise, the right subshell gets stdin redirected to the read end of the pipe object.
The first subshell contains two processes (red and green) that run in sequence (";"). "Red" is indeed printed to stdout and thus (because of the redirection) sent to the pipe. However, nothing is ever read out of the pipe: The only process that is connected to the read end of the pipe ("echo blue") never reads anything, it is output only.
Unlike "echo red", "echo green >&2" doesn't have stdout connected to the pipe. Its stdout is redirected to whatever stderr is connected to. Here is the explanation what ">&2" (or equivalently, "1>&2") means: For the execution of "echo green", make stdout (1) point to the same object that stderr (2) points to. You can imagine it as being a simple assignment: fd[1] = fd[2].
For "echo blue", stdout isn't explicitly redirected, so it gets run with stdout set to whatever it inherited from its parent shell, which is (probably) your terminal.
Seeing that both "echo green" and "echo blue" write directly to the same file (again, probably your terminal) we have a race -- who wins is basically a question of who gets scheduled to run first. For one reason or other, it seems that blue is more likely to win on your system. It might be due to the fact that the left subshell needs to finish the "echo red" first, which does print to the pipe, and that might introduce a delay / a yield, or such.
Re: How fast are Linux pipes anyway? (2022)
#6Re: How fast are Linux pipes anyway? (2022)
#7One surprising fact about Linux pipes I stumbled across 4 years ago is that using a pipe can create indeterministic behavior: https://www.gibney.org/the_output_of_linux_pipes_can_be_inde...
Re: How fast are Linux pipes anyway? (2022)
#8Re: How fast are Linux pipes anyway? (2022)
#9It's interesting that memory mapping is so expensive. I've often wondered the price that everyone pays for multiple address spades. Is isolation really worth it?
Re: How fast are Linux pipes anyway? (2022)
#10(2022) Previous discussion: https://news.ycombinator.com/item?id=31592934
How fast are Linux pipes anyway? - https://news.ycombinator.com/item?id=31592934 - June 2022 (200 comments)