📄 syscalls_76.html
字号:
<!-- This HTML file has been created by texi2html 1.29
from syscalls.texi on 4 June 1994 -->
<TITLE>Syscall specifications of Linux - msgrcv</TITLE>
<P>Go to the <A HREF="syscalls_75.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_75.html">previous</A>, <A HREF="syscalls_77.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_77.html">next</A> section.<P>
<H2><A NAME="SEC76" HREF="syscalls_toc.html#SEC76" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_toc.html#SEC76">msgrcv and msgsnd</A></H2>
<P>
<H3>SYNOPSIS</H3>
<P>
<CODE>int msgsnd(int <VAR>msqid</VAR>, struct msgbuf *<VAR>msgp</VAR>, int <VAR>msgsz</VAR>,
int <VAR>msgflg</VAR>);</CODE>
<P>
<CODE>int msgrcv(int <VAR>msqid</VAR>, struct msgbuf *<VAR>msgp</VAR>, int <VAR>msgsz</VAR>,
long <VAR>msgtyp</VAR>, int <VAR>msgflg</VAR>);</CODE>
<P>
<H3>PARAMETERS</H3>
<P>
<VAR>msqid</VAR>: [in] the message queue.
<P>
<VAR>msgp</VAR>: for <VAR>msgsnd</VAR>, [in] points to the message to send. For
<VAR>msgrcv</VAR>, [out] points to the buffer where to put the message.
<P>
<VAR>msgsz</VAR>: [in] size of the <VAR>mtext</VAR> part of the buffer. The
maximum possible size is <CODE>MSGMAX</CODE> and is currently 4080 bytes.
<P>
<VAR>msgflg</VAR>: [in] flags (see description).
<P>
<VAR>msgtyp</VAR>: [in] the type of message to receive.
<P>
<H3>DESCRIPTION</H3>
<P>
<VAR>msgp</VAR> must point to a buffer having the following structure:
<P>
<PRE>
struct msgbuf {
long mtype; /* message type, must > 0 */
char mtext[1]; /* message data */
}
</PRE>
<P>
The calling process must have read permission on the queue to call
<CODE>msgrcv</CODE> and write permission on the queue to call <CODE>msgsnd</CODE>.
<P>
<CODE>msgsnd</CODE> tries to enqueue the message. If <VAR>msglfg</VAR> is set to
<CODE>IPC_NOWAIT</CODE> and the queue is full, the call fails. Otherwise the
call blocks. If the send succeed, the message queue structure is updated
as follow:
<P>
<UL>
<LI><CODE>msg_lspid</CODE> is set to the pid of the calling process.
<LI><CODE>msg_qnum</CODE> is incremented by 1.
<LI><CODE>msg_stime</CODE> is set to the current time.
</UL>
<P>
<CODE>msgrcv</CODE> dequeues a message from the message queue. If the message
to be dequeued has a length greater than <VAR>msgsz</VAR>, then the call
fails. If the length is greater but the <CODE>MSG_NOERROR</CODE> flag is
specified, the message gets truncated (and the truncated information is
lost forever). The message to be dequeued can be choosed by the
following values of <VAR>msgtyp</VAR>:
<P>
<UL>
<LI><VAR>msgtyp</VAR> equals 0: in this case the 1st message on the queue
is dequeued.
<P>
<LI><VAR>msgtyp</VAR> is greater than 0: if the flag <CODE>MSG_EXCEPT</CODE> is
not specified, the first message of type <VAR>msgtyp</VAR> on the queue will
be dequeued, otherwise the first message <EM>not</EM> of type <VAR>msgtyp</VAR>
on the queue will be dequeued.
<P>
<LI><VAR>msgtyp</VAR> is less than 0: the first message of type in the range
[1,-<VAR>msgtyp</VAR>] will be dequeued.
</UL>
<P>
If the flag <CODE>IPC_NOWAIT</CODE> is specified and there is no message of
the specified type on the message queue, the call will fail, otherwise
the call will block. On success, the queue data structure is updated as
follow:
<P>
<UL>
<LI><CODE>msg_lrpid</CODE> is set to the pid of the calling process.
<LI><CODE>msg_qnum</CODE> is decremented by 1.
<LI><CODE>msg_rtime</CODE> is set to the current time.
</UL>
<P>
<H3>RETURN VALUE</H3>
<P>
On success <CODE>msgsnd</CODE> returns zero and <VAR>msgrcv</VAR> returns the
number of bytes copied in the <CODE>mtext</CODE> array. On error -1 is
returned and <CODE>errno</CODE> is set to one of the following values:
<P>
for <VAR>msgsnd</VAR>:
<P>
<UL>
<LI><CODE>EAGAIN</CODE>: <CODE>IPC_NOWAIT</CODE> was specified and the queue is
full.
<LI><CODE>EACCESS</CODE>: the calling task does not have write permission on
the queue.
<LI><CODE>EIDRM</CODE>: the queue has been removed.
<LI><CODE>EINVAL</CODE>: invalid <VAR>msqid</VAR>, <VAR>msgsz</VAR> or <VAR>mtype</VAR> value.
<LI><CODE>EFAULT</CODE>, <CODE>EINTR</CODE> and <CODE>ENOMEM</CODE>.
</UL>
<P>
for <VAR>msgrcv</VAR>:
<P>
<UL>
<LI><CODE>E2BIG</CODE>: <CODE>MSG_NOERROR</CODE> is not specified and the message
that should be dequeued is too bigger than <VAR>msgsz</VAR>.
<LI><CODE>EACCESS</CODE>: the calling task does not have read permission on
the queue.
<LI><CODE>EIDRM</CODE>: the queue has been removed.
<LI><CODE>EINVAL</CODE>: invalid <VAR>msqid</VAR> or <VAR>msgsz</VAR> value.
<LI><CODE>ENOMSG</CODE>: <CODE>IPC_NOWAIT</CODE> was specified no message of the
specified type is on the queue.
<LI><CODE>EFAULT</CODE>, <CODE>EINTR</CODE>
</UL>
<P>
<P>Go to the <A HREF="syscalls_75.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_75.html">previous</A>, <A HREF="syscalls_77.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_77.html">next</A> section.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -