我可以用其他进程的管道做些什么吗?

问题描述

这里我想检查两个正在运行的进程之间的管道状态。我可以在 /proc/pid/fd 中识别两端的管道文件描述符,从而使用 ls -l 识别管道编号,如下所示:

l-wx------ 1 user user 64 Jul 22 08:53 5 -> 'pipe:[135687452]'

据我所知,管道在 Linux vfs 哲学中被视为一个文件,我怀疑有某种方法可以在不干扰所有者进程的情况下深入研究它。我可以用文件描述符或管道 ID 做什么来访问更多管道?希望能获取到管道中的内容或者管道中的内容长度。

非常感谢!

解决方法

阅读实际内容会干扰所有者进程。不过,您可以使用 FIONREAD 查看内容长度。每man 7 pipe

   The following ioctl(2) operation,which can be applied to a file
   descriptor that refers to either end of a pipe,places a count of
   the number of unread bytes in the pipe in the int buffer pointed
   to by the final argument of the call:

       ioctl(fd,FIONREAD,&nbytes);

   The FIONREAD operation is not specified in any standard,but is
   provided on many implementations.