How to get proccesses currently running semaphores by /proc?

79,980

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
Share:
79,980

Related videos on Youtube

Hanna
Author by

Hanna

Updated on September 18, 2022

Comments

  • Hanna
    Hanna over 1 year

    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

  • lgeorget
    lgeorget almost 11 years
    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
    lgeorget almost 11 years
    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 :).
  • slm
    slm almost 11 years
    the contents of the map are both variety's of semaphores thought, correct?
  • lgeorget
    lgeorget almost 11 years
    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. :)