简体   繁体   English

管道中的其他程序

[英]Other programs in pipe

If a program PROG is invoked with pipes, 如果使用管道调用程序PROG

progA | progB | PROG | progC ...

Is there a way for it to tell in what context it was invoked - ie, from/to what other programs (A, B, C...) it is receiving or sending piped output? 有没有办法告诉它在什么上下文中被调用-即,从/到其他程序(A,B,C ...)正在接收或发送管道输出?

I'm mostly interested in the immediate predecessor to PROG (in the example above, progB ), but am also curious about the more general question. 我最感兴趣的是PROG前身(在上面的示例中为progB ),但也对更笼统的问题感到好奇。

If you're on Linux you can use the /proc filesystem to check how commands communicate over pipes. 如果您使用的是Linux,则可以使用/proc文件系统检查命令如何通过管道进行通信。 However, this is not really portable. 但是,这并不是真正可移植的。

You can use ps to show all the processes which have the same parent. 您可以使用ps来显示具有相同父项的所有进程。 For example, if PROG has pid PID, on Linux you can do: 例如,如果PROG具有pid PID,则在Linux上,您可以执行以下操作:

ps --ppid $(ps -o ppid= $PID)

to get a listing of all the commands in the pipeline. 以获得管道中所有命令的清单。 (Actually, you'll get all the commands that are children of the shell that invoked the pipeline, which may be sufficient. You can check the process group of each to determine which ones are actually in the pipeline.) The order in which they print is not necessarily the order they appear in the pipe, put you can look in /proc/pid/fd to see the inode of each process' inputs to determine how they line up. (实际上,您将获得作为调用管道的Shell的子命令的所有命令,这可能就足够了。您可以检查每个命令的进程组以确定哪些命令确实在管道中。)它们的顺序打印不一定是它们在管道中出现的顺序,您可以在/proc/pid/fd中查看每个进程的输入的inode以确定它们如何排列。

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

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