本文整理自网络,参考网页(references):
- http://stackoverflow.com/questions/5658568/how-to-list-processes-attached-to-a-shared-memory-segment-in-linux
- http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzahz%2Fipcs.htm
[Question]:
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 segment 98305?
[Answer]:
I don't think you can do this with the standard tools. You can useipcs -mpto get the process ID of thelastprocess to attach/detach but I'm not aware of how to getallattached processes withipcs.
With a two-process-attached segment, assuming they bothstayedattached, you can possibly figure out from the creator PIDcpidand last-attached PIDlpidwhich are the two processes but that won't scale to more than two processes so its usefulness is limited.
Thecat /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:
When I do agrepon theprocfsmaps for all processes, I get entries containing lines for thecpidandlpidprocesses.
For example, I get the following shared memory segment fromipcs -m:
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 123456 pax 600 1024 2 dest
and, fromipcs -mp, thecpidis 3956 and thelpidis 9999 for that given shared memory segment (123456).
Then, with the commandgrep 123456/proc/*/maps, I see:
/proc/3956/maps: blah blah blah 123456 /SYSV000000 (deleted)
/proc/9999/maps: blah blah blah 123456 /SYSV000000 (deleted)
So thereisa way to get the processes that attached to it. I'm pretty certain that thedeststatus 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.
CPID(-p, -a) The process ID of the job that created the shared memory segment.
LPID(-p, -a) The process ID of the last job to attach or detach from the shared memory segment or change the semaphore value.
本文介绍如何确定哪些进程附连到了特定的共享内存段。通过使用ipcs命令及检查/proc文件系统来获取相关信息的方法被详细阐述。对于两个或更少进程附连的情况,可以通过比较创建者PID (cpid) 和最后附连/分离PID (lpid) 来推测这些进程。
1096

被折叠的 条评论
为什么被折叠?



