简体   繁体   English

linux:为什么如果你通过管道将 ps 传送到 grep,grep 过滤器正在处理输出? 管道是如何工作的?

[英]linux: why if you pipe ps to grep, grep filter is in process output ? How does pipe works?

With a typical command like ps aux |grep foobar why grep foobar process is in output?使用像ps aux |grep foobar这样的典型命令,为什么grep foobar进程在输出中?

I was thinking that ps aux output was passed to grep to filter the lines by the pattern in grep argument but at the time you execute ps aux grep is still not executed (at least it's what I was supposing, I am wrong for sure), is executed later.我在想ps aux输出被传递给 grep 以通过 grep 参数中的模式过滤行,但是在您执行ps aux grep 时仍然没有执行(至少这是我的假设,我肯定错了),稍后执行。

How does pipe works then?那么管道是如何工作的呢?

The reason is simple:原因很简单:

  1. OS starts the grep process (as the pipe target).操作系统启动grep进程(作为管道目标)。
  2. OS starts the ps , which finds grep running .操作系统启动ps它发现grep正在运行
  3. OS connects the standard output of ps to the standard input of grep .操作系统将ps的标准输出连接到grep的标准输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM