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

417 lines
11 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!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>msgsnd()--Send 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>msgsnd()--Send Message Operation</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;sys/msg.h&gt;
int msgsnd(int <em>msqid</em>, void <em>*msgp</em>,
size_t <em>msgsz</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>msgsnd()</strong> function is used to send a message to the
message queue specified by the <em>msqid</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.<p></li>
<li>A data part that contains the data bytes of the message.</li>
</ol>
<p>The following structure is an example of what the 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> must be greater than zero.
When messages are received with <a href="ipcmsgrc.htm">msgrcv()</a>, the
message type can be used to select the messages.
The message data can be any length up to the system limit.
</p>
<p>If the message queue is full, the <em>msgflg</em> parameter specifies the action
to be taken. The actions are as follows:<br>
<br>
<ul>
<li>If the IPC_NOWAIT flag is set in the <em>msgflg</em> parameter, the message is not sent.
The <strong>msgsnd()</strong> function returns immediately with a return value of -1 and <em>
errno</em> set to EAGAIN.<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>Enough messages are received from the message queue so that a message of size <em>msgsz</em>
can be placed on the message queue.<p></li>
<li>The message queue identifier <em>msqid</em> is removed from the system.
When this occurs, the <strong>msgsnd()</strong> function returns with a return value of -1 and
<em>errno</em> set to EIDRM.<p></li>
<li>A signal is delivered to the calling thread.
When this occurs, the <strong>msgsnd()</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 sent to the message queue, the system sets the members of the
<samp>msqid_ds</samp> structure as follows:<br>
<br>
<ul>
<li><samp>msg_qnum</samp> is incremented by 1.<p></li>
<li><samp>msg_lspid</samp> is set to the process ID of the calling thread.<p></li>
<li><samp>msg_stime</samp> is set to the current time.</li>
</ul>
<br>
<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 send the message to.<br>
<br>
</dd>
<dt><strong>msgp</strong></dt>
<dd>(Input) Pointer to a buffer with the message to be sent. 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 message to be sent.<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:<p>
<dl>
<dt><strong>IPC_NOWAIT (0x00000800)</strong></dt>
<dd>If the message queue is full, do not wait for space to become available on
the message queue and return immediately.<p></dd>
</dl>
</dd>
</dl>
<br>
<h3>Authorities</h3>
<p><strong><a name="TBLAMSND1">Authorization Required for
msgsnd()</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 on which message is placed</td>
<td align="left" valign="top">Write</td>
<td align="left" valign="top">EACCES</td>
</tr>
</table>
<br>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="5 95" -->
<tr>
<td align="left" valign="top"><em>0</em></td>
<td align="left" valign="top"><strong>msgsnd()</strong> was successful.</td>
</tr>
<tr>
<td align="left" valign="top"><em>-1</em></td>
<td align="left" valign="top"><strong>msgsnd()</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>msgsnd()</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>[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 write permission to the message queue.</p>
</dd>
<dt><em>[EAGAIN]</em></dt>
<dd>
<p>Operation would have caused the process to be suspended.</p>
<p>The message cannot be sent for one of the reasons cited above and the IPC_NOWAIT
flag is set in the <em>msgflg</em> parameter.</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>msgsnd()</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>
<li>The <em>mtype</em> parameter is less than or equal to zero.</li>
<li>The <em>msgsz</em> parameter is greater than the system limit.</li>
</ul><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><p>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.</p>
<p>The following example shows pad data and how it affects the size of a
message when using datamodel *P128:</p>
<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>msgsnd()</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.<p></li>
<li>If the <strong>msgsnd()</strong> function does not complete successfully,
the requested message is not placed on 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="ipcmsgrc.htm">msgrcv()</a>--Receive Message Operation<p></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 sends a message to a message queue:</p>
<pre>
#include &lt;sys/msg.h&gt;
main() {
int msqid = 2;
int rc;
size_t msgsz;
struct mymsg {
long int mtype;
char mtext[256];
};
msgsz = sizeof(struct mymsg) - sizeof(long int);
mymsg.mtype = 1;
rc = msgsnd(msqid, &amp;mymsg, msgsz, 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>