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

喜欢而已 提交于 2020-01-04 04:28:34

问题


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 root 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.


回答1:


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.



来源:https://stackoverflow.com/questions/5430478/how-to-get-the-pid-of-the-other-side-of-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!