Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does commands-piping work in *NIX?

Tags:

linux

unix

pipe

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?

like image 926
Srikanth Avatar asked Feb 19 '09 13:02

Srikanth


2 Answers

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
like image 171
Nick Fortescue Avatar answered Oct 01 '22 07:10

Nick Fortescue


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.

like image 45
David Dean Avatar answered Oct 01 '22 06:10

David Dean