Wednesday, November 12, 2008

Broad Usage Of ipcs And ipcrm On Linux And Unix

Hey there,

A wise man once said to me: "Mike, " he said; "If you always remember to keep your mind focused and follow the simple and logical path of progression in every area of your life, you will accomplish many great things." As I pondered his words, he added the following punctuation, which has since become a part of the definition of "me." He added: "Of course, you'll eventually die with the realization that you missed out on 99% of your life and you'll most probably be miserable and bored at the end of both the literal and figurative day. Well before you reach your expiration date your milk will have already turned into cottage cheese. Leave me now and go do something either irrational, contrary or both, but just go. And have fun!" He was probably the most upbeat nihilist I've ever met ;)

And today, to commemorate his entertaining, and often confusing, apocalyptic outlook on life, I'm going to make our post about something that has almost no relation whatsoever to any of our posts so far this week, this month or this year, and attack two commands that vary in options and output on almost every distro of Linux or Unix you'll ever pick up: ipcs (Inter-Process Communication Status) and ipcrm (Inter-Process Communication message queue, semaphore set or shared memory segment Remover). I salute, and dedicate this post to, you, Captain Trips (he never told me his real name. It was more than he could bear ;)

Hopefully this attempt to capture the essence of using ipcs and ipcrm (along with reading the man pages on your system) will make the whole experience easy to get into and fun to customize :) If you're at all interested, we did a post a while back on cleaning up Solaris shared memory that looks at a very specific use of IPC commands on a single Unix distribution.

As a special reminder, since we're covering such broad ground and trying to make this post accessible to users of the widest variety of ipcs and ipcrm versions out there, distro-specific extended options will not be covered in this post!

1. ipcs:

Basic Usage: This command is used to identify currently active (or hung in stasis) message queues, semaphore sets and/or shared memory segments. It best serves as a way to interpret possible system performance problems (commonly referred to as sluggishness ;) and can be very useful. You'll generally want to call it in one of four or five ways (for the most generic purposes):

host # ipcs -q <-- To query the message queue
host # ipcs -m <-- To query shared memory segments
host # ipcs -s <-- To query semaphore sets
host # ipcs -a <-- To query all three (like -qms)
host # ipcs <-- To query all three the same as with "-a". Use this if you're not sure if you have the "-a" option. It should be safer.

General Listing Information by column (Note that these may appear in different forms on your machine. For instance, the type (T) might be listed in a banner across the top of the output and columns like ID might be invocation specific, like shmid for "ipcs -m," etc):

Note: To avoid carpal tunnel syndrome, I'll be referring, from this point on, to messages queues as MQ's, shared memory segments as SHM's and Semaphore Sets as SEM's.

Columns generally shared amongst all object types:

T - The type of listing: q for MQ's, m for SHM's and s for SEM's
ID - The ID number of an MQ, SHM or SEM
KEY - The hexadecimal or, more often, octal key used to identify an MQ, SHM or SEM
MODE - The permissions of the particular MQ, SHM or SEM. These work almost exactly like regular file permissions, with a few exceptions:

1. The first two columns may be either of the following (or they may be backward). Some options may not exist on your version:

Usually column 1:

S : an ipcs object waiting for a msgsnd() call
- : the exact opposite of S if in the same column

Usually column 2:

R : an ipcs object waiting on a msgrcv() call
C : an SHM that will be cleared on next SHM attach operation
D : an SHM that has been removed but won't disappear until the last process attached to it detaches
- : the opposite of any or all of the above, or (in other words) none of the above

Columns 3 through 9: Owner, Group and Other permissions. These may consist of r (read), w (write) and a (alter) permissions. the "-" character indicates that the permission is not granted. Every permissions set (from left to right: Owner, Group and Other) is made of up three columns (r, w and a, with optional "-"'s in any column):


Columns specific to MQ's:

CREATOR: The username or UID that created the listed MQ
CGROUP: The group of the MQ's CREATOR
CBYTES: The number of bytes outstanding in the MQ
QNUM: The number of messages outstanding in the MQ
QBYTES: The maximum number of bytes allowed in messages outstanding in the MQ
LSPID: The last PID to execute a msgsnd call to the MQ
LRPID: The last PID to receive a msgrcv call from the MQ
STIME: The last time a msgsnd call was sent to the MQ
RTIME: The last time a msgrcv call was received from the MQ

Columns specific to SHM's:

CREATOR / SID: The username or UID that created the listed SHM
CGROUP: The group of the SHM's CREATOR
NATTACH: The number of processes attached to the SHM
SEGSZ: The size of the SHM
LPID: The last PID to attach to, or detach from, the SHM
ATIME: The time the last attach operation was executed on the SHM
DTIME: The time the last detach operation was executed on the SHM

Columns specific to SEM's:

CREATOR: The username or UID that created the listed SEM
CGROUP: The group of the SEM's CREATOR
NSEMS: The number of semaphores in the SEM
OTIME: The time the last semaphore operation was completed on the SEM

Other Column headers you may see, that can be easily matched up with their equals in the lists above:

Additional possible MQ columns:

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

Additional possible SHM columns:

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

Additional possible SEM columns:

------ Message Queues --------
key msqid owner perms used-bytes messages

2. ipcrm:

Basic Usage: This command is used to remove currently active (or hung in stasis) MQ's, SHM's and/or SEM's. In a perfect world, you'd never have to run this command, but (if you work with IPC a lot) you'll probably use it quite a bit (by which I'm, in no way, implying that this world is anything less than perfect ;) You'll generally want to call it in one of three to six ways (I've found that quite a few distro's do not allow removal by "key," as illustrated below):

These three work on every version I've tested:

host # ipcrm -q MQ_ID <-- To remove an MQ by ID
host # ipcrm -m SHM_ID <-- To remove an SHM by ID
host # ipcrm -s SEM_ID <-- To remove a SEM by ID

These three work on most Linux distro's, but not most Unix that I've tested:

host # ipcrm -Q MQ_KEY <-- To remove an MQ by hexadecimal or Octal KEY
host # ipcrm -M SHM_KEY <-- To remove an SHM by hexadecimal or Octal KEY
host # ipcrm -S SEM_KEY <-- To remove a SEM by hexadecimal or Octal KEY

And that, my friends, concludes the short version of this attempt to reasonably cover every possible widely-portable ipcs and ipcrm option (with result-layouts explained). As I mentioned at the beginning, be sure to check the man pages for your Unix or Linux distro's specific implementation of ipcs and ipcrm. You're liable to find a lot more options and/or how some options vary slightly from what has been posted here (For instance, Solaris 10 - with all the fancy zone functionality - has quite a few options most other OS's don't ;)

Enjoy and have a safe and peaceful morning, afternoon, evening and night :)

, Mike

Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.