mooware
mooware

Reputation: 1762

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 segment 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?

Upvotes: 1

Views: 628

Answers (1)

rkhayrov
rkhayrov

Reputation: 10260

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.

Upvotes: 1

Related Questions