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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:40:26  来源:igfitidea点击:

How to list processes attached to a shared memory segment in linux?

linuxmemorymemory-managementshared

提问by Andrew Wagner

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?

即我如何确定哪个两个进程附加到 shmid 98306?

采纳答案by paxdiablo

I don't think you can do this with the standard tools. You can use ipcs -mpto get the process ID of the lastprocess to attach/detach but I'm not aware of how to get allattached processes with ipcs.

我不认为你可以用标准工具做到这一点。您可以使用ipcs -mp获得的进程ID最后过程中安装/拆卸,但我不知道如何让所有的连接过程与ipcs

With a two-process-attached segment, assuming they both stayedattached, you can possibly figure out from the creator PID cpidand last-attached PID lpidwhich are the two processes but that won't scale to more than two processes so its usefulness is limited.

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

The cat /proc/sysvipc/shmmethod seems similarly limited but I believe there's a way to do it with other parts of the /procfilesystem, as shown below:

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

When I do a grepon the procfsmaps for all processes, I get entries containing lines for the cpidand lpidprocesses.

当我grepprocfs所有进程的映射上执行 a时,我得到包含cpidlpid进程行的条目。

For example, I get the following shared memory segment from ipcs -m:

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

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

and, from ipcs -mp, the cpidis 3956 and the lpidis 9999 for that given shared memory segment (123456).

并且,从ipcs -mp中,cpid是3956和lpid9999对于给定共享存储器段(123456)。

Then, with the command grep 123456 /proc/*/maps, I see:

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

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

So there isa way to get the processes that attached to it. I'm pretty certain that the deststatus 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.

因此,有一个办法让连接到它的过程。我很确定dest状态和(deleted)指示器是因为一旦发生最终分离,创建者已将段标记为销毁,而不是它已经被销毁。

So, by scanning of the /proc/*/maps"files", you should be able to discover which PIDs are currently attached to a given segment.

因此,通过扫描/proc/*/maps“文件”,您应该能够发现当前附加到给定段的 PID。

回答by chaosless

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

给出你上面的例子 - 找到附加到 shmid 98306 的进程

lsof | egrep "98306|COMMAND"

回答by jacuro

I wrote a tool called who_attach_shm.pl, it parses /proc/[pid]/maps to get the information. you can download it from github

我写了一个名为 who_attach_shm.pl 的工具,它解析 /proc/[pid]/maps 以获取信息。你可以从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

回答by Darshan Sharma

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

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

Here is the image of the output:

这是输出的图像:

image

图片

回答by rwitzel

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.

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