开发者

piping in UNIX doubt

开发者 https://www.devze.com 2022-12-27 04:01 出处:网络
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.

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, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消