linux command to check POSIX message queue

55,741

Solution 1

There is no command I know of but there exists a libc function call which can get the statistics:

man 3 mq_getattr

   mq_getattr() returns an mq_attr structure  in  the  buffer  pointed  by
   attr.  This structure is defined as:

       struct mq_attr {
           long mq_flags;       /* Flags: 0 or O_NONBLOCK */
           long mq_maxmsg;      /* Max. # of messages on queue */
           long mq_msgsize;     /* Max. message size (bytes) */
           long mq_curmsgs;     /* # of messages currently in queue */
       };

Solution 2

If you're lucky enough to use HP-UX, the command pipcs (PDF) (sorry link broken, no current archive, see this man page on unix.com instead) performs the POSIX equivalent to the SysV IPC ipcs command. Sadly, it has never appeared for other OS's. The current (January 2021) util-linux package which provides ipcs barely mentions POSIX mqueues.

If you have mqueue mounted on /dev/mqueue, what is supported is reading metadata for an existing queue as a file:

user@linux $ sudo mount -t mqueue mqueue /dev/mqueue
user@linux $ mq_create -c /myQ
user@linux $ cat /dev/mqueue/myQ
QSIZE:0          NOTIFY:0     SIGNO:0     NOTIFY_PID:0 

The QSIZE is sadly only the size in bytes, not messages; on Linux there is (as yet, kernel 5.4) no message count field – unlike FreeBSD which provides a CURMSG value:

user@freebsd $ cat /mnt/mqueue/myQ
QSIZE:0          MAXMSG:32         CURMSG:0          MSGSIZE:1024   

So the answer should be "mount the mqueue FS and use cat", but it's not :(

(Mounting the mqueue FS is not a prerequisite for using the MQ API, it just lets you do some "everything is a file" stuff.)

There is good coverage of message queues in the book The Linux Programming Interface, including C source code for various CLI tools to create, inspect and use message queues. Happily chapter 52 of the book which covers this topic is currently available free to download. See pmsg_getattr.c.

The canonical reference code from Stephen's Unix Network Programming (Vol 2) 2nd Ed. (1999) is available here, it provides amongst other things mqgetattr which will do the job too (though you'll likely need to massage a couple of lines in the top level config.h, the #define's for various uint types conflict with contemporary Unix system headers).

There are also Ruby, Perl and TCL modules for this, the Ruby one comes with a posix-mq-rb CLI tool.

Solution 3

$ ipcs -q will provide message queue stats from the command line.

$ ipcs -m will provide shared memory stats from the command line.

$ ipcs will provide all ipc mechanism stats.

Share:
55,741

Related videos on Youtube

sujin
Author by

sujin

Linux Enthusiast.

Updated on September 18, 2022

Comments

  • sujin
    sujin almost 2 years

    We can check the details of system V message queue with the help of ipcscommand. Is there any command to check POSIX message queue in Linux?

  • sujin
    sujin about 11 years
    I using ubuntu system..
  • whoan
    whoan over 7 years
    ipcs is not for POSIX1 message queues
  • Yerke
    Yerke almost 6 years
    ipcs is used for System V message queues and not for POSIX message queues