POSIX shared memory: find out if anyone is attached to a segment
In System V shared memory (shmget()
), it is possible to find out how many attached users there are for a se开发者_Python百科gment by using shmctl(... IPC_STAT ...)
and reading the field shm_nattch
of the resulting data structure.
With POSIX shared memory (shm_open()
), is there any way to do something similar, or at least find out whether anyone is attached?
There is at least one way, somewhat cumbersome and probably not quite portable. You can scan /proc
filesystem looking for processes that have this SHM object opened. Use readlink
on "/proc/<self-PID>/fd/<shm-FD>"
to get the object path, and then scan "/proc/[0-9]+/fd/*
symlinks, comparing file names they point to or, better yet, device and inode numbers as returned by stat
. Or you can simply delegate this task to fuser
utility and parse its output.
精彩评论