# mq_overview(7) - man - phpMan

[MQ_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/MQOVERVIEW/7/markdown)                        Linux Programmer's Manual                       [MQ_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/MQOVERVIEW/7/markdown)



## NAME
       mq_overview - overview of POSIX message queues

## DESCRIPTION
       POSIX  message  queues allow processes to exchange data in the form of messages.  This API is
       distinct from that provided by System V  message  queues  ([**msgget**(2)](https://www.chedong.com/phpMan.php/man/msgget/2/markdown),  [**msgsnd**(2)](https://www.chedong.com/phpMan.php/man/msgsnd/2/markdown),  [**msgrcv**(2)](https://www.chedong.com/phpMan.php/man/msgrcv/2/markdown),
       etc.), but provides similar functionality.

       Message queues are created and opened using **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown); this function returns a _message_ _queue_
       _descriptor_ (_mqd_t_), which is used to refer to the open message queue in  later  calls.   Each
       message  queue  is  identified  by  a  name of the form _/somename_; that is, a null-terminated
       string of up to **NAME**___**MAX** (i.e., 255) characters consisting of an initial slash,  followed  by
       one  or  more  characters,  none of which are slashes.  Two processes can operate on the same
       queue by passing the same name to **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown).

       Messages are transferred to and from a queue using  **mq**___**[send**(3)](https://www.chedong.com/phpMan.php/man/send/3/markdown)  and  **mq**___**[receive**(3)](https://www.chedong.com/phpMan.php/man/receive/3/markdown).   When  a
       process  has  finished using the queue, it closes it using **mq**___**[close**(3)](https://www.chedong.com/phpMan.php/man/close/3/markdown), and when the queue is
       no longer required, it can be deleted using **mq**___**[unlink**(3)](https://www.chedong.com/phpMan.php/man/unlink/3/markdown).  Queue attributes can be  retrieved
       and  (in  some  cases) modified using **mq**___**[getattr**(3)](https://www.chedong.com/phpMan.php/man/getattr/3/markdown) and **mq**___**[setattr**(3)](https://www.chedong.com/phpMan.php/man/setattr/3/markdown).  A process can request
       asynchronous notification of the arrival of a message  on  a  previously  empty  queue  using
       **mq**___**[notify**(3)](https://www.chedong.com/phpMan.php/man/notify/3/markdown).

       A message queue descriptor is a reference to an _open_ _message_ _queue_ _description_ (see [**open**(2)](https://www.chedong.com/phpMan.php/man/open/2/markdown)).
       After a [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown), a child inherits copies of its parent's message queue descriptors, and these
       descriptors  refer  to  the same open message queue descriptions as the corresponding message
       queue descriptors in the parent.  Corresponding message queue descriptors  in  the  two  pro‐
       cesses  share  the  flags (_mq_flags_) that are associated with the open message queue descrip‐
       tion.

       Each message has an associated _priority_, and messages are always delivered to  the  receiving
       process   highest   priority   first.    Message   priorities   range   from   0   (low)   to
       _sysconf(_SC_MQ_PRIO_MAX)_ _-_ _1_ (high).  On Linux, _sysconf(_SC_MQ_PRIO_MAX)_ returns  32768,  but
       POSIX.1  requires  only  that an implementation support at least priorities in the range 0 to
       31; some implementations provide only this range.

       The remainder of this section describes some specific details of the Linux implementation  of
       POSIX message queues.

### Library interfaces and system calls
       In most cases the **mq**___*****() library interfaces listed above are implemented on top of underlying
       system calls of the same name.  Deviations from this scheme are indicated  in  the  following
       table:

              **Library** **interface**    **System** **call**
              [mq_close(3)](https://www.chedong.com/phpMan.php/man/mqclose/3/markdown)          [close(2)](https://www.chedong.com/phpMan.php/man/close/2/markdown)
              [mq_getattr(3)](https://www.chedong.com/phpMan.php/man/mqgetattr/3/markdown)        [mq_getsetattr(2)](https://www.chedong.com/phpMan.php/man/mqgetsetattr/2/markdown)
              [mq_notify(3)](https://www.chedong.com/phpMan.php/man/mqnotify/3/markdown)         [mq_notify(2)](https://www.chedong.com/phpMan.php/man/mqnotify/2/markdown)
              [mq_open(3)](https://www.chedong.com/phpMan.php/man/mqopen/3/markdown)           [mq_open(2)](https://www.chedong.com/phpMan.php/man/mqopen/2/markdown)
              [mq_receive(3)](https://www.chedong.com/phpMan.php/man/mqreceive/3/markdown)        [mq_timedreceive(2)](https://www.chedong.com/phpMan.php/man/mqtimedreceive/2/markdown)
              [mq_send(3)](https://www.chedong.com/phpMan.php/man/mqsend/3/markdown)           [mq_timedsend(2)](https://www.chedong.com/phpMan.php/man/mqtimedsend/2/markdown)
              [mq_setattr(3)](https://www.chedong.com/phpMan.php/man/mqsetattr/3/markdown)        [mq_getsetattr(2)](https://www.chedong.com/phpMan.php/man/mqgetsetattr/2/markdown)
              [mq_timedreceive(3)](https://www.chedong.com/phpMan.php/man/mqtimedreceive/3/markdown)   [mq_timedreceive(2)](https://www.chedong.com/phpMan.php/man/mqtimedreceive/2/markdown)
              [mq_timedsend(3)](https://www.chedong.com/phpMan.php/man/mqtimedsend/3/markdown)      [mq_timedsend(2)](https://www.chedong.com/phpMan.php/man/mqtimedsend/2/markdown)
              [mq_unlink(3)](https://www.chedong.com/phpMan.php/man/mqunlink/3/markdown)         [mq_unlink(2)](https://www.chedong.com/phpMan.php/man/mqunlink/2/markdown)

### Versions
       POSIX message queues have been supported on Linux since kernel 2.6.6.  Glibc support has been
       provided since version 2.3.4.

### Kernel configuration
       Support for POSIX message queues is configurable via the **CONFIG**___**POSIX**___**MQUEUE** kernel  configu‐
       ration option.  This option is enabled by default.

### Persistence
       POSIX message queues have kernel persistence: if not removed by **mq**___**[unlink**(3)](https://www.chedong.com/phpMan.php/man/unlink/3/markdown), a message queue
       will exist until the system is shut down.

### Linking
       Programs using the POSIX message queue API must be compiled with _cc_ _-lrt_ to link against  the
       real-time library, _librt_.

### /proc interfaces
       The  following  interfaces can be used to limit the amount of kernel memory consumed by POSIX
       message queues and to set the default attributes for new message queues:

       _/proc/sys/fs/mqueue/msg_default_ (since Linux 3.5)
              This file defines the value used for a new queue's _mq_maxmsg_ setting when the queue is
              created  with a call to **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown) where _attr_ is specified as NULL.  The default value
              for this file is 10.  The minimum and maximum are as for  _/proc/sys/fs/mqueue/msg_max_.
              A  new queue's default _mq_maxmsg_ value will be the smaller of _msg_default_ and _msg_max_.
              Up until Linux 2.6.28, the default _mq_maxmsg_ was 10; from Linux 2.6.28 to  Linux  3.4,
              the default was the value defined for the _msg_max_ limit.

       _/proc/sys/fs/mqueue/msg_max_
              This  file  can be used to view and change the ceiling value for the maximum number of
              messages in a queue.  This value acts as a ceiling  on  the  _attr->mq_maxmsg_  argument
              given to **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown).  The default value for _msg_max_ is 10.  The minimum value is 1 (10
              in kernels before 2.6.28).  The upper limit is **HARD**___**MSGMAX**.  The _msg_max_ limit is  ig‐
              nored for privileged processes (**CAP**___**SYS**___**RESOURCE**), but the **HARD**___**MSGMAX** ceiling is nev‐
              ertheless imposed.

              The definition of **HARD**___**MSGMAX** has changed across kernel versions:

              *  Up to Linux 2.6.32: _131072_ _/_ _sizeof(void_ _*)_

              *  Linux 2.6.33 to 3.4: _(32768_ _*_ _sizeof(void_ _*)_ _/_ _4)_

              *  Since Linux 3.5: 65,536

       _/proc/sys/fs/mqueue/msgsize_default_ (since Linux 3.5)
              This file defines the value used for a new queue's _mq_msgsize_ setting when  the  queue
              is  created  with  a  call to **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown) where _attr_ is specified as NULL.  The default
              value  for  this  file  is  8192  (bytes).   The  minimum  and  maximum  are  as   for
              _/proc/sys/fs/mqueue/msgsize_max_.    If  _msgsize_default_  exceeds  _msgsize_max_,  a  new
              queue's default _mq_msgsize_ value is capped to the _msgsize_max_ limit.  Up  until  Linux
              2.6.28,  the  default _mq_msgsize_ was 8192; from Linux 2.6.28 to Linux 3.4, the default
              was the value defined for the _msgsize_max_ limit.

       _/proc/sys/fs/mqueue/msgsize_max_
              This file can be used to view and change the ceiling  on  the  maximum  message  size.
              This  value  acts  as  a ceiling on the _attr->mq_msgsize_ argument given to **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown).
              The default value for _msgsize_max_ is 8192 bytes.  The minimum value is  128  (8192  in
              kernels before 2.6.28).  The upper limit for _msgsize_max_ has varied across kernel ver‐
              sions:

              *  Before Linux 2.6.28, the upper limit is **INT**___**MAX**.

              *  From Linux 2.6.28 to 3.4, the limit is 1,048,576.

              *  Since Linux 3.5, the limit is 16,777,216 (**HARD**___**MSGSIZEMAX**).

              The _msgsize_max_ limit is ignored for privileged process (**CAP**___**SYS**___**RESOURCE**), but, since
              Linux 3.5, the **HARD**___**MSGSIZEMAX** ceiling is enforced for privileged processes.

       _/proc/sys/fs/mqueue/queues_max_
              This  file  can be used to view and change the system-wide limit on the number of mes‐
              sage queues that can be created.  The default value for _queues_max_ is 256.  No ceiling
              is imposed on the _queues_max_ limit; privileged processes (**CAP**___**SYS**___**RESOURCE**) can exceed
              the limit (but see BUGS).

### Resource limit
       The **RLIMIT**___**MSGQUEUE** resource limit, which places a limit on the amount of space that  can  be
       consumed  by all of the message queues belonging to a process's real user ID, is described in
       [**getrlimit**(2)](https://www.chedong.com/phpMan.php/man/getrlimit/2/markdown).

### Mounting the message queue filesystem
       On Linux, message queues are created in a virtual  filesystem.   (Other  implementations  may
       also  provide  such a feature, but the details are likely to differ.)  This filesystem can be
       mounted (by the superuser) using the following commands:

           # **mkdir** **/dev/mqueue**
           # **mount** **-t** **mqueue** **none** **/dev/mqueue**

       The sticky bit is automatically enabled on the mount directory.

       After the filesystem has been mounted, the message queues on the system can be viewed and ma‐
       nipulated using the commands usually used for files (e.g., [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) and [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown)).

       The  contents  of  each file in the directory consist of a single line containing information
       about the queue:

           $ **cat** **/dev/mqueue/mymq**
           QSIZE:129     NOTIFY:2    SIGNO:0    NOTIFY_PID:8260

       These fields are as follows:

       **QSIZE**  Number of bytes of data in all messages in the queue (but see BUGS).

       **NOTIFY**___**PID**
              If this is nonzero, then the process with this PID has used **mq**___**[notify**(3)](https://www.chedong.com/phpMan.php/man/notify/3/markdown)  to  register
              for asynchronous message notification, and the remaining fields describe how notifica‐
              tion occurs.

       **NOTIFY** Notification method: 0 is **SIGEV**___**SIGNAL**; 1 is **SIGEV**___**NONE**; and 2 is **SIGEV**___**THREAD**.

       **SIGNO**  Signal number to be used for **SIGEV**___**SIGNAL**.

### Linux implementation of message queue descriptors
       On Linux, a message queue descriptor is actually a file descriptor.  (POSIX does not  require
       such  an  implementation.)  This means that a message queue descriptor can be monitored using
       [**select**(2)](https://www.chedong.com/phpMan.php/man/select/2/markdown), [**poll**(2)](https://www.chedong.com/phpMan.php/man/poll/2/markdown), or [**epoll**(7)](https://www.chedong.com/phpMan.php/man/epoll/7/markdown).  This is not portable.

       The close-on-exec flag (see [**open**(2)](https://www.chedong.com/phpMan.php/man/open/2/markdown)) is automatically set on the file descriptor returned  by
       **mq**___**[open**(2)](https://www.chedong.com/phpMan.php/man/open/2/markdown).

### IPC namespaces
       For  a  discussion  of the interaction of POSIX message queue objects and IPC namespaces, see
       **ipc**___**[namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown).

## NOTES
       System V message queues ([**msgget**(2)](https://www.chedong.com/phpMan.php/man/msgget/2/markdown), [**msgsnd**(2)](https://www.chedong.com/phpMan.php/man/msgsnd/2/markdown), [**msgrcv**(2)](https://www.chedong.com/phpMan.php/man/msgrcv/2/markdown), etc.) are an older API for exchang‐
       ing  messages  between  processes.   POSIX message queues provide a better designed interface
       than System V message queues; on the other hand POSIX message queues are less  widely  avail‐
       able (especially on older systems) than System V message queues.

       Linux  does  not  currently (2.6.26) support the use of access control lists (ACLs) for POSIX
       message queues.

## BUGS
       In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024 (**HARD**___**QUEUESMAX**)  on  the
       value  to  which  the _queues_max_ limit could be raised, and the ceiling was enforced even for
       privileged processes.  This ceiling value was removed in Linux 3.14, and  patches  to  stable
       kernels 3.5.x to 3.13.x also removed the ceiling.

       As  originally  implemented  (and  documented), the QSIZE field displayed the total number of
       (user-supplied) bytes in all messages in the message queue.  Some changes in Linux 3.5  inad‐
       vertently  changed  the behavior, so that this field also included a count of kernel overhead
       bytes used to store the messages in the queue.  This behavioral regression was  rectified  in
       Linux  4.2  (and earlier stable kernel series), so that the count once more included just the
       bytes of user data in messages in the queue.

## EXAMPLES
       An example of the use of various message queue functions is shown in **mq**___**[notify**(3)](https://www.chedong.com/phpMan.php/man/notify/3/markdown).

## SEE ALSO
       [**getrlimit**(2)](https://www.chedong.com/phpMan.php/man/getrlimit/2/markdown), **mq**___**[getsetattr**(2)](https://www.chedong.com/phpMan.php/man/getsetattr/2/markdown), [**poll**(2)](https://www.chedong.com/phpMan.php/man/poll/2/markdown), [**select**(2)](https://www.chedong.com/phpMan.php/man/select/2/markdown), **mq**___**[close**(3)](https://www.chedong.com/phpMan.php/man/close/3/markdown), **mq**___**[getattr**(3)](https://www.chedong.com/phpMan.php/man/getattr/3/markdown), **mq**___**[notify**(3)](https://www.chedong.com/phpMan.php/man/notify/3/markdown),
       **mq**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown), **mq**___**[receive**(3)](https://www.chedong.com/phpMan.php/man/receive/3/markdown), **mq**___**[send**(3)](https://www.chedong.com/phpMan.php/man/send/3/markdown), **mq**___**[unlink**(3)](https://www.chedong.com/phpMan.php/man/unlink/3/markdown), [**epoll**(7)](https://www.chedong.com/phpMan.php/man/epoll/7/markdown), [**namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown)

## COLOPHON
       This  page  is  part  of  release  5.10 of the Linux _man-pages_ project.  A description of the
       project, information about reporting bugs, and the latest version of this page, can be  found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-06-09                               [MQ_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/MQOVERVIEW/7/markdown)
