DEV Community

Francis Ndung'u
Francis Ndung'u

Posted on

List Processes Attached to a Shared Memory Segment in Linux

*1. Overview
*

In Linux, a shared memory segment is part of the memory that two or more processes can share. It’s a mechanism meant to facilitate the communication and sharing of data between processes. However, monitoring these processes attached to the shared memory segment can be demanding for beginners.

In this tutorial, we’ll discuss an option that we can use to list these processes. First, we’ll look at the ipcs command and then follow this up with the pstree command.

*2. List Processes Attached to a Shared Memory Segment
*

First, let’s start by taking a look at all the shared memory segments in our system:

$ ipcs -m
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 6          francis      600        524288     2          dest         
Enter fullscreen mode Exit fullscreen mode

From the example above, we’re using the ipcs command to display the shared memory segments. In detail, the ipcs command is a utility for displaying information on inter-process communication (IPC) mechanisms. Also, we add the -m option to filter the output to only show information about the shared memory segments available.

So, we can see that there’s only one segment in our system. Further, from the visible column names, we have to note the shared memory segment identifier (shmid) value which acts as the process identifier for the shared memory segment. This is because, for us to list the processes attached to a shared memory segment, we have to first determine the process ID of this shared memory segment that we’re interested in.

Secondly, we can view information specific to a shared memory segment of our choosing:

$ ipcs -m -i 6
Shared memory Segment shmid=6
uid=1000    gid=1000    cuid=1000   cgid=1000
mode=01600  access_perms=0600
bytes=524288    lpid=4766   cpid=1574   nattch=2
att_time=Sun Jan 29 15:40:28 2023  
det_time=Sun Jan 29 15:40:28 2023  
change_time=Sun Jan 29 15:25:30 2023

Enter fullscreen mode Exit fullscreen mode

Above, we’re displaying detailed information about the shared memory segment whose identifier is 6.

Next, we’re going to use the pstree command with shmid to list the attached processes:

$ pstree -p 6
rcu_par_gp(6)
Enter fullscreen mode Exit fullscreen mode

The pstree command lists the processes attached to this shared memory segment in a tree-like structure. This command shows the relationship between processes by presenting the hierarchy of parent processes with their child processes. Additionally, we use the -p option to instruct pstree to include process identifiers (PIDs) in the output.

*3. Conclusion
*

In this tutorial, we’ve learned that shared memory is an important feature for processes to share data. Also, by using the ipcs command we get to view the shared memory segments in our system. Additionally, by utilizing the pstree command we get to list processes generated from the shared memory segment using its identifier (shmid)

Top comments (0)