如何在Linux中列出附加到共享内存段的进程? [英] How to list processes attached to a shared memory segment in linux?

查看:153
本文介绍了如何在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时,会得到包含cpidlpid进程行的条目.

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屋!

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