tags:

views:

266

answers:

4

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?

+5  A: 

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.

David Dean
+11  A: 

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:

  1. other program terminates
  2. operating system closes pipe
  3. yes tries to output character
  4. error happens (broken pipe)
  5. yes terminates
Nick Fortescue
+1: quoting wikipedia.
S.Lott
+1 for a much better explanation than mine.
Paul Tomblin
+1, thanks for the explanation.
artknish
+3  A: 

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.

Paul Tomblin
+4  A: 

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.

Douglas Leeder