Reputation: 12120
When I do this:
find . -name "pattern" | grep "another-pattern"
Are the processes, find
and grep
, spawned together? My guess is yes. If so, then how does this work?:
yes | command_that_prompts_for_confirmations
If yes
is continuously sending 'y' to stdout and command_that_prompts_for_confirmations
reads 'y' whenever it's reading its stdin, how does yes
know when to terminate? Because if I run yes
alone without piping its output to some other command, it never ends.
But if piping commands don't spawn all the processes simultaneously, then how does yes
know how many 'y's to output? It's catch-22 for me here. Can someone explain me how this piping works in *NIX?
Upvotes: 5
Views: 1229
Reputation: 53320
Other answers have covered termination. The other facet is that yes will only output a limited number of y's - there is a buffer in the pipe, and once that is full yes will block in its write request. Thus yes doesn't consume infinite CPU time.
Upvotes: 4
Reputation: 7701
Yes, (generally speaking) all of the processes in a pipeline are spawned together. With regard to yes
and similar situations, a signal is passed back up the pipeline to indicate that it is no longer accepting input. Specifically: SIGPIPE
, details here and here. Lots more fun infomation on *nix pipelining is available on wikipedia.
You can see the SIGPIPE
in action if you interrupt a command that doesn't expect it, as you get a Broken Pipe
error. I can't seem to find an example that does it off the top of my head on my Ubuntu setup though.
Upvotes: 5
Reputation: 182880
The stdout of the first process is connected to the stdin of the second process, and so on down the line. "yes" exits when the second process is done because it no longer has a stdout to write to.
Upvotes: 3
Reputation: 44203
From the wikipedia page: "By itself, the yes command outputs 'y' or whatever is specified as an argument, followed by a newline, until stopped by the user or otherwise killed; when piped into a command, it will continue until the pipe breaks (i.e., the program completes its execution)."
yes
does not "know" when to terminate. However, at some point outputting "y" to stdout will cause an error because the other process has finished, that will cause a broken pipe, and yes will terminate.
The sequence is:
Upvotes: 11