976 lines
23 KiB
HTML
976 lines
23 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of MSGOP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>MSGOP</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
msgrcv, msgsnd - System V message queue operations
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/msg.h">sys/msg.h</A>></B>
|
|
|
|
<B>int msgsnd(int </B><I>msqid</I><B>, const void *</B><I>msgp</I><B>, size_t </B><I>msgsz</I><B>, int </B><I>msgflg</I><B>);</B>
|
|
|
|
<B>ssize_t msgrcv(int </B><I>msqid</I><B>, void *</B><I>msgp</I><B>, size_t </B><I>msgsz</I><B>, long </B><I>msgtyp</I><B>,</B>
|
|
<B> int </B><I>msgflg</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>msgsnd</B>()
|
|
|
|
and
|
|
<B>msgrcv</B>()
|
|
|
|
system calls are used to send messages to,
|
|
and receive messages from, a System V 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.
|
|
<P>
|
|
|
|
The
|
|
<I>msgp</I>
|
|
|
|
argument is a pointer to a caller-defined structure
|
|
of the following general form:
|
|
<P>
|
|
|
|
|
|
|
|
struct msgbuf {
|
|
<BR> long mtype; /* message type, must be > 0 */
|
|
<BR> char mtext[1]; /* message data */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>mtext</I>
|
|
|
|
field is an array (or other structure) whose size is specified by
|
|
<I>msgsz</I>,
|
|
|
|
a nonnegative integer value.
|
|
Messages of zero length (i.e., no
|
|
<I>mtext</I>
|
|
|
|
field) are permitted.
|
|
The
|
|
<I>mtype</I>
|
|
|
|
field must have a strictly positive integer value.
|
|
This value can be
|
|
used by the receiving process for message selection
|
|
(see the description of
|
|
<B>msgrcv</B>()
|
|
|
|
below).
|
|
<A NAME="lbAE"> </A>
|
|
<H3>msgsnd()</H3>
|
|
|
|
The
|
|
<B>msgsnd</B>()
|
|
|
|
system call appends a copy of the message pointed to by
|
|
<I>msgp</I>
|
|
|
|
to the message queue whose identifier is specified
|
|
by
|
|
<I>msqid</I>.
|
|
|
|
<P>
|
|
|
|
If sufficient space is available in the queue,
|
|
<B>msgsnd</B>()
|
|
|
|
succeeds immediately.
|
|
The queue capacity is governed by the
|
|
<I>msg_qbytes</I>
|
|
|
|
field in the associated data structure for the message queue.
|
|
During queue creation this field is initialized to
|
|
<B>MSGMNB</B>
|
|
|
|
bytes, but this limit can be modified using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+msgctl">msgctl</A></B>(2).
|
|
|
|
A message queue is considered to be full if either of the following
|
|
conditions is true:
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
Adding a new message to the queue would cause the total number of bytes
|
|
in the queue to exceed the queue's maximum size (the
|
|
<I>msg_qbytes</I>
|
|
|
|
field).
|
|
<DT id="2">*<DD>
|
|
Adding another message to the queue would cause the total number of messages
|
|
in the queue to exceed the queue's maximum size (the
|
|
<I>msg_qbytes</I>
|
|
|
|
field).
|
|
This check is necessary to prevent an unlimited number of zero-length
|
|
messages being placed on the queue.
|
|
Although such messages contain no data,
|
|
they nevertheless consume (locked) kernel memory.
|
|
</DL>
|
|
<P>
|
|
|
|
If insufficient space is available in the queue, then the default
|
|
behavior of
|
|
<B>msgsnd</B>()
|
|
|
|
is to block until space becomes available.
|
|
If
|
|
<B>IPC_NOWAIT</B>
|
|
|
|
is specified in
|
|
<I>msgflg</I>,
|
|
|
|
then the call instead fails with the error
|
|
<B>EAGAIN</B>.
|
|
|
|
<P>
|
|
|
|
A blocked
|
|
<B>msgsnd</B>()
|
|
|
|
call may also fail if:
|
|
<DL COMPACT>
|
|
<DT id="3">*<DD>
|
|
the queue is removed,
|
|
in which case the system call fails with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>EIDRM</B>;
|
|
|
|
or
|
|
<DT id="4">*<DD>
|
|
a signal is caught, in which case the system call fails
|
|
with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>EINTR</B>;<B>see</B>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
(<B>msgsnd</B>()
|
|
|
|
is never automatically restarted after being interrupted by a
|
|
signal handler, regardless of the setting of the
|
|
<B>SA_RESTART</B>
|
|
|
|
flag when establishing a signal handler.)
|
|
</DL>
|
|
<P>
|
|
|
|
Upon successful completion the message queue data structure is updated
|
|
as follows:
|
|
<DL COMPACT>
|
|
<DT id="5"><DD>
|
|
<I>msg_lspid</I>
|
|
|
|
is set to the process ID of the calling process.
|
|
<DT id="6"><DD>
|
|
<I>msg_qnum</I>
|
|
|
|
is incremented by 1.
|
|
<DT id="7"><DD>
|
|
<I>msg_stime</I>
|
|
|
|
is set to the current time.
|
|
</DL>
|
|
<A NAME="lbAF"> </A>
|
|
<H3>msgrcv()</H3>
|
|
|
|
The
|
|
<B>msgrcv</B>()
|
|
|
|
system call removes a message from the queue specified by
|
|
<I>msqid</I>
|
|
|
|
and places it in the buffer
|
|
pointed to by
|
|
<I>msgp</I>.
|
|
|
|
<P>
|
|
|
|
The argument
|
|
<I>msgsz</I>
|
|
|
|
specifies the maximum size in bytes for the member
|
|
<I>mtext</I>
|
|
|
|
of the structure pointed to by the
|
|
<I>msgp</I>
|
|
|
|
argument.
|
|
If the message text has length greater than
|
|
<I>msgsz</I>,
|
|
|
|
then the behavior depends on whether
|
|
<B>MSG_NOERROR</B>
|
|
|
|
is specified in
|
|
<I>msgflg</I>.
|
|
|
|
If
|
|
<B>MSG_NOERROR</B>
|
|
|
|
is specified, then
|
|
the message text will be truncated (and the truncated part will be
|
|
lost); if
|
|
<B>MSG_NOERROR</B>
|
|
|
|
is not specified, then
|
|
the message isn't removed from the queue and
|
|
the system call fails returning -1 with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>E2BIG</B>.
|
|
|
|
<P>
|
|
|
|
Unless
|
|
<B>MSG_COPY</B>
|
|
|
|
is specified in
|
|
<I>msgflg</I>
|
|
|
|
(see below),
|
|
the
|
|
<I>msgtyp</I>
|
|
|
|
argument specifies the type of message requested, as follows:
|
|
<DL COMPACT>
|
|
<DT id="8">*<DD>
|
|
If
|
|
<I>msgtyp</I>
|
|
|
|
is 0,
|
|
then the first message in the queue is read.
|
|
<DT id="9">*<DD>
|
|
If
|
|
<I>msgtyp</I>
|
|
|
|
is greater than 0,
|
|
then the first message in the queue of type
|
|
<I>msgtyp</I>
|
|
|
|
is read, unless
|
|
<B>MSG_EXCEPT</B>
|
|
|
|
was specified in
|
|
<I>msgflg</I>,
|
|
|
|
in which case
|
|
the first message in the queue of type not equal to
|
|
<I>msgtyp</I>
|
|
|
|
will be read.
|
|
<DT id="10">*<DD>
|
|
If
|
|
<I>msgtyp</I>
|
|
|
|
is less than 0,
|
|
then the first message in the queue with the lowest type less than or
|
|
equal to the absolute value of
|
|
<I>msgtyp</I>
|
|
|
|
will be read.
|
|
</DL>
|
|
<P>
|
|
|
|
The
|
|
<I>msgflg</I>
|
|
|
|
argument is a bit mask constructed by ORing together zero or more
|
|
of the following flags:
|
|
<DL COMPACT>
|
|
<DT id="11"><B>IPC_NOWAIT</B>
|
|
|
|
<DD>
|
|
Return immediately if no message of the requested type is in the queue.
|
|
The system call fails with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>ENOMSG</B>.
|
|
|
|
<DT id="12"><B>MSG_COPY</B> (since Linux 3.8)
|
|
|
|
<DD>
|
|
|
|
Nondestructively fetch a copy of the message at the ordinal position
|
|
in the queue specified by
|
|
<I>msgtyp</I>
|
|
|
|
(messages are considered to be numbered starting at 0).
|
|
<DT id="13"><DD>
|
|
This flag must be specified in conjunction with
|
|
<B>IPC_NOWAIT</B>,
|
|
|
|
with the result that, if there is no message available at the given position,
|
|
the call fails immediately with the error
|
|
<B>ENOMSG</B>.
|
|
|
|
Because they alter the meaning of
|
|
<I>msgtyp</I>
|
|
|
|
in orthogonal ways,
|
|
<B>MSG_COPY</B>
|
|
|
|
and
|
|
<B>MSG_EXCEPT</B>
|
|
|
|
may not both be specified in
|
|
<I>msgflg</I>.
|
|
|
|
<DT id="14"><DD>
|
|
The
|
|
<B>MSG_COPY</B>
|
|
|
|
flag was added for the implementation of
|
|
the kernel checkpoint-restore facility and
|
|
is available only if the kernel was built with the
|
|
<B>CONFIG_CHECKPOINT_RESTORE</B>
|
|
|
|
option.
|
|
<DT id="15"><B>MSG_EXCEPT</B>
|
|
|
|
<DD>
|
|
Used with
|
|
<I>msgtyp</I>
|
|
|
|
greater than 0
|
|
to read the first message in the queue with message type that differs
|
|
from
|
|
<I>msgtyp</I>.
|
|
|
|
<DT id="16"><B>MSG_NOERROR</B>
|
|
|
|
<DD>
|
|
To truncate the message text if longer than
|
|
<I>msgsz</I>
|
|
|
|
bytes.
|
|
</DL>
|
|
<P>
|
|
|
|
If no message of the requested type is available and
|
|
<B>IPC_NOWAIT</B>
|
|
|
|
isn't specified in
|
|
<I>msgflg</I>,
|
|
|
|
the calling process is blocked until one of the following conditions occurs:
|
|
<DL COMPACT>
|
|
<DT id="17">*<DD>
|
|
A message of the desired type is placed in the queue.
|
|
<DT id="18">*<DD>
|
|
The message queue is removed from the system.
|
|
In this case, the system call fails with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>EIDRM</B>.
|
|
|
|
<DT id="19">*<DD>
|
|
The calling process catches a signal.
|
|
In this case, the system call fails with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>EINTR</B>.
|
|
|
|
(<B>msgrcv</B>()
|
|
|
|
is never automatically restarted after being interrupted by a
|
|
signal handler, regardless of the setting of the
|
|
<B>SA_RESTART</B>
|
|
|
|
flag when establishing a signal handler.)
|
|
</DL>
|
|
<P>
|
|
|
|
Upon successful completion the message queue data structure is updated
|
|
as follows:
|
|
<DL COMPACT>
|
|
<DT id="20"><DD>
|
|
<I>msg_lrpid</I>
|
|
|
|
is set to the process ID of the calling process.
|
|
<DT id="21"><DD>
|
|
<I>msg_qnum</I>
|
|
|
|
is decremented by 1.
|
|
<DT id="22"><DD>
|
|
<I>msg_rtime</I>
|
|
|
|
is set to the current time.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On failure both functions return -1
|
|
with
|
|
<I>errno</I>
|
|
|
|
indicating the error,
|
|
otherwise
|
|
<B>msgsnd</B>()
|
|
|
|
returns 0
|
|
and
|
|
<B>msgrcv</B>()
|
|
|
|
returns the number of bytes actually copied into the
|
|
<I>mtext</I>
|
|
|
|
array.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
When
|
|
<B>msgsnd</B>()
|
|
|
|
fails,
|
|
<I>errno</I>
|
|
|
|
will be set to one among the following values:
|
|
<DL COMPACT>
|
|
<DT id="23"><B>EACCES</B>
|
|
|
|
<DD>
|
|
The calling process does not have write permission on the message queue,
|
|
and does not have the
|
|
<B>CAP_IPC_OWNER</B>
|
|
|
|
capability in the user namespace that governs its IPC namespace.
|
|
<DT id="24"><B>EAGAIN</B>
|
|
|
|
<DD>
|
|
The message can't be sent due to the
|
|
<I>msg_qbytes</I>
|
|
|
|
limit for the queue and
|
|
<B>IPC_NOWAIT</B>
|
|
|
|
was specified in
|
|
<I>msgflg</I>.
|
|
|
|
<DT id="25"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The address pointed to by
|
|
<I>msgp</I>
|
|
|
|
isn't accessible.
|
|
<DT id="26"><B>EIDRM</B>
|
|
|
|
<DD>
|
|
The message queue was removed.
|
|
<DT id="27"><B>EINTR</B>
|
|
|
|
<DD>
|
|
Sleeping on a full message queue condition, the process caught a signal.
|
|
<DT id="28"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
Invalid
|
|
<I>msqid</I>
|
|
|
|
value, or nonpositive
|
|
<I>mtype</I>
|
|
|
|
value, or
|
|
invalid
|
|
<I>msgsz</I>
|
|
|
|
value (less than 0 or greater than the system value
|
|
<B>MSGMAX</B>).
|
|
|
|
<DT id="29"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
The system does not have enough memory to make a copy of the
|
|
message pointed to by
|
|
<I>msgp</I>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
When
|
|
<B>msgrcv</B>()
|
|
|
|
fails,
|
|
<I>errno</I>
|
|
|
|
will be set to one among the following values:
|
|
<DL COMPACT>
|
|
<DT id="30"><B>E2BIG</B>
|
|
|
|
<DD>
|
|
The message text length is greater than
|
|
<I>msgsz</I>
|
|
|
|
and
|
|
<B>MSG_NOERROR</B>
|
|
|
|
isn't specified in
|
|
<I>msgflg</I>.
|
|
|
|
<DT id="31"><B>EACCES</B>
|
|
|
|
<DD>
|
|
The calling process does not have read permission on the message queue,
|
|
and does not have the
|
|
<B>CAP_IPC_OWNER</B>
|
|
|
|
capability in the user namespace that governs its IPC namespace.
|
|
<DT id="32"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The address pointed to by
|
|
<I>msgp</I>
|
|
|
|
isn't accessible.
|
|
<DT id="33"><B>EIDRM</B>
|
|
|
|
<DD>
|
|
While the process was sleeping to receive a message,
|
|
the message queue was removed.
|
|
<DT id="34"><B>EINTR</B>
|
|
|
|
<DD>
|
|
While the process was sleeping to receive a message,
|
|
the process caught a signal; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
<DT id="35"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>msqid</I>
|
|
|
|
was invalid, or
|
|
<I>msgsz</I>
|
|
|
|
was less than 0.
|
|
<DT id="36"><B>EINVAL</B> (since Linux 3.14)
|
|
|
|
<DD>
|
|
<I>msgflg</I>
|
|
|
|
specified
|
|
<B>MSG_COPY</B>,
|
|
|
|
but not
|
|
<B>IPC_NOWAIT</B>.
|
|
|
|
<DT id="37"><B>EINVAL</B> (since Linux 3.14)
|
|
|
|
<DD>
|
|
<I>msgflg</I>
|
|
|
|
specified both
|
|
<B>MSG_COPY</B>
|
|
|
|
and
|
|
<B>MSG_EXCEPT</B>.
|
|
|
|
<DT id="38"><B>ENOMSG</B>
|
|
|
|
<DD>
|
|
<B>IPC_NOWAIT</B>
|
|
|
|
was specified in
|
|
<I>msgflg</I>
|
|
|
|
and no message of the requested type existed on the message queue.
|
|
<DT id="39"><B>ENOMSG</B>
|
|
|
|
<DD>
|
|
<B>IPC_NOWAIT</B>
|
|
|
|
and
|
|
<B>MSG_COPY</B>
|
|
|
|
were specified in
|
|
<I>msgflg</I>
|
|
|
|
and the queue contains less than
|
|
<I>msgtyp</I>
|
|
|
|
messages.
|
|
<DT id="40"><B>ENOSYS</B> (since Linux 3.8)
|
|
|
|
<DD>
|
|
<I>MSG_COPY</I>
|
|
|
|
was specified in
|
|
<I>msgflg</I>,
|
|
|
|
and this kernel was configured without
|
|
<B>CONFIG_CHECKPOINT_RESTORE</B>.
|
|
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4.
|
|
<P>
|
|
|
|
The
|
|
<B>MSG_EXCEPT</B>
|
|
|
|
and
|
|
<B>MSG_COPY</B>
|
|
|
|
flags are Linux-specific;
|
|
their definitions can be obtained by defining the
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
|
|
feature test macro.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The inclusion of
|
|
<I><<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></I>
|
|
|
|
and
|
|
<I><<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>></I>
|
|
|
|
isn't required on Linux or by any version of POSIX.
|
|
However,
|
|
some old implementations required the inclusion of these header files,
|
|
and the SVID also documented their inclusion.
|
|
Applications intended to be portable to such old systems may need
|
|
to include these header files.
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>msgp</I>
|
|
|
|
argument is declared as <I>struct msgbuf *</I> in
|
|
glibc 2.0 and 2.1.
|
|
It is declared as <I>void *</I>
|
|
in glibc 2.2 and later, as required by SUSv2 and SUSv3.
|
|
<P>
|
|
|
|
The following limits on message queue resources affect the
|
|
<B>msgsnd</B>()
|
|
|
|
call:
|
|
<DL COMPACT>
|
|
<DT id="41"><B>MSGMAX</B>
|
|
|
|
<DD>
|
|
Maximum size of a message text, in bytes (default value: 8192 bytes).
|
|
On Linux, this limit can be read and modified via
|
|
<I>/proc/sys/kernel/msgmax</I>.
|
|
|
|
<DT id="42"><B>MSGMNB</B>
|
|
|
|
<DD>
|
|
Maximum number of bytes that can be held in a message queue
|
|
(default value: 16384 bytes).
|
|
On Linux, this limit can be read and modified via
|
|
<I>/proc/sys/kernel/msgmnb</I>.
|
|
|
|
A privileged process
|
|
(Linux: a process with the
|
|
<B>CAP_SYS_RESOURCE</B>
|
|
|
|
capability)
|
|
can increase the size of a message queue beyond
|
|
<B>MSGMNB</B>
|
|
|
|
using the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+msgctl">msgctl</A></B>(2)
|
|
|
|
<B>IPC_SET</B>
|
|
|
|
operation.
|
|
</DL>
|
|
<P>
|
|
|
|
The implementation has no intrinsic system-wide limits on the
|
|
number of message headers
|
|
(<B>MSGTQL</B>)
|
|
|
|
and the number of bytes in the message pool
|
|
(<B>MSGPOOL</B>).
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
In Linux 3.13 and earlier,
|
|
if
|
|
<B>msgrcv</B>()
|
|
|
|
was called with the
|
|
<B>MSG_COPY</B>
|
|
|
|
flag, but without
|
|
<B>IPC_NOWAIT</B>,
|
|
|
|
and the message queue contained less than
|
|
<I>msgtyp</I>
|
|
|
|
messages, then the call would block until the next message is written
|
|
to the queue.
|
|
|
|
At that point, the call would return a copy of the message,
|
|
<I>regardless</I>
|
|
|
|
of whether that message was at the ordinal position
|
|
<I>msgtyp</I>.
|
|
|
|
This bug is fixed
|
|
|
|
in Linux 3.14.
|
|
<P>
|
|
|
|
Specifying both
|
|
<B>MSG_COPY</B>
|
|
|
|
and
|
|
<B>MSC_EXCEPT</B>
|
|
|
|
in
|
|
<I>msgflg</I>
|
|
|
|
is a logical error (since these flags impose different interpretations on
|
|
<I>msgtyp</I>).
|
|
|
|
In Linux 3.13 and earlier,
|
|
|
|
this error was not diagnosed by
|
|
<B>msgrcv</B>().
|
|
|
|
This bug is fixed
|
|
|
|
in Linux 3.14.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>msgsnd</B>()
|
|
|
|
and
|
|
<B>msgrcv</B>().
|
|
|
|
<P>
|
|
|
|
The example program is first run with the <B>-s</B> option to send a
|
|
message and then run again with the <B>-r</B> option to receive a
|
|
message.
|
|
<P>
|
|
|
|
The following shell session shows a sample run of the program:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out -s</B>
|
|
|
|
sent: a message at Wed Mar 4 16:25:45 2015
|
|
<P>
|
|
$<B> ./a.out -r</B>
|
|
|
|
message received: a message at Wed Mar 4 16:25:45 2015
|
|
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/msg.h">sys/msg.h</A>>
|
|
<P>
|
|
struct msgbuf {
|
|
<BR> long mtype;
|
|
<BR> char mtext[80];
|
|
};
|
|
<P>
|
|
static void
|
|
usage(char *prog_name, char *msg)
|
|
{
|
|
<BR> if (msg != NULL)
|
|
<BR> fputs(msg, stderr);
|
|
<P>
|
|
<BR> fprintf(stderr, "Usage: %s [options]\n", prog_name);
|
|
<BR> fprintf(stderr, "Options are:\n");
|
|
<BR> fprintf(stderr, "-s send message using msgsnd()\n");
|
|
<BR> fprintf(stderr, "-r read message using msgrcv()\n");
|
|
<BR> fprintf(stderr, "-t message type (default is 1)\n");
|
|
<BR> fprintf(stderr, "-k message queue key (default is 1234)\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
}
|
|
<P>
|
|
static void
|
|
send_msg(int qid, int msgtype)
|
|
{
|
|
<BR> struct msgbuf msg;
|
|
<BR> time_t t;
|
|
<P>
|
|
<BR> msg.mtype = msgtype;
|
|
<P>
|
|
<BR> time(&t);
|
|
<BR> snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
|
|
<BR> ctime(&t));
|
|
<P>
|
|
<BR> if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
|
|
<BR> IPC_NOWAIT) == -1) {
|
|
<BR> perror("msgsnd error");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<BR> printf("sent: %s\n", msg.mtext);
|
|
}
|
|
<P>
|
|
static void
|
|
get_msg(int qid, int msgtype)
|
|
{
|
|
<BR> struct msgbuf msg;
|
|
<P>
|
|
<BR> if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
|
|
<BR> MSG_NOERROR | IPC_NOWAIT) == -1) {
|
|
<BR> if (errno != ENOMSG) {
|
|
<BR> perror("msgrcv");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<BR> printf("No message available for msgrcv()\n");
|
|
<BR> } else
|
|
<BR> printf("message received: %s\n", msg.mtext);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int qid, opt;
|
|
<BR> int mode = 0; /* 1 = send, 2 = receive */
|
|
<BR> int msgtype = 1;
|
|
<BR> int msgkey = 1234;
|
|
<P>
|
|
<BR> while ((opt = getopt(argc, argv, "srt:k:")) != -1) {
|
|
<BR> switch (opt) {
|
|
<BR> case 's':
|
|
<BR> mode = 1;
|
|
<BR> break;
|
|
<BR> case 'r':
|
|
<BR> mode = 2;
|
|
<BR> break;
|
|
<BR> case 't':
|
|
<BR> msgtype = atoi(optarg);
|
|
<BR> if (msgtype <= 0)
|
|
<BR> usage(argv[0], "-t option must be greater than 0\n");
|
|
<BR> break;
|
|
<BR> case 'k':
|
|
<BR> msgkey = atoi(optarg);
|
|
<BR> break;
|
|
<BR> default:
|
|
<BR> usage(argv[0], "Unrecognized option\n");
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (mode == 0)
|
|
<BR> usage(argv[0], "must use either -s or -r option\n");
|
|
<P>
|
|
<BR> qid = msgget(msgkey, IPC_CREAT | 0666);
|
|
<P>
|
|
<BR> if (qid == -1) {
|
|
<BR> perror("msgget");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (mode == 2)
|
|
<BR> get_msg(qid, msgtype);
|
|
<BR> else
|
|
<BR> send_msg(qid, msgtype);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+msgctl">msgctl</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+msgget">msgget</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+mq_overview">mq_overview</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sysvipc">sysvipc</A></B>(7)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 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
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="43"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="44"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="45"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="46"><A HREF="#lbAE">msgsnd()</A><DD>
|
|
<DT id="47"><A HREF="#lbAF">msgrcv()</A><DD>
|
|
</DL>
|
|
<DT id="48"><A HREF="#lbAG">RETURN VALUE</A><DD>
|
|
<DT id="49"><A HREF="#lbAH">ERRORS</A><DD>
|
|
<DT id="50"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="51"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DT id="52"><A HREF="#lbAK">BUGS</A><DD>
|
|
<DT id="53"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="54"><A HREF="#lbAM">Program source</A><DD>
|
|
</DL>
|
|
<DT id="55"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="56"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:33 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|