tags:

views:

37

answers:

1

In The Unix Programming Environment by K & P, it is written that
" The programs in a pipeline actually run at the same time, not one after another.
This means that programs in a pipeline can be interactive;"

How can programs run at same time?
For ex: $ who | grep mary | wc -l
How grep mary will be executed until who is run or how wc -l will be executed until it knows results of previous programs?

+6  A: 
  • All three programs will start. grep and wc wait for input via stdin
  • who will output a line of data, which grep will then receive
  • If the line matches, grep will write it to stdout, which wc will then read and count
  • In the meantime, who may also have been writing out more data for grep etc

Each program needs the results of the previous one, but it doesn't need all of the results before it can start working, which is why pipelining is feasible.

Jon Skeet