如何在Linux中列出附加到共享内存段的进程? [英] How to list processes attached to a shared memory segment in linux?
问题描述
如何确定附加到共享内存段的进程是什么?
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
即如何确定shmid 98306附加了哪些两个进程?
i.e. how do I figure out which two processes are attached to shmid 98306?
推荐答案
我认为您无法使用标准工具来做到这一点.您可以使用ipcs -mp
获取要附加/分离的 last 进程的进程ID,但是我不知道如何使用ipcs
来获取 all 附加进程.
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 with ipcs
.
有两个进程的段,假设它们都保持固定,则可以从创建者的PID cpid
和最后一个的PID lpid
中找出这两个进程但这不会扩展到两个以上的过程,因此其用途受到限制.
With a two-process-attached segment, assuming they both stayed attached, you can possibly figure out from the creator PID cpid
and last-attached PID lpid
which are the two processes but that won't scale to more than two processes so its usefulness is limited.
cat /proc/sysvipc/shm
方法似乎受到类似的限制,但是我相信有一种方法可以对/proc
文件系统的其他部分进行处理,如下所示:
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:
当我在所有进程的procfs
映射上执行grep
时,会得到包含cpid
和lpid
进程行的条目.
When I do a grep
on the procfs
maps for all processes, I get entries containing lines for the cpid
and lpid
processes.
例如,我从ipcs -m
获取以下共享内存段:
For example, I get the following shared memory segment from ipcs -m
:
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 123456 pax 600 1024 2 dest
,对于给定的共享内存段(123456),cpid
为<3>,lpid
为9999.
and, from ipcs -mp
, the cpid
is 3956 and the lpid
is 9999 for that given shared memory segment (123456).
然后,使用命令grep 123456 /proc/*/maps
,我看到:
Then, with the command grep 123456 /proc/*/maps
, I see:
/proc/3956/maps: blah blah blah 123456 /SYSV000000 (deleted)
/proc/9999/maps: blah blah blah 123456 /SYSV000000 (deleted)
因此,有 一种获取附加到其上的进程的方法.我非常确定dest
状态和(deleted)
指示符是因为创建者已将片段标记为要在最终分离发生后销毁,而不是已经被销毁.
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.
因此,通过扫描/proc/*/maps
文件",您应该能够发现当前将哪些PID附加到给定段.
So, by scanning of the /proc/*/maps
"files", you should be able to discover which PIDs are currently attached to a given segment.
这篇关于如何在Linux中列出附加到共享内存段的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!