{"id":4105,"date":"2022-12-20T17:39:24","date_gmt":"2022-12-20T20:39:24","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/mq_overview-man7\/"},"modified":"2022-12-20T17:39:24","modified_gmt":"2022-12-20T20:39:24","slug":"mq_overview-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/mq_overview-man7\/","title":{"rendered":"MQ_OVERVIEW (man7)"},"content":{"rendered":"<h1 align=\"center\">MQ_OVERVIEW<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">mq_overview \u2212 overview of POSIX message queues<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 (<b>msgget<\/b>(2), <b>msgsnd<\/b>(2), <b>msgrcv<\/b>(2), etc.), but provides similar functionality.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Message queues are created and opened using <b>mq_open<\/b>(3); this function returns a <i>message queue descriptor<\/i> (<i>mqd_t<\/i>), which is used to refer to the open message queue in later calls. Each message queue is identified by a name of the form <i>\/somename<\/i>; that is, a null-terminated string of up to <b>NAME_MAX<\/b> (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 <b>mq_open<\/b>(3).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Messages are transferred to and from a queue using <b>mq_send<\/b>(3) and <b>mq_receive<\/b>(3). When a process has finished using the queue, it closes it using <b>mq_close<\/b>(3), and when the queue is no longer required, it can be deleted using <b>mq_unlink<\/b>(3). Queue attributes can be retrieved and (in some cases) modified using <b>mq_getattr<\/b>(3) and <b>mq_setattr<\/b>(3). A process can request asynchronous notification of the arrival of a message on a previously empty queue using <b>mq_notify<\/b>(3).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A message queue descriptor is a reference to an <i>open message queue description<\/i> (see <b>open<\/b>(2)). After a <b>fork<\/b>(2), a child inherits copies of its parent\u2019s 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 processes share the flags (<i>mq_flags<\/i>) that are associated with the open message queue description.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Each message has an associated <i>priority<\/i>, and messages are always delivered to the receiving process highest priority first. Message priorities range from 0 (low) to <i>sysconf(_SC_MQ_PRIO_MAX)\u00a0&#8211;\u00a01<\/i> (high). On Linux, <i>sysconf(_SC_MQ_PRIO_MAX)<\/i> 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The remainder of this section describes some specific details of the Linux implementation of POSIX message queues.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Library interfaces and system calls<\/b> <br \/> In most cases the <b>mq_*<\/b>() 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:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"23%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-942571.png\" alt=\"Image grohtml-942571.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Versions<\/b> <br \/> POSIX message queues have been supported on Linux since kernel 2.6.6. Glibc support has been provided since version 2.3.4.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Kernel configuration<\/b> <br \/> Support for POSIX message queues is configurable via the <b>CONFIG_POSIX_MQUEUE<\/b> kernel configuration option. This option is enabled by default.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Persistence<\/b> <br \/> POSIX message queues have kernel persistence: if not removed by <b>mq_unlink<\/b>(3), a message queue will exist until the system is shut down.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Linking<\/b> <br \/> Programs using the POSIX message queue API must be compiled with <i>cc \u2212lrt<\/i> to link against the real-time library, <i>librt<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>\/proc interfaces<\/b> <br \/> 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: <i><br \/> \/proc\/sys\/fs\/mqueue\/msg_default<\/i> (since Linux 3.5)<\/p>\n<p style=\"margin-left:22%;\">This file defines the value used for a new queue\u2019s <i>mq_maxmsg<\/i> setting when the queue is created with a call to <b>mq_open<\/b>(3) where <i>attr<\/i> is specified as NULL. The default value for this file is 10. The minimum and maximum are as for <i>\/proc\/sys\/fs\/mqueue\/msg_max<\/i>. A new queue\u2019s default <i>mq_maxmsg<\/i> value will be the smaller of <i>msg_default<\/i> and <i>msg_max<\/i>. Up until Linux 2.6.28, the default <i>mq_maxmsg<\/i> was 10; from Linux 2.6.28 to Linux 3.4, the default was the value defined for the <i>msg_max<\/i> limit.<\/p>\n<p style=\"margin-left:11%;\"><i>\/proc\/sys\/fs\/mqueue\/msg_max<\/i><\/p>\n<p style=\"margin-left:22%;\">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 <i>attr\u2212>mq_maxmsg<\/i> argument given to <b>mq_open<\/b>(3). The default value for <i>msg_max<\/i> is 10. The minimum value is 1 (10 in kernels before 2.6.28). The upper limit is <b>HARD_MSGMAX<\/b>. The <i>msg_max<\/i> limit is ignored for privileged processes (<b>CAP_SYS_RESOURCE<\/b>), but the <b>HARD_MSGMAX<\/b> ceiling is nevertheless imposed.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">The definition of <b>HARD_MSGMAX<\/b> has changed across kernel versions:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>Up to Linux 2.6.32: <i>131072\u00a0\/\u00a0sizeof(void\u00a0*)<\/i><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>Linux 2.6.33 to 3.4: <i>(32768\u00a0*\u00a0sizeof(void\u00a0*) \/ 4)<\/i><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>Since Linux 3.5: 65,536<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>\/proc\/sys\/fs\/mqueue\/msgsize_default<\/i> (since Linux 3.5)<\/p>\n<p style=\"margin-left:22%;\">This file defines the value used for a new queue\u2019s <i>mq_msgsize<\/i> setting when the queue is created with a call to <b>mq_open<\/b>(3) where <i>attr<\/i> is specified as NULL. The default value for this file is 8192 (bytes). The minimum and maximum are as for <i>\/proc\/sys\/fs\/mqueue\/msgsize_max<\/i>. If <i>msgsize_default<\/i> exceeds <i>msgsize_max<\/i>, a new queue\u2019s default <i>mq_msgsize<\/i> value is capped to the <i>msgsize_max<\/i> limit. Up until Linux 2.6.28, the default <i>mq_msgsize<\/i> was 8192; from Linux 2.6.28 to Linux 3.4, the default was the value defined for the <i>msgsize_max<\/i> limit.<\/p>\n<p style=\"margin-left:11%;\"><i>\/proc\/sys\/fs\/mqueue\/msgsize_max<\/i><\/p>\n<p style=\"margin-left:22%;\">This file can be used to view and change the ceiling on the maximum message size. This value acts as a ceiling on the <i>attr\u2212>mq_msgsize<\/i> argument given to <b>mq_open<\/b>(3). The default value for <i>msgsize_max<\/i> is 8192 bytes. The minimum value is 128 (8192 in kernels before 2.6.28). The upper limit for <i>msgsize_max<\/i> has varied across kernel versions:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>Before Linux 2.6.28, the upper limit is <b>INT_MAX<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>From Linux 2.6.28 to 3.4, the limit is 1,048,576.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>Since Linux 3.5, the limit is 16,777,216 (<b>HARD_MSGSIZEMAX<\/b>).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:22%; margin-top: 1em\">The <i>msgsize_max<\/i> limit is ignored for privileged process (<b>CAP_SYS_RESOURCE<\/b>), but, since Linux 3.5, the <b>HARD_MSGSIZEMAX<\/b> ceiling is enforced for privileged processes.<\/p>\n<p style=\"margin-left:11%;\"><i>\/proc\/sys\/fs\/mqueue\/queues_max<\/i><\/p>\n<p style=\"margin-left:22%;\">This file can be used to view and change the system-wide limit on the number of message queues that can be created. The default value for <i>queues_max<\/i> is 256. No ceiling is imposed on the <i>queues_max<\/i> limit; privileged processes (<b>CAP_SYS_RESOURCE<\/b>) can exceed the limit (but see BUGS).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Resource limit<\/b> <br \/> The <b>RLIMIT_MSGQUEUE<\/b> 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\u2019s real user ID, is described in <b>getrlimit<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Mounting the message queue filesystem<\/b> <br \/> 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:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"># <b>mkdir \/dev\/mqueue<\/b> <br \/> # <b>mount \u2212t mqueue none \/dev\/mqueue<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The sticky bit is automatically enabled on the mount directory.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">After the filesystem has been mounted, the message queues on the system can be viewed and manipulated using the commands usually used for files (e.g., <b>ls<\/b>(1) and <b>rm<\/b>(1)).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The contents of each file in the directory consist of a single line containing information about the queue:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>cat \/dev\/mqueue\/mymq<\/b> <br \/> QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">These fields are as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>QSIZE<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>Number of bytes of data in all messages in the queue (but see BUGS).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>NOTIFY_PID<\/b><\/p>\n<p style=\"margin-left:22%;\">If this is nonzero, then the process with this PID has used <b>mq_notify<\/b>(3) to register for asynchronous message notification, and the remaining fields describe how notification occurs.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>NOTIFY<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Notification method: 0 is <b>SIGEV_SIGNAL<\/b>; 1 is <b>SIGEV_NONE<\/b>; and 2 is <b>SIGEV_THREAD<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>SIGNO<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Signal number to be used for <b>SIGEV_SIGNAL<\/b>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Linux implementation of message queue descriptors<\/b> <br \/> 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 <b>select<\/b>(2), <b>poll<\/b>(2), or <b>epoll<\/b>(7). This is not portable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The close-on-exec flag (see <b>open<\/b>(2)) is automatically set on the file descriptor returned by <b>mq_open<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>IPC namespaces<\/b> <br \/> For a discussion of the interaction of POSIX message queue objects and IPC namespaces, see <b>ipc_namespaces<\/b>(7).<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">System V message queues (<b>msgget<\/b>(2), <b>msgsnd<\/b>(2), <b>msgrcv<\/b>(2), etc.) are an older API for exchanging 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 available (especially on older systems) than System V message queues.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Linux does not currently (2.6.26) support the use of access control lists (ACLs) for POSIX message queues.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024 (<b>HARD_QUEUESMAX<\/b>) on the value to which the <i>queues_max<\/i> 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 inadvertently 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.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">An example of the use of various message queue functions is shown in <b>mq_notify<\/b>(3).<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getrlimit<\/b>(2), <b>mq_getsetattr<\/b>(2), <b>poll<\/b>(2), <b>select<\/b>(2), <b>mq_close<\/b>(3), <b>mq_getattr<\/b>(3), <b>mq_notify<\/b>(3), <b>mq_open<\/b>(3), <b>mq_receive<\/b>(3), <b>mq_send<\/b>(3), <b>mq_unlink<\/b>(3), <b>epoll<\/b>(7), <b>namespaces<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> 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\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  mq_overview \u2212 overview of POSIX message queues <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,1184],"class_list":["post-4105","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-mq_overview"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4105"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4105\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}