19

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

3
  • 1
    In /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.
    – lgeorget
    Commented May 12, 2013 at 18:43
  • @lgeorget and myself have confirmed that both POSIX and SysV semaphores do in fact show up in the /proc/PID/maps file for a given PID.
    – slm
    Commented May 12, 2013 at 19:12
  • But apparently, shared memory fragments also show up as entries in /dev/shm and they are pretty indistinguishable from semaphores (except if they have clever names).
    – lgeorget
    Commented May 12, 2013 at 19:14

1 Answer 1

30

My 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

POSIX & SystemV

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
5
  • 2
    As far as I know, only System V semaphores (those you get with 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.
    – lgeorget
    Commented May 12, 2013 at 18:50
  • Know of any terminal commands that will return the sem_open(2) variety? Just curious. Everything I know about semaphores is in the answer above 8-).
    – slm
    Commented May 12, 2013 at 18:52
  • No, that's the problem. ;) As the 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 :).
    – lgeorget
    Commented May 12, 2013 at 18:55
  • the contents of the map are both variety's of semaphores thought, correct?
    – slm
    Commented May 12, 2013 at 18:59
  • I guess so. One way or another, semaphores have to be attached somewhere in the process memory. But I was not sure SysV semaphores were also created in the pseudo-filesystem /dev/shm. Now, we have the answer. :)
    – lgeorget
    Commented May 12, 2013 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .