如何获取管道另一侧的PID? [英] How to get the pid of the other side of pipe?

查看:63
本文介绍了如何获取管道另一侧的PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道管道另一侧的pid.如果是Linux,我可以像以下命令一样匹配 ls -l/proc/SELF_PID/fd/0 的ID.

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

在Linux上还有更好的方法吗?或如何使用BSD和其他操作系统?

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

我想知道是否有一个好的CPAN模块...

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

谢谢.

推荐答案

在Perl中,进程可以使用特殊变量$$获取其自己的PID

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

如果您有一对管道或一个套接字,则每个进程都可以将自己的PID发送到管道或套接字中,以供其他进程读取.

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.

此外,父母通常通过创建它们的系统调用来了解其孩子的PID.

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

这篇关于如何获取管道另一侧的PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆