2011-03-25 14 views
3

我想知道管道另一端的pid。如果是Linux,我可以像這些命令一樣匹配ls -l /proc/SELF_PID/fd/0的ID。如何獲得管道另一側的pid?

 
[[email protected] ~]# command1 | command2 & 

I've known command2's PID = 5912. 

[[email protected] ~]# ls -l /proc/5912/fd/0 
lr-x------ 1 root root 64 Mar 25 18:00 /proc/5912/fd/0 -> pipe:[540748072] 
[[email protected] ~]# ls -l /proc/[0-9]*/fd/1 | grep 'pipe:\[540748072\]' 
l-wx------ 1 root root 64 Mar 25 18:01 /proc/5911/fd/1 -> pipe:[540748072] 
[[email protected] ~]# cat /proc/5911/cmdline 
vmstat12 

Linux上有更好的方法嗎?或如何獲得BSD和其他操作系統?

而且我想知道如果有一個很好的CPAN模塊...

感謝。

+0

爲什麼你需要知道誰在管道上聆聽? – DarkDust 2011-03-25 09:14:08

+0

我想使用stdin管道另一側的命令名稱命名日誌文件。 – riywo 2011-03-25 09:25:56

+0

但是如果我做了'tee_log DarkDust 2011-03-25 09:56:43

回答

0

Perl編寫的過程中可以得到自己的PID與特殊的變量$$

如果你有一對管道,或插座,每一個過程可以只發送自己的PID進入管道或插座是由另一個進程讀取。

此外,父母通常通過系統調用創建它們來知道子女的PID。