ipcs & ipcrm — IPC Command-Line Tools Listing and Removing System V IPC Objects

 

ipcs & ipcrm — IPC Command-Line Tools
Chapter 45, Section 45.6 | Listing and Removing System V IPC Objects
ipcs
List IPC objects
ipcrm
Remove objects
ls / rm
File analogs
Key Terms
ipcs ipcrm -q message queue -s semaphore -m shared memory -Q / -q key/id nattch nsems

Why We Need These Tools

Because System V IPC objects have kernel persistence, they outlive the processes that created them. If an application crashes without cleaning up, IPC objects remain on the system — consuming resources and potentially causing confusion for future runs.

The ipcs and ipcrm commands are the System V IPC equivalents of the familiar ls and rm file commands. They let you inspect and clean up IPC objects from the shell.

ipcs — List All IPC Objects

Run ipcs with no arguments to see all three types of IPC objects:

$ ipcs

—— Shared Memory Segments ——–
key        shmid   owner   perms   bytes   nattch  status
0x6d0731db 262147  mtk     600     8192    2

—— Semaphore Arrays ——–
key        semid   owner   perms   nsems
0x6107c0b8 0       cecilia  660     6
0x6107c0b6 32769   britta   660     1

—— Message Queues ——–
key        msqid   owner   perms   used-bytes  messages
0x71075958 229376  cecilia  620     12          2

For each IPC object, ipcs shows:

  • key — the IPC key (in hex)
  • id (shmid/semid/msqid) — the integer identifier
  • owner — username of the owner
  • perms — permission mask in octal
  • Object-specific info (size, nattch, nsems, messages, etc.)
On Linux: ipcs only shows objects for which you have read permission. On some other UNIX implementations, it shows all objects regardless of permissions.

ipcs Options
Option Description
(none) Show all three types
-q Show only message queues
-s Show only semaphore sets
-m Show only shared memory segments
-l Show system limits for each IPC type
-u Show summary/usage statistics
-i id Show detailed info for specific object by ID
-p Show PID of creator and last user
-t Show time information
-a Show all available info

Object-Specific Information Shown by ipcs
📋 Shared Memory (ipcs -m)
  • bytes — size of the shared memory region in bytes
  • nattch — number of processes currently attached (via shmat)
  • status — whether region is locked in RAM (dest flag = marked for deletion)
🚦 Semaphores (ipcs -s)
  • nsems — number of semaphores in the set (System V semaphores are arrays)
📨 Message Queues (ipcs -q)
  • used-bytes — total bytes of all messages currently in the queue
  • messages — number of messages currently in the queue

ipcrm — Delete an IPC Object

ipcrm removes a specific IPC object. You can identify the object by either its key or its identifier:

ipcrm Syntax
$ ipcrm -Q key   # Delete message queue by key
$ ipcrm -q id    # Delete message queue by identifier
$ ipcrm -S key   # Delete semaphore set by key
$ ipcrm -s id    # Delete semaphore set by identifier
$ ipcrm -M key   # Delete shared memory segment by key
$ ipcrm -m id    # Delete shared memory segment by identifier
$ ipcs -s
—— Semaphore Arrays ——–
key        semid   owner   perms   nsems
0x6107c0b8 65538   ravi     600     1

$ ipcrm -s 65538
$ ipcs -s
—— Semaphore Arrays ——–
key        semid   owner   perms   nsems
(empty)

Coding Example 1 — Create IPC Objects and Inspect with ipcs
/* create_ipc_objects.c — create objects that you can inspect with ipcs */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void) {
    int msqid, semid, shmid;

    /* Create one of each type */
    msqid = msgget(IPC_PRIVATE, 0600);
    semid = semget(IPC_PRIVATE, 3, 0600);  /* set of 3 semaphores */
    shmid = shmget(IPC_PRIVATE, 8192, 0600);

    if (msqid == -1 || semid == -1 || shmid == -1) {
        perror("get call failed");
        exit(EXIT_FAILURE);
    }

    printf("Created IPC objects (PID=%d):\n", getpid());
    printf("  Message Queue ID: %d\n", msqid);
    printf("  Semaphore Set ID: %d\n", semid);
    printf("  Shared Memory ID: %d\n", shmid);
    printf("\nRun 'ipcs' in another terminal to see these objects.\n");
    printf("Run 'ipcrm -q %d' to delete the queue.\n", msqid);
    printf("Run 'ipcrm -s %d' to delete the semaphore set.\n", semid);
    printf("Run 'ipcrm -m %d' to delete the shared memory.\n", shmid);
    printf("\nPress Enter to delete and exit...\n");
    getchar();

    /* Cleanup */
    msgctl(msqid, IPC_RMID, NULL);
    semctl(semid, 0, IPC_RMID);
    shmctl(shmid, IPC_RMID, NULL);
    printf("All IPC objects deleted.\n");

    return 0;
}
Workflow: Run this program in one terminal. In another terminal, run ipcs to see the three objects listed. Press Enter in the first terminal to clean them up.

Coding Example 2 — List IPC Objects Programmatically via /proc

You can programmatically list IPC objects by reading the /proc/sysvipc files (covered in detail in the next tutorial). Here’s a preview:

/* list_ipc_proc.c — list IPC objects by reading /proc/sysvipc */
#include <stdio.h>
#include <stdlib.h>

void list_ipc_type(const char *proc_file, const char *type_name) {
    FILE *f = fopen(proc_file, "r");
    if (!f) {
        perror(proc_file);
        return;
    }

    printf("\n=== %s (%s) ===\n", type_name, proc_file);
    char line[512];
    int first = 1;
    while (fgets(line, sizeof(line), f)) {
        if (first) {
            /* Print header line */
            printf("Header: %s", line);
            first = 0;
        } else {
            printf("Entry:  %s", line);
        }
    }
    if (first) printf("(no objects)\n");
    fclose(f);
}

int main(void) {
    printf("Listing all System V IPC objects via /proc/sysvipc:\n");

    list_ipc_type("/proc/sysvipc/msg", "Message Queues");
    list_ipc_type("/proc/sysvipc/sem", "Semaphore Sets");
    list_ipc_type("/proc/sysvipc/shm", "Shared Memory Segments");

    return 0;
}
/proc/sysvipc advantage: Unlike ipcs, these files show all IPC objects regardless of whether you have read permission on them. They are read-only files maintained by the kernel.

Coding Example 3 — Shell Script to Clean Up All Your IPC Objects
#!/bin/bash
# cleanup_ipc.sh — Remove all System V IPC objects owned by current user

USER=$(whoami)
echo "Cleaning up IPC objects owned by: $USER"

# Remove message queues owned by this user
ipcs -q | awk -v user="$USER" '$3 == user {print $2}' | while read id; do
    echo "  Deleting message queue ID=$id"
    ipcrm -q "$id"
done

# Remove semaphore sets owned by this user
ipcs -s | awk -v user="$USER" '$3 == user {print $2}' | while read id; do
    echo "  Deleting semaphore set ID=$id"
    ipcrm -s "$id"
done

# Remove shared memory segments owned by this user
ipcs -m | awk -v user="$USER" '$3 == user {print $2}' | while read id; do
    echo "  Deleting shared memory segment ID=$id"
    ipcrm -m "$id"
done

echo "Done. Remaining IPC objects:"
ipcs
Use with caution! This script removes ALL your IPC objects. Only run it when you are sure no important applications are using System V IPC. Good for development cleanup after a crashed program leaves stale objects.

🎯 Interview Questions — ipcs & ipcrm

Q1. What are ipcs and ipcrm analogous to in terms of file commands?
A: ipcs is analogous to ls (lists IPC objects) and ipcrm is analogous to rm (removes IPC objects). The analogy reflects their roles in managing System V IPC resources.
Q2. What three sections does the default ipcs output have?
A: Shared Memory Segments, Semaphore Arrays, and Message Queues. Each section shows the key, identifier, owner, permissions, and object-specific information.
Q3. How do you delete a semaphore set with identifier 65538 using ipcrm?
A: ipcrm -s 65538. To delete by key instead, use ipcrm -S 0xkey_value.
Q4. What does the nattch field mean in ipcs shared memory output?
A: nattch is the number of processes currently attached to (having called shmat on) that shared memory segment. This helps you know if the segment is actively in use.
Q5. On Linux, which IPC objects does ipcs show?
A: Only those for which the current user has read permission, regardless of whether they own the objects. This differs from some other UNIX implementations that show all objects regardless of permissions.
Q6. You see a shared memory segment in ipcs with status “dest”. What does that mean?
A: “dest” means the segment has been marked for deletion (IPC_RMID was called on it) but is deferred because processes are still attached. Once all processes detach (shmdt), it will be physically removed.
Q7. Why might a developer need to run ipcrm manually on a production system?
A: If an application crashes without cleaning up its IPC objects (due to kernel persistence), stale objects remain. These consume system resources and can prevent new instances of the application from creating fresh IPC objects (EEXIST errors). Manual cleanup with ipcrm is the solution.
Q8. What does the nsems column in ipcs -s output represent?
A: nsems is the number of individual semaphores in the semaphore set (array). System V semaphores are always allocated as arrays of multiple semaphores in a single call to semget().

Leave a Reply

Your email address will not be published. Required fields are marked *