开发者

How to get the pid of the other side of pipe?

I want to know pid of the other side of the pipe. If Linux, I can match up the id of ls -l /proc/SELF_PID/fd/0 like these commands.

[root@host ~]# command1 | command2 &

I've known command2's PID = 5912.

[root@host ~]# ls -l /proc/5912/fd/0 
lr-x------  1 roo开发者_开发知识库t root 64 Mar 25 18:00 /proc/5912/fd/0 -> pipe:[540748072]
[root@host ~]# 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]
[root@host ~]# cat /proc/5911/cmdline 
vmstat12

Are there better ways on Linux? or How to get on BSD and the other OS?

And I want to know if there is a good CPAN module...

Thanks.


In Perl a process can get its own PID with the special variable $$

If you have a pair of pipes, or a socket, each process could just send its own PID into the pipe or socket to be read by the other process.

Also, parents know the PIDs of their children usually through the system call creating them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜