IBM Support

Processes and Shared Memory Segments

Question & Answer


Question

How do I list shared memory segments used by a process?

Answer

Introduction
How do I list all shared memory segments used by a process?
Example

Introduction

The AIX process memory model divides the process memory space into multiple 256 MB segments. There are different types of segments used for various purposes. One type of segment is the shared memory segment. AIX provides a shared memory facility for use by applications consisting of multiple processes that need to manipulate the same data. Programs implement shared memory regions by using the System V Shared Memory services (shmat) and/or the BSD Memory Mapped services (mmap). Both of these services consists of a few system functions that can be used to create shared memory regions and attach them to processes. The ipcs -mS command can be used to view all shared memory segments that have been allocated on the entire system, and the svmon -P <PID> command can be used to view shared memory segments that are currently being used by a specific process.

Another related type of segment is the memory mapped segment, which is implemented in programs using the BSD Memory Mapped Services mentioned above. The memory mapping facility can be used by applications to map files directly into memory to avoid the overhead of file buffering. This facility can also be used to create shared memory regions. And in some cases the System V Shared Memory services will automatically call the BSD Memory Mapped services to create shared memory regions. This happens within the shmat interface and for the purposes of this document, is only a concern when using the ipcs and svmon commands to view shared memory regions. This is because the labels used in svmon command output are different for shared memory segments and memory mapped segments.

Both shared memory and memory mapped segments are under the control of the application, although a system administrator may use commands to see which segments have been allocated, and remove segments that for some reason were not removed by an application and are no longer needed.

Note: The reasons why shared memory regions might be created internally using the memory mapping facility are beyond the scope of this document. However the operating characteristics and usage of these types of shared memory segments are identical to those created by the shared memory facility.

How do I list all shared memory segments used by a process?

The svmon -P <PID> command will list all types of memory segments used by a process, including shared memory segments and memory mapped segments. Each segment has a segment label that is used to denote the type of segment. Three different labels are used in svmon output to denote shared memory segments.
  • shared memory (32-bit processes only)
    This label is used for shared memory segments from 32-bit processes that do not use extended shared memory.

    Note: A 32-bit process will use extended shared memory if the EXTSHM environment variable is set to ON, 1SEG, or MSEG. If this variable is not set, the process will not use extended shared memory.
     
  • mmap maps (32-bit processes only)
    This label might be used for some extended shared memory segments for 32-bit processes. It will also be used for all memory mapped segments for 32-bit processes.

    Note: The EXTSHM environment variable can have a number of different values. The label used by svmon depends on the value of this environment variable and the size of the shared memory segment. If EXTSHM is set to ON or 1SEG and the size of the segment is less than 256 MB, the segment will be created internally with the memory mapping facility, and thus the label will be mmap maps. Any segment equal or greater than 256 MB will be created internally with the shared memory facility and thus the label will be shared memory. If EXTSHM is set to MSEG, all shared memory will be created internally with the memory mapping facility and thus the label will be mmap maps.
     
  • shmat/mmap (64-bit processes only)
    This label is used for both shared memory segments and memory mapped segments for all 64-bit processes.

    Note: Extended shared memory is not applicable to 64-bit processes.
The following commands can be used to list shared memory segments for the different process types:
  • 32-bit processes that do not used extended shared memory
    # svmon -P <PID> | egrep "Vsid|shared memory"

    Note: Output will only include shared memory segments, even if the process also uses memory mapping.
     
  • 32-bit processes that use extended shared memory
    # svmon -P <PID> | egrep "Vsid|shared memory|mmap maps"

    Note: If the process uses memory mapping, output will also include memory mapped segments.
     
  • All 64-bit processes
    # svmon -P <PID> | egrep "Vsid|shmat/mmap"

    Note: If the process uses memory mapping, output will also include memory mapped segments.
Because shared memory segments are sometimes labeled as memory mapped segments, it is not possible with svmon command output alone to know if a segment labeled as a memory mapped segment is actually used as a shared memory segment. To do this, output from svmon -P must be correlated with the output from ipcs -mS. The svmon -P command identifies all types of memory segments with a unique virtual segment ID, or Vsid. The ipcs -mS command lists the virtual segment ID only for segments used as shared memory segments, so the ipcs output can be used to see which segments in the svmon -P output are shared memory segments.

Note: The virtual segment ID in ipcs output is labeled with SID instead of Vsid in svmon -P output.

Example

For this example, we will use a 32-bit executable that does not use extended shared memory. For the other types of processes identified in the section above, the procedure is the same except for the search pattern used with the grep command.

# file /lgfs/jcs/shmem/test
/lgfs/jcs/shmem/test: executable (RISC System/6000) or object module
# ps g eww | grep test
    root 274594 356394    0    -    0:01 /lgfs/jcs/shmem/test

# ps g eww | grep 274594 | grep EXTSHM
#


This is a 32-bit executable that does not use extended shared memory. We see from the svmon output that this process has only one shared memory segment.

# svmon -P 274594 | egrep "Vsid|shared memory"
    Vsid      Esid Type Description              PSize  Inuse   Pin Pgsp Virtual
   22359         4 work shared memory segment        s      1     0    0     1

To find the shared memory identifier, run the ipcs -mS command and search for Vsid 22359.

# ipcs -mS
IPC status from /dev/mem as of Thu Dec  3 08:41:07 CST 2009
T        ID     KEY        MODE       OWNER    GROUP
Shared Memory:
m   1048577 0xffffffff --rw-rw----     root   system   <<
SID :                                                  <<
0x22359                                                <<
m   1048578 0x78000004 --rw-rw-rw-     root   system
SID :
0x2e29f
m         3 0x0d000f97 --rw-rw----     root   system
SID :
0x636b
m         4 0xffffffff --rw-rw----     root   system
SID :
0x14502
m 277872645 0x780005ef --rw-rw-rw-     root   system
SID :
0x6e8b

We see the process with PID 274594 is attached to one shared memory segment with the shared memory identifier 1048577 and SID 22359. Unfortunately because of the way the output from ipcs -mS is formatted, we can't use the paragraph -p option on grep to search for the SID. For example, the command below will return the shared memory identifier for the shared memory segment below the one associated with SID 22359 instead of the correct one located above SID 22359.

# ipcs -mS | grep -p: 22359
0x22359
m   1048578 0x78000004 --rw-rw-rw-     root   system

To search the ipcs output for a specific SID, we would need to use a different method than grep alone.

How do I list all processes attached to a shared memory segment?

We can also easily find all processes that are attached to a single shared memory segment. For example, using the output from ipcs -mS above, we can find all processes that are attached to the shared memory segment with SID 22359 by running the following command:

# svmon -S 0x22359 -l

    Vsid      Esid Type Description              PSize  Inuse   Pin Pgsp Virtual
   22359         4 work shared memory segment        s      1     0    0     1
                   pid(s)=274594

From this output we see that the shared memory segment with a Vsid (the same as the SID) of 22359 is attached by only one process with PID 274594.

Additional Resources

You can read more about shared memory and file mapping on the following pages, located within the AIX General Programming Concepts: Writing and Debugging Programs guide.

Shared Memory
Understanding Memory Mapping

Conclusion

Shared memory segments are created by application programs, and exist on the system until the application removes them. In some cases an application might fail to remove its shared memory segments. A system administrator can use the svmon and ipcs commands to locate shared memory segments used by one or more processes, and can also use the ipcrm command to remove shared memory segments. The svmon and ipcs commands can be used together to find the shared memory segments that are used by a process, and to find all processes that are attached to a particular shared memory segment.
 
 
 
 

[{"Type":"MASTER","Line of Business":{"code":"LOB08","label":"Cognitive Systems"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG10","label":"AIX"},"ARM Category":[{"code":"a8m0z000000cvzgAAA","label":"Commands"}],"ARM Case Number":"","Platform":[{"code":"PF002","label":"AIX"}],"Version":"All Versions"}]

Document Information

Modified date:
08 April 2025

UID

isg3T1012013