msgopのヘルプ・マニュアル
日本語 英語
msgop --help
man msgop
MSGOP(2) Linux Programmer’s Manual MSGOP(2)
名前
msgrcv, msgsnd - メッセージ操作
書式
#include
#include
#include
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
int msgflg);
説明
シ ステムコール msgsnd() と msgrcv() はそれぞれ、メッセージ・キューへの
メッセージの送信と、メッセージの受信に使用される。呼び出し元プロセス は
、 メッセージを送信するためにはメッセージ・キューに対する書き込み許可を
、メッセージを受信するためには読み出し許可を持っていなければならない。
呼び出し元プロセスは以下に示す構造体を用意し、この構造体へのポインタ を
引き数として渡す。
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
mtext フィールドは配列 (または他の構造体) で、その大きさは非負の整数で
ある msgsz で指定される。長さ 0 のメッセージ (つまり mtext フィールドが
ないメッセージ) も認められている。 mtype フィールドは厳密に正の整数でな
ければならない。この値は、メッセージを受信するプロセスでメッセージを 選
択するために使用される (下記の msgrcv() の説明を参照のこと)。
msgsnd()
msgsnd() システムコールは msgp 引き数で指定されたメッセージのコピーを
msqid で指定された識別子を持つメッセージ・キューへ追加する。
キューに十分な空き容量がある場合、 msgsnd() は直ちに成功する。 (キュ ー
の容量は、メッセージ・キューのデータ構造体の msg_bytes フィールドで定義
される。キュー作成時にこのフィールドは MSGMNB に初期化されるが、この 制
限は msgctl(2) を使って変更できる。) キューに十分な空き容量がない場合、
デフォルトでは msgsnd() は空き容量ができるまで停 止 (block) す る 。
msgflg に IPC_NOWAIT が指定された場合は、エラー EAGAIN で失敗する。
停止している msgsnd() は以下の場合にも失敗する。
* キューが削除された。この場合、 errno は EIDRM に設定される。
* シ グナルが捕捉された。この場合、 errno は EINTR に設定される。 sig-
nal(7) 参照。 (msgsnd() は、たとえシグナル ハ ン ド ラ の 設 定 時 に
SA_RESTART を指定していたとしても、シグナルハンドラによって割り込まれ
た後で自動的に再スタートすることは決してない。)
正常に終了した場合、メッセージ・キューのデータ構造体は以下のように更 新
される:
msg_lspid には呼び出し元プロセスのプロセス ID が設定される。
msg_qnum は 1 増加する。
msg_stime には現在時刻が設定される。
msgrcv()
msgrcv() システムコールは msqid で指定されたキューからメッセージを削除
し、 msgp で指定されたバッファにそのメッセージを格納する。
msgsz 引き数には msgp 引き数で指定された構造体の mtext メンバーの最大の
バイト数を指定する。メッセージのテキストの長さが msgsz より大きい場合の
動作は、 msgflg に MSG_NOERROR が指定されているかどう か で 決 ま る 。
MSG_NOERROR が指定されていれば、メッセージのテキストは切り詰められる (
切り捨てられた部分は失われる)。 MSG_NOERROR が指定されていなければ、 メ
ッ セ ー ジはキューから削除されず、システムコールは -1 を返して失敗し、
errno に E2BIG が設定される。
msgtyp 引き数には要求するメッセージの型を指定する。型は以下のように指定
する:
* msgtyp が 0 ならば、キューの最初にあるメッセージが読み込まれる。
* msgtyp が 0 より大きい場合、 msgflg に MSG_EXCEPT が指定されていなけ
れば、 msgtyp 型のキューの最初のメッセージが読み込まれる。 MSG_EXCEPT
が 指定された場合は、 msgtyp 型以外のキューの最初のメッセージが読み込
まれる。
* msgtyp が 0 より小さければ、 msgtyp の絶対値以下で最も小さい型を持 つ
キューの最初のメッセージが読み込まれる。
msgflg 引き数には、以下のフラグを任意の数だけ (0個も可)、これらの OR で
指定する:
IPC_NOWAIT
キューに要求された型のメッセージがない場合には直ちに返る。システ
ムコールは失敗し、 errno には ENOMSG が設定される。
MSG_EXCEPT
0 より大きな msgtyp と一緒に使用して、 msgtyp 以外のキューの最初
のメッセージを読み込む。
MSG_NOERROR
msgsz バイトよりも長かった場合はメッセージのテキストを切り詰める
。
要 求された型のメッセージが存在せず、 msgflg に IPC_NOWAIT が指定されて
いなかった場合、呼び出し元プロセスは以下のいずれかの状況になるまで停 止
(block) される:
* 要求している型のメッセージがキューへ入れられた。
* メッセージ・キューがシステムから削除された。この場合、システムコール
は失敗し、 errno に EIDRM が設定される。
* 呼び出し元プロセスがシグナルを捕獲した。この場合、システムコールは 失
敗し、 errno に EINTR が設定される。 (msgrcv() は、たとえシグナルハン
ドラの設定時に SA_RESTART を指定していたとしても、シグナルハンドラ に
よって割り込まれた後で自動的に再スタートすることは決してない。)
実 行に成功した場合、メッセージ・キューのデータ構造体は以下のように更新
される:
msg_lrpid には呼び出し元プロセスのプロセス ID が設定される。
msg_qnum は 1 減算される。
msg_rtime には現在の時刻が設定される。
返り値
失敗した場合は、どちらの関数も -1 を返し、エラーを errno に表示する。成
功 した場合、 msgsnd() は 0 を返し、 msgrcv() は mtext 配列に実際にコピ
ーしたバイト数を返す。
エラー
msgsnd() が失敗した場合、 errno に以下の値のいずれかが設定される:
EACCES 呼び出し元プロセスにはメッセージ・キューに対する書き込み許可がな
く、 CAP_IPC_OWNER ケーパビリティもない。
EAGAIN msg_qbytes がキューの制限を超えていたため、メッセージを送ること
ができず、かつ msgflg に IPC_NOWAIT が指定されていた。
EFAULT msgp が指しているアドレスがアクセス可能でない。
EIDRM メッセージ・キューが削除された。
EINTR メッセージ・キューが要求した条件を満たすまで停止している時に、プ
ロセスがシグナルを捕獲した。
EINVAL msqid が不適切な値であるか、 mtype が正の値でないか、 msgsz が不
適切な値 (0 以下か、システムで決まる値 MSGMAX よりも大きい値) で
ある。
ENOMEM msgp が指すメッセージのコピーを作成するのに十分なメモリがシステ
ムに存在しない。
msgrcv() が失敗した場合には errno に以下の値のいずれかが設定される:
E2BIG メッセージのテキストの長さが msgsz よりも大き く 、 msgflg に
MSG_NOERROR が設定されていなかった。
EACCES 呼び出し元プロセスにはメッセージ・キューに対する読み込み許可がな
く、 CAP_IPC_OWNER ケーパビリティもない。
EAGAIN キューにはメッセージがなく、 msgflg に IPC_NOWAIT が指定された。
EFAULT msgp が指しているアドレスがアクセス可能でない。
EIDRM メッセージを受信するためにプロセスが停止している間に、メッセージ
・キューが削除された。
EINTR メッセージを受けるためにプロセスが停止している間に、プロセスがシ
グナルを捕獲した。 signal(7) 参照。
EINVAL msgqid が不正か、 msgsz が 0 より小さい。
ENOMSG msgflg に IPC_NOWAIT が設定されており、メッセージ・キューに要求
された型のメッセージが存在しなかった。
準拠
SVr4, POSIX.1-2001.
注意
msgp 引き数は、 libc4, libc5, glibc 2.0, glibc 2.1 では struct msgbuf *
と 宣言されている。glibc 2.2 以降では、 SUSv2 と SUSv3 の要求通り、void
* と宣言されている。
以下は msgsnd システムコールに影響するシステム制限である:
MSGMAX メッセージのテキストの最大サイズ: 8192 バイト (Linux では、こ の
制 限値は /proc/sys/kernel/msgmax 経由で読み出したり変更したりで
きる)。
MSGMNB バイト単位でのメッセージ・キューのデフォルトの最大サイズ : 16384
バ イト。 (Linux では、この制限値は /proc/sys/kernel/msgmnb 経由
で読み出したり変更したりできる)。スーパーユーザーは msgctl(2) シ
ステムコールでメッセージ・キューのサイズを MSGMNB よりも大きい値
に増やすことができる。
現在の実装では、システム全体のメッセージ・ヘッダーの最大数 (MSGTQL) と
、システム全体のメッセージ・プールの最大バイト数 (MSGPOOL) に関して実装
依存の制限はない。
関連項目
msgctl(2), msgget(2), capabilities(7), mq_overview(7), svipc(7)
Linux 2008-04-23 MSGOP(2)
MSGOP(2) Linux Programmer’s Manual MSGOP(2)
NAME
msgrcv, msgsnd - message operations
SYNOPSIS
#include
#include
#include
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
int msgflg);
DESCRIPTION
The msgsnd() and msgrcv() system calls are used, respectively, to send
messages to, and receive messages from, a message queue. The calling
process must have write permission on the message queue in order to
send a message, and read permission to receive a message.
The msgp argument is a pointer to caller-defined structure of the fol-
lowing general form:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
The mtext field is an array (or other structure) whose size is speci-
fied by msgsz, a non-negative integer value. Messages of zero length
(i.e., no mtext field) are permitted. The mtype field must have a
strictly positive integer value. This value can be used by the receiv-
ing process for message selection (see the description of msgrcv()
below).
msgsnd()
The msgsnd() system call appends a copy of the message pointed to by
msgp to the message queue whose identifier is specified by msqid.
If sufficient space is available in the queue, msgsnd() succeeds imme-
diately. (The queue capacity is defined by the msg_bytes field in the
associated data structure for the message queue. During queue creation
this field is initialized to MSGMNB bytes, but this limit can be modi-
fied using msgctl(2).) If insufficient space is available in the
queue, then the default behavior of msgsnd() is to block until space
becomes available. If IPC_NOWAIT is specified in msgflg, then the call
instead fails with the error EAGAIN.
A blocked msgsnd() call may also fail if:
* the queue is removed, in which case the system call fails with errno
set to EIDRM; or
* a signal is caught, in which case the system call fails with errno
set to EINTR;see signal(7). (msgsnd() is never automatically
restarted after being interrupted by a signal handler, regardless of
the setting of the SA_RESTART flag when establishing a signal han-
dler.)
Upon successful completion the message queue data structure is updated
as follows:
msg_lspid is set to the process ID of the calling process.
msg_qnum is incremented by 1.
msg_stime is set to the current time.
msgrcv()
The msgrcv() system call removes a message from the queue specified by
msqid and places it in the buffer pointed to by msgp.
The argument msgsz specifies the maximum size in bytes for the member
mtext of the structure pointed to by the msgp argument. If the message
text has length greater than msgsz, then the behavior depends on
whether MSG_NOERROR is specified in msgflg. If MSG_NOERROR is speci-
fied, then the message text will be truncated (and the truncated part
will be lost); if MSG_NOERROR is not specified, then the message isn’t
removed from the queue and the system call fails returning -1 with
errno set to E2BIG.
The argument msgtyp specifies the type of message requested as follows:
* If msgtyp is 0, then the first message in the queue is read.
* If msgtyp is greater than 0, then the first message in the queue of
type msgtyp is read, unless MSG_EXCEPT was specified in msgflg, in
which case the first message in the queue of type not equal to msgtyp
will be read.
* If msgtyp is less than 0, then the first message in the queue with
the lowest type less than or equal to the absolute value of msgtyp
will be read.
The msgflg argument is a bit mask constructed by ORing together zero or
more of the following flags:
IPC_NOWAIT
Return immediately if no message of the requested type is in the
queue. The system call fails with errno set to ENOMSG.
MSG_EXCEPT
Used with msgtyp greater than 0 to read the first message in the
queue with message type that differs from msgtyp.
MSG_NOERROR
To truncate the message text if longer than msgsz bytes.
If no message of the requested type is available and IPC_NOWAIT isn’t
specified in msgflg, the calling process is blocked until one of the
following conditions occurs:
* A message of the desired type is placed in the queue.
* The message queue is removed from the system. In this case the sys-
tem call fails with errno set to EIDRM.
* The calling process catches a signal. In this case the system call
fails with errno set to EINTR. (msgrcv() is never automatically
restarted after being interrupted by a signal handler, regardless of
the setting of the SA_RESTART flag when establishing a signal han-
dler.)
Upon successful completion the message queue data structure is updated
as follows:
msg_lrpid is set to the process ID of the calling process.
msg_qnum is decremented by 1.
msg_rtime is set to the current time.
RETURN VALUE
On failure both functions return -1 with errno indicating the error,
otherwise msgsnd() returns 0 and msgrcv() returns the number of bytes
actually copied into the mtext array.
ERRORS
When msgsnd() fails, errno will be set to one among the following val-
ues:
EACCES The calling process does not have write permission on the mes-
sage queue, and does not have the CAP_IPC_OWNER capability.
EAGAIN The message can’t be sent due to the msg_qbytes limit for the
queue and IPC_NOWAIT was specified in msgflg.
EFAULT The address pointed to by msgp isn’t accessible.
EIDRM The message queue was removed.
EINTR Sleeping on a full message queue condition, the process caught a
signal.
EINVAL Invalid msqid value, or non-positive mtype value, or invalid
msgsz value (less than 0 or greater than the system value MSG-
MAX).
ENOMEM The system does not have enough memory to make a copy of the
message pointed to by msgp.
When msgrcv() fails, errno will be set to one among the following val-
ues:
E2BIG The message text length is greater than msgsz and MSG_NOERROR
isn’t specified in msgflg.
EACCES The calling process does not have read permission on the message
queue, and does not have the CAP_IPC_OWNER capability.
EAGAIN No message was available in the queue and IPC_NOWAIT was speci-
fied in msgflg.
EFAULT The address pointed to by msgp isn’t accessible.
EIDRM While the process was sleeping to receive a message, the message
queue was removed.
EINTR While the process was sleeping to receive a message, the process
caught a signal; see signal(7).
EINVAL msgqid was invalid, or msgsz was less than 0.
ENOMSG IPC_NOWAIT was specified in msgflg and no message of the
requested type existed on the message queue.
CONFORMING TO
SVr4, POSIX.1-2001.
NOTES
The msgp argument is declared as struct msgbuf * with libc4, libc5,
glibc 2.0, glibc 2.1. It is declared as void * with glibc 2.2 and
later, as required by SUSv2 and SUSv3.
The following limits on message queue resources affect the msgsnd()
call:
MSGMAX Maximum size for a message text: 8192 bytes (on Linux, this
limit can be read and modified via /proc/sys/kernel/msgmax).
MSGMNB Default maximum size in bytes of a message queue: 16384 bytes
(on Linux, this limit can be read and modified via
/proc/sys/kernel/msgmnb). The superuser can increase the size
of a message queue beyond MSGMNB by a msgctl(2) system call.
The implementation has no intrinsic limits for the system wide maximum
number of message headers (MSGTQL) and for the system wide maximum size
in bytes of the message pool (MSGPOOL).
SEE ALSO
msgctl(2), msgget(2), capabilities(7), mq_overview(7), svipc(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 2008-04-23 MSGOP(2)