piping in UNIX doubt
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开发者_Go百科 until it knows results of previous programs?- All three programs will start. grep and wc wait for input via stdin
who
will output a line of data, whichgrep
will then receive- If the line matches, grep will write it to
stdout
, whichwc
will then read and count - In the meantime,
who
may also have been writing out more data forgrep
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.
精彩评论