如何列出 Linux 中附加到共享内存段的进程?

发布于 2024-11-01 01:08:39 字数 556 浏览 1 评论 0原文

如何确定哪个进程附加到共享内存段?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-11-08 01:08:39

我认为使用标准工具无法做到这一点。您可以使用 ipcs -mp 来获取要附加/分离的最后一个进程的进程 ID,但我不知道如何获取所有进程> 使用ipcs附加进程。

对于两个进程附加的段,假设它们都保持附加,您可以从创建者 PID cpid 和最后附加的 PID lpid 中找出code> 这是两个进程,但不会扩展到两个以上的进程,因此它的用处是有限的。

cat /proc/sysvipc/shm 方法似乎同样受到限制,但我相信有一种方法可以对 /proc 文件系统的其他部分执行此操作,如下所示

:在所有进程的 procfs 映射上执行 grep,我得到包含 cpidlpid 进程行的条目。

例如,我从 ipcs -m 获得以下共享内存段:

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 123456     pax        600        1024       2          dest

并且,从 ipcs -mp 中,cpid 为 3956,<对于给定的共享内存段 (123456),code>lpid 是 9999。

然后,使用命令 grep 123456 /proc/*/maps,我看到:

/proc/3956/maps: blah blah blah 123456 /SYSV000000 (deleted)
/proc/9999/maps: blah blah blah 123456 /SYSV000000 (deleted)

所以有一种方法可以获取附加到它的进程。我非常确定 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 with ipcs.

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.

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 the procfs maps for all processes, I get entries containing lines for the cpid and lpid processes.

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

and, from ipcs -mp, the cpid is 3956 and the lpid is 9999 for that given shared memory segment (123456).

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)

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.

公布 2024-11-08 01:08:39

给出上面的示例 - 查找附加到 shmid 98306 的进程

lsof | egrep "98306|COMMAND"

given your example above - to find processes attached to shmid 98306

lsof | egrep "98306|COMMAND"
旧情勿念 2024-11-08 01:08:39

我写了一个名为 who_attach_shm.pl 的工具,它解析 /proc/[pid]/maps 来获取信息。
您可以从 github

示例输出下载它:

shm attach process list, group by shm key
##################################################################

0x2d5feab4:    /home/curu/mem_dumper /home/curu/playd
0x4e47fc6c:    /home/curu/playd
0x77da6cfe:    /home/curu/mem_dumper /home/curu/playd /home/curu/scand

##################################################################
process shm usage
##################################################################
/home/curu/mem_dumper [2]:    0x2d5feab4 0x77da6cfe
/home/curu/playd [3]:    0x2d5feab4 0x4e47fc6c 0x77da6cfe
/home/curu/scand [1]:    0x77da6cfe

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:

shm attach process list, group by shm key
##################################################################

0x2d5feab4:    /home/curu/mem_dumper /home/curu/playd
0x4e47fc6c:    /home/curu/playd
0x77da6cfe:    /home/curu/mem_dumper /home/curu/playd /home/curu/scand

##################################################################
process shm usage
##################################################################
/home/curu/mem_dumper [2]:    0x2d5feab4 0x77da6cfe
/home/curu/playd [3]:    0x2d5feab4 0x4e47fc6c 0x77da6cfe
/home/curu/scand [1]:    0x77da6cfe
北方的韩爷 2024-11-08 01:08:39

以防万一有人只对创建共享内存的进程感兴趣,请调用

ls -l /dev/shm

它列出与共享内存关联的名称 - 至少在 Ubuntu 上。通常这些名字很能说明问题。

Just in case someone is interest only in what kind of process created the shared moeries, call

ls -l /dev/shm

It lists the names that are associated with the shared memories - at least on Ubuntu. Usually the names are quite telling.

浅沫记忆 2024-11-08 01:08:39

使用 ipcs -a:它提供所有资源的详细信息 [信号量、共享内存等]

这是输出的图像:

图片

Use ipcs -a: it gives detailed information of all resources [semaphore, shared-memory etc]

Here is the image of the output:

image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文