I wonder how to get processes currently running semaphores by /proc
? I guess it's possible by SysVIPC subdirectory.But I don't know how to use this commands.
Ubuntu 12.10
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityMy only experience in dealing with semaphores and shared memory is through the use of the command ipcs
. Take a look at the ipcs man page for more details.
This command shows you what processes have semaphores:
$ ipcs -s
------ Semaphore Arrays --------
key semid owner perms nsems
0x4d114854 65536 saml 600 8
With the semid known we can query for addition info about the PIDs that have semaphores (note there are 8 - the nsems column):
$ ipcs -s -i 65536
Semaphore Array semid=65536
uid=500 gid=501 cuid=500 cgid=501
mode=0600, access_perms=0600
nsems = 8
otime = Sun May 12 14:44:53 2013
ctime = Wed May 8 22:12:15 2013
semnum value ncount zcount pid
0 1 0 0 0
1 1 0 0 0
2 1 0 0 2265
3 1 0 0 2265
4 1 0 0 0
5 1 0 0 0
6 1 0 0 4390
7 1 0 0 4390
The pid column are these processes. You can either look them up using ps
or look through the /proc
file-system, /proc/<pid>
.
For example:
$ more /proc/2265/cmdline
mono
Building off of a comment left by @lgeorget I dug into my PID 2265's /proc/2265/map
contents and did find the following /dev/shm
references:
$ grep shm /proc/2265/maps
7fa38e7f6000-7fa38ebdf000 rw-s 00000000 00:11 18517 /dev/shm/mono-shared-500-shared_fileshare-grinchy-Linux-x86_64-40-12-0
7fa38f0ca000-7fa38f0cb000 rw-s 00000000 00:11 18137 /dev/shm/mono.2265
7fa3967be000-7fa3967d3000 rw-s 00000000 00:11 18516 /dev/shm/mono-shared-500-shared_data-grinchy-Linux-x86_64-328-12-0
semget(2)
) show up in ipcs -s
so if you're using POSIX semaphores (those you get with sem_open(2)
), you have to use another method.
ipcs
manpage say: "The Linux ipcs utility is not fully compatible to the POSIX ipcs utility." so I'm not sure they even thought of something for POSIX IPC. Maybe a future release of ipcs
:).
map
are both variety's of semaphores thought, correct?
/dev/shm
. Now, we have the answer. :)
/proc/PID/maps
, you can see the memory mapping of a process and POSIX semaphores show up as attached files in/dev/shm
. I'm not sure about SysV semaphores though./proc/PID/maps
file for a given PID./dev/shm
and they are pretty indistinguishable from semaphores (except if they have clever names).