MQ_OVERVIEW(7) Linux Programmer’s Manual MQ_OVERVIEW(7) 名前 mq_overview - POSIX メッセージキューの概要 説明 POSIX メッセージキューを使用すると、プロセス間でメッセージの形でのデー タのやり取りを行うことができる。この API は System V メッセージキューの API (msgget(2), msgsnd(2), msgrcv(2) など) とは異なるものだが、同様の機 能を提供する。 メッセージキューの作成とオープンは mq_open(3) を使って行う。この関数は メ ッセージキュー記述子 (message queue descriptor) (mqd_t) を返す。これ 以降のコールでは、オープンされたメッセージキューは メッセージキュー記述 子を使って参照される。各メッセージキューは /somename の形の名前で区別す ることができる。その名前は、最大で NAME_MAX (すなわち 255) 文字の NULL 終端された文字列で、スラッシュで始まり、スラッシュ以外の文字が 1 文字以 上続く形式である。 mq_open(3) に同じ名前を渡すことで、2つのプロセスで同 一のキューを操作することができる。 メッセージのキューへの送受信は mq_send(3) と mq_receive(3) を使って行う 。プロセスがキューの使用を終えるときには、 mq_close(3) を使ってキューを ク ローズする。キューがもはや不要となった場合には、 mq_unlink(3) を使っ てキューを削除できる。キューの属性は mq_getattr(3) で取得でき、 (制限は あ るが) mq_setattr(3) で変更できる。 mq_notify(3) を使うことで、空のキ ューへのメッセージ到着を非同期で通知するように要求することもできる。 メッセージキュー記述子は オープンメッセージキュー 記 述 (open message queue description) への参照である (open(2) も参照)。 fork(2) 実行後は、 子プロセスは親プロセスのメッセージキュー記述子のコピーを継承する。こ れ ら の記述子は、親プロセスの対応する記述子と同じオープンメッセージキュー 記述を参照している。親プロセスと子プロセスの対応する記述子は、 フ ラ グ (mq_flags) を共有する。なぜなら、フラグはオープンメッセージキュー記述に 関連付けられているからである。 各メッセージにはそれぞれ 優先度 (priority) があり、メッセージの受信プロ セ スへの配送は常に優先度の高いメッセージから順に行われる。メッセージの 優先度は 0 (低優先) から sysconf(_SC_MQ_PRIO_MAX) - 1 (高優先) の値を持 つ 。 Linux で は 、 sysconf(_SC_MQ_PRIO_MAX) は 32768 を返すが、 POSIX.1-2001 で要求されているのは 0 から 31 までの優先度を実装するこ と だけであり、実装によってはこの範囲の優先度しか対応していない。 この節の残りでは、POSIX メッセージキューの Linux の実装の詳細について説 明する。 ライブラリインタフェースとシステムコール ほとんどの場合、上記の mq_*() ライブラリインタフェースは、同じ名前の 下 位 層のシステムコールを使って実装されている。この枠組みにあてはまらない ものを以下の表に示す。 Library interface System call mq_close(3) close(2) mq_getattr(3) mq_getsetattr(2) mq_notify(3) mq_notify(2) mq_open(3) mq_open(2) mq_receive(3) mq_timedreceive(2) mq_send(3) mq_timedsend(2) mq_setattr(3) mq_getsetattr(2) mq_timedreceive(3) mq_timedreceive(2) mq_timedsend(3) mq_timedsend(2) mq_unlink(3) mq_unlink(2) バージョン Linux では POSIX メッセージキューはカーネル 2.6.6 以降でサポートされ て いる。 glibc ではバージョン 2.3.4 以降でサポートされている。 カーネルの設定 POSIX メッセージキューのサポートは、カーネルの設定 (configuration) オプ ション CONFIG_POSIX_MQUEUE で設定可能である。このオプションはデフォルト では有効である。 持続性 POSIX メッセージキューはカーネル内で保持される。 mq_unlink(3) で削除さ れなければ、メッセージキューはシステムがシャットダウンされるまで存在 し 続ける。 リンク POSIX メッセージキュー API を使用したプログラムは cc -lrt でコンパイル し、リアルタイムライブラリ librt とリンクしなければならない。 /proc インタフェース 以下のインタフェースを使って、POSIX メッセージキューが消費するカーネ ル メモリの量を制限することができる。 /proc/sys/fs/mqueue/msg_max このファイルを使って、一つのキューに入れられるメッセージの最大数 の上限値を参照したり変更したりできる。この値は、 mq_open(3) に渡 す attr->mq_maxmsg 引き数に対する上限値として機能する。 msg_max のデフォルト値および最小値は 10 である; 上限は「埋め込みの固定値 」 (HARD_MAX) で (131072 / sizeof(void *)) (Linux/86 では 32768) である。この上限は特権プロセス (CAP_SYS_RESOURCE) では無視される が、埋め込みの固定値による上限はどんな場合にでも適用される。 /proc/sys/fs/mqueue/msgsize_max このファイルを使って、メッセージの最大サイズの上限値を参照したり 変 更 し た り で き る 。 こ の 値 は 、 mq_open(3) に渡す attr->mq_msgsize 引き数に 対する上限値として機能する。 msgsize_max のデフォル ト 値 および最小値は 8192 バイトである; 上限は INT_MAX (Linux/86 で は 2147483647) で あ る 。 こ の 上 限 は 特 権 プ ロ セ ス (CAP_SYS_RESOURCE) で は無視される。 /proc/sys/fs/mqueue/queues_max このファイルを使って、作成することができるメッセージキューの数に 対するシステム全体での制限を参照したり変更したりできる。一度この 上限に達すると、新しいメッセージキューを作成できるのは特権プロセ ス (CAP_SYS_RESOURCE) だ けとなる 。 queues_max のデフォ ル ト 値 は 256 で あ り 、 0 から INT_MAX の範囲の任意の値に変更することができ る。 リソース制限 リソース上限 RLIMIT_MSGQUEUE は、プロセスの実 UID に対応する全メッセ ー ジ キ ューが消費するメモリ空間の量に対して上限を設定する。 getrlimit(2) を参照。 メッセージキュー・ファイルシステムのマウント Linux では、メッセージキューは仮想ファイルシステム内に作成される (他 の 実装でも同様の機能が提供されているものもあるが、詳細は違っているだろう) 。以下のコマンドを使うことで、このファイルシステムをマウントできる: $ mkdir /dev/mqueue $ mount -t mqueue none /dev/mqueue マウントしたディレクトリのスティッキービット (sticky bit) は自動的に オ ンとなる。 メ ッセージキュー・ファイルシステムのマウント後は、ファイルに対して通常 使うコマンド (例えば ls(1) や rm(1)) を使って、システム上のメッセージキ ューを表示したり操作したりできる。 デ ィレクトリ内の各ファイルの内容は 1行であり、キューに関する情報が表示 される。 $ ls /dev/mqueue/mymq QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260 $ mount -t mqueue none /dev/mqueue 各フィールドの詳細は以下の通りである: QSIZE キューに入っている全メッセージの合計バイト数。 NOTIFY_PID この値が 0 以外の場合、この 値 の PID を 持 つ プ ロ セ ス が mq_notify(3) を使って、非同期のメッセージ通知を行うように設定し たことを示す。どのように通知が行われるかは、以下のフィールドによ り決定される。 NOTIFY 通知方法: 0 は SIGEV_SIGNAL; 1 は SIGEV_NONE; 2 は SIGEV_THREAD SIGNO SIGEV_SIGNAL に使用されるシグナル番号。 メッセージキュー記述子のポーリング Linux では、メッセージキュー記述子は実際はファイル記述子 (file descrip- tor) であり、 select(2), poll(2), epoll(7) を使って監視することができる 。この機能の移植性はない。 準拠 POSIX.1-2001. 注意 System V メッセージキュー (msgget(2), msgsnd(2), msgrcv(2) など) はプロ セス間でメッセージをやり取りするための古い API である。 POSIX メッセ ー ジ キューは System V メッセージキューよりもうまく設計されたインタフェー スを提供している。一方で、POSIX メッセージキューは System V メッセー ジ キ ューと比べると利用できるシステムが少ない (特に、古いシステムでは少な い)。 現在のことろ (バージョン 2.6.26 時点)、 Linux は POSIX メッセージキュー に対するアクセス制御リスト (ACL) に対応していない。 例 各 種のメッセージキュー関数を使用した例が mq_notify(3) に記載されている 。 関連項目 getrlimit(2), mq_getsetattr(2), poll(2), select(2), mq_close(3), mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3), mq_unlink(3), epoll(7) Linux 2009-02-25 MQ_OVERVIEW(7)
MQ_OVERVIEW(7) Linux Programmer’s Manual MQ_OVERVIEW(7) 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), msgsnd(2), msgrcv(2), etc.), but provides similar functionality. Message queues are created and opened using mq_open(3); 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 identi- fied 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). Messages are transferred to and from a queue using mq_send(3) and mq_receive(3). When a process has finished using the queue, it closes it using mq_close(3), and when the queue is no longer required, it can be deleted using mq_unlink(3). Queue attributes can be retrieved and (in some cases) modified using mq_getattr(3) and mq_setattr(3). A pro- cess can request asynchronous notification of the arrival of a message on a previously empty queue using mq_notify(3). A message queue descriptor is a reference to an open message queue description (cf. open(2)). After a fork(2), 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 descriptors in the parent. Corresponding descriptors in the two pro- cesses share the flags (mq_flags) that are associated with the open message queue description. Each message has an associated priority, and messages are always deliv- ered to the receiving process highest priority first. Message priori- ties range from 0 (low) to sysconf(_SC_MQ_PRIO_MAX) - 1 (high). On Linux, sysconf(_SC_MQ_PRIO_MAX) returns 32768, but POSIX.1-2001 only requires an implementation to support priorities in the range 0 to 31; some implementations only provide 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 imple- mented 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) close(2) mq_getattr(3) mq_getsetattr(2) mq_notify(3) mq_notify(2) mq_open(3) mq_open(2) mq_receive(3) mq_timedreceive(2) mq_send(3) mq_timedsend(2) mq_setattr(3) mq_getsetattr(2) mq_timedreceive(3) mq_timedreceive(2) mq_timedsend(3) mq_timedsend(2) mq_unlink(3) mq_unlink(2) 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 CON- FIG_POSIX_MQUEUE kernel configuration option. This option is enabled by default. Persistence POSIX message queues have kernel persistence: if not removed by mq_unlink(3), 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 mem- ory consumed by POSIX message queues: /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). The default and minimum value for msg_max is 10; the upper limit is HARD_MAX: (131072 / sizeof(void *)) (32768 on Linux/86). This limit is ignored for privileged processes (CAP_SYS_RESOURCE), but the HARD_MAX ceiling is nevertheless imposed. /proc/sys/fs/mqueue/msgsize_max This file can be used to view and change the ceiling on the max- imum message size. This value acts as a ceiling on the attr->mq_msgsize argument given to mq_open(3). The default and minimum value for msgsize_max is 8192 bytes; the upper limit is INT_MAX (2147483647 on Linux/86). This limit is ignored for privileged processes (CAP_SYS_RESOURCE). /proc/sys/fs/mqueue/queues_max This file can be used to view and change the system-wide limit on the number of message queues that can be created. Only priv- ileged processes (CAP_SYS_RESOURCE) can create new message queues once this limit has been reached. The default value for queues_max is 256; it can be changed to any value in the range 0 to INT_MAX. 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). Mounting the message queue file system On Linux, message queues are created in a virtual file system. (Other implementations may also provide such a feature, but the details are likely to differ.) This file system 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 file system has been mounted, the message queues on the sys- tem can be viewed and manipulated using the commands usually used for files (e.g., ls(1) and rm(1)). 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. NOTIFY_PID If this is non-zero, then the process with this PID has used mq_notify(3) to register for asynchronous message notification, and the remaining fields describe how notification 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. Polling message queue descriptors On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable. CONFORMING TO POSIX.1-2001. NOTES System V message queues (msgget(2), msgsnd(2), msgrcv(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 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. EXAMPLE An example of the use of various message queue functions is shown in mq_notify(3). SEE ALSO getrlimit(2), mq_getsetattr(2), poll(2), select(2), mq_close(3), mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3), mq_unlink(3), epoll(7) COLOPHON This page is part of release 3.22 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-07-25 MQ_OVERVIEW(7)
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa