ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/ipcmsgrc.htm

453 lines
13 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>msgrcv()--Receive Message Operation</title>
<!-- Begin Header Records ========================================== -->
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Change History: -->
<!-- YYMMDD USERID Change description -->
<!-- Direct1 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<!-- Edited by Kersten Feb 02 -->
<!-- This file has undergone error condition cleanup on 05/01/02 by JET -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--End Header Records -->
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>msgrcv()--Receive Message Operation</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;sys/msg.h&gt;
int msgrcv(int <em>msqid</em>, void <em>*msgp</em>, size_t <em>msgsz</em>,
long int <em>msgtyp</em>, int <em>msgflg</em>);
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QP0ZUSHR<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>msgrcv()</strong> function reads a message from the message queue
specified by the <em>msqid</em> parameter and
places it in the user-defined buffer pointed to by the <em>*msgp</em> parameter.</p>
<p>The <em>*msgp</em> parameter points to a user-defined buffer that must
contain the following:</p>
<ol>
<li>A field of type <samp>long int</samp> that specifies the type of the
message.</li>
<li>A data part that contains the data bytes of the message.</li>
</ol>
<p>The following structure is an example of what this user-defined buffer might
look like for a message that has 5 bytes of data:</p>
<pre>
struct mymsg {
long int mtype; /* message type */
char mtext[5]; /* message text */
}
</pre>
<p>The value of <samp>mtype</samp> is the type of the received message,
as specified by the sender of the message.</p>
<p>The <em>msgtyp</em> parameter specifies the type of message to receive from
the message queue as follows:</p>
<ul>
<li>If <em>msgtyp</em> is equal to zero, the first message is
received.</li>
<li>If <em>msgtyp</em> is greater than zero, the first message of type <em>
msgtyp</em> is received.</li>
<li>If <em>msgtyp</em> is less than zero, the first message of the lowest type
that is less than or equal to the absolute value of <em>msgtyp</em> is
received.</li>
</ul>
<p>The <em>msgsz</em> parameter specifies the size in bytes of the data part of the
message. The received message is truncated to <em>msgsz</em> bytes if it
is larger than <em>msgsz</em> and the MSG_NOERROR flag is set in the <em>msgflg</em>
parameter. The truncated part of the message is lost and no indication of the
truncation is given to the calling thread. Otherwise, <strong>msgrcv()</strong>
returns with return value -1 and <em>errno</em> set to E2BIG.</p>
<p>If a message of the desired type is not available on the message queue, the
<em>msgflg</em> parameter specifies the action to be taken. The actions are as follows:</p>
<br>
<ul>
<li>If the IPC_NOWAIT flag is set in the <em>msgflg</em> parameter, <strong>msgrcv()</strong>
returns immediately with a return value of -1 and <em>errno</em> set to
ENOMSG.<br>
<br>
</li>
<li>If the IPC_NOWAIT flag is not set in the <em>msgflg</em> parameter, the calling thread suspends
processing until one of the following occurs:<br><br>
<ul>
<li>A message of the desired type is sent to the message queue.</li>
<li>The message queue identifier <em>msqid</em> is removed from the system.
When this occurs, the <strong>msgrcv()</strong> function returns with a return value of -1 and
<em>errno</em> set to EIDRM.</li>
<li>A signal is delivered to the calling thread.
When this occurs, the <strong>msgrcv()</strong> function returns with a return value of -1 and
<em>errno</em> set to EINTR.</li>
</ul>
</ul>
<p>The system maintains status information about a message queue which can be
retrieved with the <a href="ipcmsgct.htm">msgctl()</a> function. When a message
is successfully received from the message queue, the system sets the members of the
<samp>msqid_ds</samp> structure as follows:</p>
<br>
<ul>
<li><samp>msg_qnum</samp> is decremented by 1.<br>
<br>
</li>
<li><samp>msg_lrpid</samp> is set to the process ID of the calling thread.<br>
<br>
</li>
<li><samp>msg_rtime</samp> is set to the current time.</li>
</ul>
<h3>Parameters</h3>
<dl>
<dt><strong>msqid</strong></dt>
<dd>(Input) Message queue identifier, a positive integer. It is returned by the
<a href="ipcmsggt.htm">msgget()</a> function and used to identify the message queue
to receive the message from.<br>
<br>
</dd>
<dt><strong>msgp</strong></dt>
<dd>(Output) Pointer to a buffer in which the received message will be stored.
See above for the details on the format of the buffer.<br>
<br>
</dd>
<dt><strong>msgsz</strong></dt>
<dd>(Input) Length of the data part of the buffer.<br>
<br>
</dd>
<dt><strong>msgtyp</strong></dt>
<dd>(Input) Type of message to be received.<br>
<br>
</dd>
<dt><strong>msgflg</strong></dt>
<dd>(Input) Operations flags. The value of <em>msgflg</em> is either zero or is
obtained by performing an OR operation on one or more of the following constants:
<dl>
<dt><strong>IPC_NOWAIT (0x00000800)</strong></dt>
<dd>If a message is not available, do not wait for the message and return immediately.</dd>
<dt><strong>MSG_NOERROR (0x00001000)</strong></dt>
<dd>If the data part of the message is larger than <em>msgsz</em>, do not return an error.<p></dd>
</dl>
</dd>
</dl>
<br>
<h3>Authorities</h3>
<p><strong><a name="TBLAMRCV1">Authorization Required for
msgrcv()</a></strong></p>
<table border cellpadding="5">
<!-- cols="60 20 20" -->
<tr>
<th align="left" valign="bottom">Object Referred to</th>
<th align="left" valign="bottom">Authority Required</th>
<th align="left" valign="bottom">errno</th>
</tr>
<tr>
<td align="left" valign="top">Message queue from which message is received</td>
<td align="left" valign="top">Read</td>
<td align="left" valign="top">EACCES</td>
</tr>
</table>
<br>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top"><em>value</em></td>
<td align="left" valign="top"><strong>msgrcv()</strong> was successful. The
value returned is the number of bytes of data placed in the data part of the buffer
pointed to by the <em>msgp</em> parameter.</td>
</tr>
<tr>
<td align="left" valign="top"><em>-1</em></td>
<td align="left" valign="top"><strong>msgrcv()</strong> was not successful. The
<em>errno</em> variable is set to indicate the error.</td>
</tr>
</table>
<br>
<br>
<h3>Error Conditions</h3>
<p>If <strong>msgrcv()</strong> is not successful, <em>errno</em> usually
indicates one of the following errors. Under some conditions, <em>errno</em>
could indicate an error other than those listed here.</p>
<dl compact>
<dt><em>[E2BIG]</em></dt>
<dd>
<p>Argument list too long.</p>
<p>The size in bytes of the message is greater than <em>msgsz</em> and
the MSG_NOERROR flag is not set in the <em>msgflg</em> parameter.</p>
</dd>
<dt><em>[EACCES]</em></dt>
<dd>
<p>Permission denied.</p>
<p>An attempt was made to access an object in a way forbidden by its object
access permissions.</p>
<p>The thread does not have access to the specified file, directory, component,
or path.</p>
<p>The calling thread does not have read permission to the message queue.</p>
</dd>
<dt><em>[EDAMAGE]</em></dt>
<dd>
<p>A damaged object was encountered.</p>
<p>A referenced object is damaged. The object cannot be used.</p>
<p>The message queue has been damaged by a previous message queue
operation.</p>
</dd>
<dt><em>[EFAULT]</em></dt>
<dd>
<p>The address used for an argument is not correct.</p>
<p>In attempting to use an argument in a call, the system detected an address
that is not valid.</p>
<p>While attempting to access a parameter passed to this function, the system
detected an address that is not valid.</p>
</dd>
<dt><em>[EIDRM]</em></dt>
<dd>
<p>ID has been removed.</p>
<p>The message queue identifier <em>msqid</em> was removed from the system.</p>
</dd>
<dt><em>[EINTR]</em></dt>
<dd>
<p>Interrupted function call.</p>
<p>The function <strong>msgrcv()</strong> was interrupted by a signal.</p>
</dd>
<dt><em>[EINVAL]</em></dt>
<dd>
<p>The value specified for the argument is not correct.</p>
<p>A function was passed incorrect argument values, or an operation was
attempted on an object and the operation specified is not supported for that
type of object.</p>
<p>An argument value is not valid, out of range, or NULL.</p>
<p>One of the following has occurred:</p>
<ul>
<li>The <em>*msgp</em> parameter is NULL.</li>
<li>The <em>msqid</em> parameter is not a valid message queue identifier.</li>
</ul>
</dd>
<dt><em>[ENOMSG]</em></dt>
<dd>
<p>Message does not exist.</p>
<p>The message queue does not contain a message of the desired type and the
IPC_NOWAIT flag is set in the <em>msgflg</em> parameter.</p>
</dd>
<dt><em>[EUNKNOWN]</em></dt>
<dd>
<p>Unknown system state.</p>
<p>The operation failed because of an unknown system state. See any messages in
the job log and correct any errors that are indicated, then retry the
operation.</p>
</dd>
</dl>
<br>
<h3>Error Messages</h3>
<p>None.</p>
<br>
<h3><a name="USAGE_NOTES">Usage Notes</a></h3>
<ol>
<li>The parameter <em>msgsz</em> should include any bytes inserted by the
compiler for padding or alignment purposes. These bytes are part of the message
data and affect the total number of bytes in the message queue.</li>
<li>The following example shows pad data and how it affects the size of a
message using datamodel *P128:</li>
<li><pre>
struct mymsg {
long int mtype; /* 12 bytes padding inserted after */
char *pointer; /* the mtype field by the compiler.*/
char c; /* 15 bytes padding inserted after */
char *pointer2; /* the c field by the compiler. */
} msg; /* After the mtype field, there are*/
/* 33 bytes of user data, but 60 */
/* bytes of data including padding.*/
msgsz = sizeof(msg) - sizeof(long int); /* 60 bytes. */
</pre>
</li>
<li>The <strong>msgrcv()</strong> function does not tag message data with a
CCSID (coded character set identifier) value. If a CCSID value is required to
correctly interpret the message data, it is the responsibility of the caller to
include the CCSID value as part of the data.<br>
<br>
</li>
<li>If the <strong>msgrcv()</strong> function does not complete successfully,
the requested message is not removed from the message queue.</li>
</ol>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>sys/msg.h</strong>&gt; file (see <a href="unix13.htm">
Header Files for UNIX-Type Functions</a>)<br>
<br>
</li>
<li><a href="unix3a.htm#messagequeue">Message Queues</a> for the current system limits.
<br><br></li>
<li><a href="ipcmsgct.htm">msgctl()</a>--Perform Message Control Operations<br>
<br>
</li>
<li><a href="ipcmsggt.htm">msgget()</a>--Get Message Queue<br>
<br>
</li>
<li><a href="ipcmsgsn.htm">msgsnd()</a>--Send Message Operation</li>
<li><a href="sigactn.htm">sigaction()</a>--Examine and change signal action<p></li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<p>The following example receives a message from a message queue:</p>
<pre>
#include &lt;sys/msg.h&gt;
main() {
int msqid = 2;
int rc;
size_t msgsz;
long int msgtyp;
struct mymsg {
long int mtype;
char mtext[256];
};
msgsz = sizeof(struct mymsg) - sizeof(long int);
msgtyp = 1;
rc = msgrcv(msqid, &amp;mymsg, msgsz, msgtyp, IPC_NOWAIT);
}
</pre>
<br>
<hr>
API introduced: V3R6
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>