ipcrm Command in Linux



The ipcrm command in Linux removes Inter-Process Communication (IPC) resources. The IPC resources include objects from the system, like message queues, semaphores, and shared memory segments. Removing such objects requires superuser permissions.

System V IPC objects have three types: shared memory, message queues, and semaphores. When a message queue or semaphore is deleted, the action is instantaneous, even if some processes still have an IPC identifier for the object. However, a shared memory object is removed only after all processes currently attached to it have detached it from its virtual address space.

Table of Contents

Here is a comprehensive guide to the options available with the ipcrm command −

Note − While systemd provides robust service management and can handle resource cleanup tasks automatically, ipcrm still plays a crucial role in manually managing IPC resources, especially in complex applications or during debugging.

Syntax of ipcrm Command

The syntax of the Linux ipcrm command is as follows −

ipcrm [options] [ id | key ]

The [options] field is used to specify the various options to change the command’s behavior. Note that in Linux, particularly with System V IPC, two syntax styles are supported for the ipcrm command: the historical syntax and the newer syntax. The historical syntax of ipcrm requires an ID associated with the IPC object being removed, not a key. The newer or SUS-compliant syntax allows for the use of either an ID or a key, providing greater flexibility in how IPC objects are specified.

ipcrm Command Options

The options of the ipcrm command are listed below −

Flags Options Description
-a [shm|msg|sem] --all=[shm|msg|sem] To remove all resources
-M <key> --shmem-key <key> To remove shared memory segment by key
-m <id> --shmem-id <id> To remove shared memory segment by id
-Q <key> --queue-key <key> To remove message queue by key
-q <id> --queue-id <id> To remove message queue by id
-S <key> --semaphore-key <key> To remove semaphores by key
-s <id> --semaphore-id <id> To remove semaphores by id
-h --help To display help related to command
-V --version To display the command version

Examples of ipcrm Command in Linux

This section demonstrates the usage of the ipcrm command in Linux with examples −

Removing Shared Memory by ID

To list the shared memory IDs, use the ipcs command with the -m option −

ipcs -m
ipcrm Command in Linux1

The shared memory IDs are 0 and 1, as shown in the above output image. A shared memory ID of 0 generally refers to the first shared memory segment created, and 1 is the second.

To remove the shared memory by ID, use the -m or --shmem-id options with ipcrm command −

ipcrm -m 1
ipcrm Command in Linux2

The ipcrm command itself does not produce output when it successfully removes IPC resources, as it is designed to perform actions without returning messages. However, if there is an error, it will provide an error message, as shown in the output image above.

Removing Shared Memory by Key

To list the shared memory keys, use the same command mentioned in the previous section −

ipcs -m
ipcrm Command in Linux3

To remove the shared memory by key, use the -M or --shmem-key options −

ipcrm -M 0x001637bd

Removing Message Queue by ID

To list the message queue IDs, use the ipcs command with the -q option −

ipcs -q
ipcrm Command in Linux4

The message queue ID is 0, as indicated in the output above. An ID of 0 usually represents the first message queue that was created.

To remove the message queue by ID, use the -q or --queue-id options with the ipcrm command −

ipcrm -q 0

Removing Message Queue by Key

To list the message queue key, use the command mentioned below −

ipcs -q

To remove the message queue by key, use the -Q or --queue-key options with ipcrm command −

ipcrm -Q 0xffffffff

Removing Semaphore by ID

To list the semaphore IDs, use the ipcs command with the -s option −

ipcs -s
ipcrm Command in Linux5

The semaphore ID is 1, as shown in the output above, and typically represents the first semaphore created.

To remove the semaphore by ID, use the -s or --semaphore-id options with ipcrm command −

ipcrm -s 1

Removing Semaphore by Key

To list the semaphore keys, use the same command mentioned in the previous section −

ipcs -s

To remove the semaphore by key, use the -S or --semaphore-key options with ipcrm command −

ipcrm -S 0xffffffff

Removing All Resources

To remove all the resources, use the -a or --all options −

ipcrm -a

Similarly, to remove all resources related to shared memory, message queue, and semaphore, use the following commands −

ipcrm -a m
ipcrm -a q
ipcrm -a s

Warning − Use this command only if you are sure that no important applications depend on the IPC resources being removed.

Displaying Help

To display help related to a command, use -h or --help options −

ipcrm -h

Conclusion

The ipcrm command is a handy tool for managing Inter-Process Communication (IPC) resources in Linux. The ipcrm command comes with various options that help in customizing the resource management strategies to fit the specific needs.

While modern service managers like systemd handle many cleanup tasks automatically, ipcrm remains vital for manual management and debugging. The ability to use both historical and newer syntax styles enhances its versatility, allowing users to specify IPC objects by ID or key as needed.

This tutorial explained the ipcrm command, its syntax, options, and usage in Linux with examples.

Advertisements