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

381 lines
10 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>msync()--Synchronize Modified Data with Mapped File</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 -->
<!-- MSYNC SCRIPT A converted by B2H R4.1 (346) (CMS) by V2CDIJAB at -->
<!-- RCHVMW2 on 19 Sep 2000 at 14:21:52 -->
<!-- 011108 JTROUS Changes from CL Review 1, V5R2 -->
<!-- 0205?? JET This file has undergone html cleanup -->
<!-- 020918 Rosckes: Add ENOTAVAIL errno description, V5R3 -->
<!-- 050404 JTROUS: fix enums, no change flag, V5R4 -->
<!-- 050404 JTROUS Add note on tagged pointers, V5R4 -->
<!-- End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link -->
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
</script>
<a name="Top_Of_Page"></a>
<h2>msync()--Synchronize Modified Data with Mapped File</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;sys/types.h&gt;
#include &lt;sys/mman.h&gt;
int msync( void <em>*addr</em>,
size_t <em>len</em>,
int <em>flags</em> );
</pre>
&nbsp;&nbsp;Service Program Name: QP0LLIB1<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>msync()</strong> function can be used to write modified data
from a shared mapping (created using the <strong>mmap()</strong> function) to
non-volatile storage or invalidate privately mapped pages. The data located
through mapping address <em>addr</em> for a length of <em>len</em> are either
written to disk, or invalidated, depending on the value of <em>flags</em> and
the private or shared nature of the mapping.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong><em>addr</em></strong></dt>
<dd>The starting address of the memory region to be synchronized to permanent
storage. The specified address must be a multiple of the page size.<br>
<br>
</dd>
<dt><strong><em>len</em></strong></dt>
<dd>The number of bytes affected. The length must not be zero. If the length is
not a multiple of the page size the system will round this value to the next
page boundary.<br>
<br>
</dd>
<dt><strong><em>flags</em></strong></dt>
<dd>The desired synchronization.
<p>The following table shows the symbolic constants allowed for the flags
parameter.</p>
<table border cellpadding="5" width="80%">
<tr>
<th align="left" valign="top">Symbolic Constant</th>
<th align="left" valign="top">Decimal<br>
Value</th>
<th align="left" valign="bottom">Description</th>
</tr>
<tr>
<td align="left" valign="top" width="20%">MS_ASYNC</td>
<td align="center" valign="top" width="15%">1</td>
<td align="left" valign="top" width="65%">Perform asynchronous writes.</td>
</tr>
<tr>
<td align="left" valign="top">MS_SYNC</td>
<td align="center" valign="top">2</td>
<td align="left" valign="top">Perform synchronous writes.</td>
</tr>
<tr>
<td align="left" valign="top">MS_INVALIDATE</td>
<td align="center" valign="top">4</td>
<td align="left" valign="top">Invalidate privately cached data</td>
</tr>
</table>
<p>The MS_SYNC and MS_ASYNC options are mutually exclusive. The MS_SYNC and
MS_ASYNC options are ignored if the memory map was created with the MAP_PRIVATE
option.</p>
<p>The MS_INVALIDATE option is used to discard changes made to a memory map
created with the MAP_PRIVATE option. The private memory map is synchronized
with the current data in the file. Any reference subsequent to the execution of
the <strong>msync()</strong> function that invalidates a page will result in a
reference to the current value of the file. The first modification of a page
after the privately mapped page is invalidated results in the creation of a
fresh private copy of that page. Subsequent modifications of this page prior to
the next execution of an msync that invalidates the page will result in
modifications to the same private copy of the page.</p>
<p>The MS_INVALIDATE value is ignored if the memory map was created with the
MAP_SHARED option.</p>
</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>No authorization is required.</p>
<br>
<h3>Return Value</h3>
<p>Upon successful completion, the <strong>msync()</strong> function returns
0.</p>
<br>
<h3>Error Conditions</h3>
<p>When the <strong>msync()</strong> function fails, it returns -1 and sets
errno as follows.</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<th align="left" valign="bottom">Error condition</th>
<th align="left" valign="bottom">Additional information</th>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EINVAL">EINVAL</a>]</em></td>
<td align="left" valign="top">
<p>For example, the value of the <em>len</em> parameter may be zero. Or,
the value of the <em>addr</em> may not be a multiple of the page size or is
out of the allowed range.</p>
</td></tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOTAVAIL">ENOTAVAIL</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EUNKNOWN">EUNKNOWN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
</table>
<br>
<br>
<h3>Error Messages</h3>
<p>The following messages may be sent from this function.</p>
<table width="100%">
<tr>
<th align="left" valign="top">Message ID</th>
<th align="left" valign="top">Error Message Text</th>
</tr>
<tr>
<td width="15%" valign="top">CPE3418 E</td>
<td width="85%" valign="top">Possible APAR condition or hardware failure.</td>
</tr>
<tr>
<td valign="top">CPFA0D4 E</td>
<td valign="top">File system error occurred. Error number &amp;1.</td>
</tr>
<tr>
<td valign="top">CPF3CF2 E</td>
<td valign="top">Error(s) occurred during running of &amp;1 API.</td>
</tr>
<tr>
<td valign="top">CPF9872 E</td>
<td valign="top">Program or service program &amp;1 in library &amp;2 ended.
Reason code &amp;3.</td>
</tr>
</table>
<br>
<h3>Usage Notes</h3>
<ol>
<li>The <strong>msync()</strong> function must be used to write changed pages
of a shared mapping to disk. If a system crash occurs before the <strong>
msync()</strong> function completes, some data may not be preserved.
<p>Process termination does not automatically write changed pages to disk. Some
or all pages may be eventually written by the paging subsystem, but no
guarantee is given. Therefore, if the data must be preserved the <strong>
msync()</strong> function must be used to ensure changes made through a shared
memory map are written to disk.</p>
</li>
<li>The address pointer that was returned by <strong>mmap()</strong> can only
be used with the V4R4M0 or later versions of the following languages:
<ul>
<li>ILE COBOL</li>
<li>ILE RPG</li>
<li>ILE C if the TERASPACE parameter is used when compiling the program.</li>
</ul>
<br>
</li>
<li>
<img src="delta.gif" alt="Start of change">
The application cannot write or store any data via the memory mapping which
includes any tagged (16-byte) pointers because the pointer attribute will be lost.
Some examples of tagged pointers include space pointers, system pointers,
invocation pointers etc..
<p>If the DTAMDL(*LLP64) parameter is used when compiling an ILE C program,
this limitation does not apply as the pointers will be 8 byte pointers, and their
pointer attribute will be preserved.
<img src="deltaend.gif" alt="End of change">
</li>
</ol>
<br>
<h3>Related Information</h3>
<ul>
<li><a href="open.htm">open()</a>--Open File
</li>
<li><a href="open64.htm">open64()</a>--Open File (Large File Enabled)
</li>
<li><a href="mmap.htm">mmap()</a>--Memory Map a Stream File
</li>
<li><a href="munmap.htm">munmap()</a>--Remove Memory Mapping
</li>
<li><a href="mprotect.htm">mprotect()</a>--Change Access Protection for Memory
Mapping</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 creates a file, creates a memory map, stores data into
the file, and writes the data to disk using the <strong>msync()</strong>
function.</p>
<pre>
#include &lt;errno.h &gt;
#include &lt;fcntl.h &gt;
#include &lt;unistd.h &gt;
#include &lt;stdio.h &gt;
#include &lt;stdlib.h &gt;
#include &lt;string.h &gt;
#include &lt;sys/types.h &gt;
#include &lt;sys/mman.h &gt;
main(void) {
size_t bytesWritten =0;
int fd;
int PageSize;
const char text&Yacute; = "This is a test";
if ( (PageSize = sysconf(_SC_PAGE_SIZE)) &lt; 0) {
perror("sysconf() Error=");
return -1;
}
fd = open("/tmp/mmsyncTest",
(O_CREAT | O_TRUNC | O_RDWR),
(S_IRWXU | S_IRWXG | S_IRWXO) );
if ( fd &lt; 0 ) {
perror("open() error");
return fd;
}
off_t lastoffset = lseek( fd, PageSize, SEEK_SET);
bytesWritten = write(fd, " ", 1 );
if (bytesWritten != 1 ) {
perror("write error. ");
return -1;
}
/* mmap the file. */
void *address;
int len;
off_t my_offset = 0;
len = PageSize; /* Map one page */
address =
mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, my_offset);
if ( address == MAP_FAILED ) {
perror("mmap error. " );
return -1;
}
/* Move some data into the file using memory map. */
(void) strcpy( (char*) address, text);
/* use msync to write changes to disk. */
if ( msync( address, PageSize , MS_SYNC ) &lt; 0 ) {
perror("msync failed with error:");
return -1;
}
else (void) printf("%s","msync completed successfully.");
close(fd);
unlink("/tmp/msyncTest");
}
</pre>
<p><strong>Output:</strong></p>
<pre>
This is a test.
</pre>
<hr>
API introduced: V5R1
<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>