如何列出 Linux 中附加到共享内存段的进程?
如何确定哪个进程附加到共享内存段?
awagner@tree:/home/awagner$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 root 777 102400 1
0x00000000 32769 root 774 96 1 dest
0x00000000 98306 awagner 600 393216 2 dest
0x00000000 131075 awagner 600 393216 2 dest
即我如何找出哪两个进程附加到 shmid 98306?
How do I determine what process is attached to a shared memory segment?
awagner@tree:/home/awagner$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 root 777 102400 1
0x00000000 32769 root 774 96 1 dest
0x00000000 98306 awagner 600 393216 2 dest
0x00000000 131075 awagner 600 393216 2 dest
i.e. how do I figure out which two processes are attached to shmid 98306?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为使用标准工具无法做到这一点。您可以使用 ipcs -mp 来获取要附加/分离的最后一个进程的进程 ID,但我不知道如何获取所有进程> 使用
ipcs
附加进程。对于两个进程附加的段,假设它们都保持附加,您可以从创建者 PID
cpid
和最后附加的 PIDlpid
中找出code> 这是两个进程,但不会扩展到两个以上的进程,因此它的用处是有限的。cat /proc/sysvipc/shm
方法似乎同样受到限制,但我相信有一种方法可以对/proc
文件系统的其他部分执行此操作,如下所示:在所有进程的
procfs
映射上执行grep
,我得到包含cpid
和lpid
进程行的条目。例如,我从
ipcs -m
获得以下共享内存段:并且,从
ipcs -mp
中,cpid
为 3956,<对于给定的共享内存段 (123456),code>lpid 是 9999。然后,使用命令
grep 123456 /proc/*/maps
,我看到:所以有一种方法可以获取附加到它的进程。我非常确定
dest
状态和(deleted)
指示器是因为创建者已将段标记为在最终分离发生后销毁,而不是它已经被销毁。因此,通过扫描
/proc/*/maps
“文件”,您应该能够发现哪些 PID 当前附加到给定段。I don't think you can do this with the standard tools. You can use
ipcs -mp
to get the process ID of the last process to attach/detach but I'm not aware of how to get all attached processes withipcs
.With a two-process-attached segment, assuming they both stayed attached, you can possibly figure out from the creator PID
cpid
and last-attached PIDlpid
which are the two processes but that won't scale to more than two processes so its usefulness is limited.The
cat /proc/sysvipc/shm
method seems similarly limited but I believe there's a way to do it with other parts of the/proc
filesystem, as shown below:When I do a
grep
on theprocfs
maps for all processes, I get entries containing lines for thecpid
andlpid
processes.For example, I get the following shared memory segment from
ipcs -m
:and, from
ipcs -mp
, thecpid
is 3956 and thelpid
is 9999 for that given shared memory segment (123456).Then, with the command
grep 123456 /proc/*/maps
, I see:So there is a way to get the processes that attached to it. I'm pretty certain that the
dest
status and(deleted)
indicator are because the creator has marked the segment for destruction once the final detach occurs, not that it's already been destroyed.So, by scanning of the
/proc/*/maps
"files", you should be able to discover which PIDs are currently attached to a given segment.给出上面的示例 - 查找附加到 shmid 98306 的进程
given your example above - to find processes attached to shmid 98306
我写了一个名为 who_attach_shm.pl 的工具,它解析 /proc/[pid]/maps 来获取信息。
您可以从 github
示例输出下载它:
I wrote a tool called who_attach_shm.pl, it parses /proc/[pid]/maps to get the information.
you can download it from github
sample output:
以防万一有人只对创建共享内存的进程感兴趣,请调用
它列出与共享内存关联的名称 - 至少在 Ubuntu 上。通常这些名字很能说明问题。
Just in case someone is interest only in what kind of process created the shared moeries, call
It lists the names that are associated with the shared memories - at least on Ubuntu. Usually the names are quite telling.
使用
ipcs -a
:它提供所有资源的详细信息 [信号量、共享内存等]这是输出的图像:
Use
ipcs -a
: it gives detailed information of all resources [semaphore, shared-memory etc]Here is the image of the output: