List IPC objects
Remove objects
File analogs
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.
Run ipcs with no arguments to see all three types of IPC objects:
—— 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.)
| 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 |
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)
ipcs -s)- nsems — number of semaphores in the set (System V semaphores are arrays)
ipcs -q)- used-bytes — total bytes of all messages currently in the queue
- messages — number of messages currently in the queue
ipcrm removes a specific IPC object. You can identify the object by either its key or its identifier:
$ 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
—— 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)
/* 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;
}
ipcs to see the three objects listed. Press Enter in the first terminal to clean them up.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;
}
#!/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
🎯 Interview Questions — ipcs & ipcrm
ipcrm -s 65538. To delete by key instead, use ipcrm -S 0xkey_value.← Previous: Identifier Algorithm Next: /proc/sysvipc & IPC Limits →
