为什么管后PS O / P列表中grep的进程? [英] Why does ps o/p list the grep process after the pipe?
问题描述
当我这样做
$ ps -ef | grep cron
我得到
root 1036 1 0 Jul28 ? 00:00:00 cron
abc 21025 14334 0 19:15 pts/2 00:00:00 grep --color=auto cron
我的问题是,为什么我看到第二行。从我的理解, PS
列出流程和管道列表以的grep
。 的grep
还没有开始,而 PS
正在上市进程在运行,那么如何来的grep
过程是在O / p上市?
My question is why do I see the second line. From my understanding, ps
lists the processes and pipes the list to grep
. grep
hasn't even started running while ps
is listing processes, then how come grep
process is listed in the o/p ?
相关的第二个问题:
当我这样做
$ ps -ef | grep [c]ron
只有
我得到
root 1036 1 0 Jul28 ? 00:00:00 cron
什么是第一和第二的grep
执行之间有什么区别?
推荐答案
在执行命令:
ps -ef | grep cron
您正在使用的shell
the shell you are using
(...我假设的bash在你的情况下,由于grep的颜色属性,我认为你在运行一个GNU系统就像一个Linux发行版,但它在其他UNIX /壳一样好......)
(...I assume bash in your case, due to the color attribute of grep I think you are running a gnu system like a linux distribution, but it's the same on other unix/shell as well...)
将执行管道()
呼吁建立一个FIFO,那么它会叉()
(作运行本身副本)。这将创建一个新的子进程。这种新生成的子进程将的close()
它的标准输出文件描述符(FD 1)和FD 1连接到由父亲过程中创建的管道写端(外壳在那里你执行的命令)。这是可能的,因为叉()
系统调用将继续保持,为每一个有效的打开文件描述符(管道FD在这种情况下)。这样做后,它会执行exec()
第一(你的情况) PS
在<$ C $发现命令C> PATH 环境变量。随着执行exec()
通话的过程将成为你执行的命令。
will execute the pipe()
call to create a FIFO, then it will fork()
(make a running copy of itself). This will create a new child process. This new generated child process will close()
its standard output file descriptor (fd 1) and attach the fd 1 to the write side of the pipe created by the father process (the shell where you executed the command). This is possible because the fork()
syscall will maintain, for each, a valid open file descriptor (the pipe fd in this case). After doing so it will exec()
the first (in your case) ps
command found in your PATH
environment variable. With the exec()
call the process will become the command you executed.
所以,你现在有一个孩子也就是在你的情况壳过程中,随着 -ef $ c中的
PS
命令$ C>属性。
So, you now have the shell process with a child that is, in your case, the ps
command with -ef
attributes.
在这一点上,父亲(外壳)叉()
s键一下。这个新生成的子进程的close()
开发它的标准输入文件描述符(FD 0)并附加FD 0由父进程创建的管道的读取端(外壳在那里你执行的命令)。
At this point, the father (the shell) fork()
s again. This newly generated child process close()
s its standard input file descriptor (fd 0) and attaches the fd 0 to the read side of the pipe created by the father process (the shell where you executed the command).
这样做后,它会执行exec()
第一(你的情况)的grep
在PATH命令发现环境变量。
After doing so it will exec()
the first (in your case) grep
command found in your PATH environment variable.
现在你有两个孩子(即是兄弟),其中第一个是 -ef $的
PS
命令shell进程C $ C>属性,第二个是与的cron
属性的grep
命令。管的读出侧被附连到 STDIN
在的grep
命令和写入侧的附连到 STDOUT
的 PS
命令:标准输出 PS
命令连接到的grep
命令的标准输入。
Now you have the shell process with two children (that are siblings) where the first one is the ps
command with -ef
attributes and the second one is the grep
command with the cron
attribute. The read side of the pipe is attached to the STDIN
of the grep
command and the write side is attached to the STDOUT
of the ps
command: the standard output of the ps
command is attached to the standard input of the grep
command.
由于 PS
被写入到每个正在运行的进程的标准输出信息发送,而grep的写入获得的标准输入的东西,有匹配给定模式,你必须回答你的第一个问题:
Since ps
is written to send on the standard output info on each running process, while grep is written to get on its standard input something that has to match a given pattern, you'll have the answer to your first question:
- 外壳运行:
PS -ef;
- 外壳运行:
的grep cron的;
-
PS
发送数据(甚至包含字符串的grep的cron),以的grep
-
的grep
匹配从STDIN
的搜索模式,这是因为串的grep cron的匹配 cron的属性,你传递给的grep
:你指示的grep
来匹配的cron的字符串,它不会因为grep的cron的,是当时的的grep
按PS
返回的字符串开始执行。
- the shell runs:
ps -ef;
- the shell runs:
grep cron;
ps
sends data (that even contains the string "grep cron") togrep
grep
matches its search pattern from theSTDIN
and it matches the string "grep cron" because of the "cron" attribute you passed in togrep
: you are instructinggrep
to match the "cron" string and it does because "grep cron" is a string returned byps
at the timegrep
has started its execution.
当你执行:
ps -ef | grep '[c]ron'
属性通过Instructs指示的grep
来匹配包含一些C,其次是罗恩。像第一实施例,但在这种情况下,它会破坏由 PS
返回的匹配字符串,因为:
the attribute passed instructs grep
to match something containing "c" followed by "ron". Like the first example, but in this case it will break the match string returned by ps
because:
- 外壳运行:
PS -ef;
- 外壳运行:
代表[C]罗恩;
-
PS
发送数据(甚至包含字符串的grep [C]罗恩
)以的grep
-
的grep
不因为一个字符串包含C的搜索模式从标准输入匹配,其次是罗恩,它没有找到,却发现包含字符串 C,其次是]罗恩
- the shell runs:
ps -ef;
- the shell runs:
rep [c]ron;
ps
sends data (that even contains the stringgrep [c]ron
) togrep
grep
does not match its search pattern from the stdin because a string containing "c" followed by "ron" it's not found, but it has found a string containing "c" followed by "]ron"
GNU 的grep
没有任何字符串匹配限制,在某些平台上(我认为的Solaris,HPUX,AIX)字符串的限制由$定COLUMN变量或终端的屏幕宽度。
GNU grep
does not have any string matching limit, and on some platforms (I think Solaris, HPUX, aix) the limit of the string is given by the "$COLUMN" variable or by the terminal's screen width.
希望这漫长的响应澄清壳管过程位。
Hopefully this long response clarifies the shell pipe process a bit.
提示:
ps -ef | grep cron | grep -v grep
这篇关于为什么管后PS O / P列表中grep的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!