Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list processes attached to a shared memory segment in linux?

How do I determine what process is attached to a shared memory segment?

awagner@tree:/home/awagner$ ipcs -m  ------ Shared Memory Segments -------- key        shmid      owner      perms      bytes      nattch     status       0x00000000 0          root       777        102400     1                        0x00000000 32769      root       774        96         1          dest          0x00000000 98306      awagner    600        393216     2          dest          0x00000000 131075     awagner    600        393216     2          dest     

i.e. how do I figure out which two processes are attached to shmid 98306?

like image 476
Andrew Wagner Avatar asked Apr 14 '11 04:04

Andrew Wagner


People also ask

How can I tell which process is using a shared memory?

To find the shared memory identifier, run the ipcs -mS command and search for Vsid 22359. We see the process with PID 274594 is attached to one shared memory segment with the shared memory identifier 1048577 and SID 22359.

Which of the following used for attaching a process to a shared memory segment?

Once created, a shared segment can be attached to a process address space using shmat().

Can we attach more than one process to shared memory segment?

To answer your question: Yes of course. You have the evidence right there in your question. How does it happen? If you call mmap() on the same file multiple times it maps it multiple times.

How do I find IPC in Linux?

All the IPC facility has unique key and identifier, which is used to identify an IPC facility. $ ipcs -q : It lists only message queues for which the current process has read access. # ipcs -s : To list the accessible semaphores. # ipcs -m : To lists the shared memories.


2 Answers

I don't think you can do this with the standard tools. You can use ipcs -mp to get the process ID of the last process to attach/detach but I'm not aware of how to get all attached processes with ipcs.

With a two-process-attached segment, assuming they both stayed attached, you can possibly figure out from the creator PID cpid and last-attached PID lpid which are the two processes but that won't scale to more than two processes so its usefulness is limited.

The cat /proc/sysvipc/shm method seems similarly limited but I believe there's a way to do it with other parts of the /proc filesystem, as shown below:

When I do a grep on the procfs maps for all processes, I get entries containing lines for the cpid and lpid processes.

For example, I get the following shared memory segment from ipcs -m:

------ Shared Memory Segments -------- key        shmid      owner      perms      bytes      nattch     status       0x00000000 123456     pax        600        1024       2          dest 

and, from ipcs -mp, the cpid is 3956 and the lpid is 9999 for that given shared memory segment (123456).

Then, with the command grep 123456 /proc/*/maps, I see:

/proc/3956/maps: blah blah blah 123456 /SYSV000000 (deleted) /proc/9999/maps: blah blah blah 123456 /SYSV000000 (deleted) 

So there is a way to get the processes that attached to it. I'm pretty certain that the dest status and (deleted) indicator are because the creator has marked the segment for destruction once the final detach occurs, not that it's already been destroyed.

So, by scanning of the /proc/*/maps "files", you should be able to discover which PIDs are currently attached to a given segment.

like image 73
paxdiablo Avatar answered Nov 01 '22 10:11

paxdiablo


given your example above - to find processes attached to shmid 98306

lsof | egrep "98306|COMMAND" 
like image 36
chaosless Avatar answered Nov 01 '22 09:11

chaosless