307 lines
8.5 KiB
HTML
307 lines
8.5 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>munmap()--Remove Memory Mapping</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 -->
|
||
|
<!-- MUNMAP SCRIPT A converted by B2H R4.1 (346) (CMS) by V2CDIJAB at -->
|
||
|
<!-- RCHVMW2 on 19 Sep 2000 at 14:19:56 -->
|
||
|
<!-- 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>munmap()--Remove Memory Mapping</h2>
|
||
|
|
||
|
<div class="box" style="width: 60%;">
|
||
|
<br>
|
||
|
Syntax<br>
|
||
|
|
||
|
|
||
|
<pre>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/mman.h>
|
||
|
|
||
|
int munmap ( void <em>*addr</em>,
|
||
|
size_t <em>len</em> );
|
||
|
</pre>
|
||
|
|
||
|
Service Program Name: QP0LLIB1<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Default Public Authority: *USE<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Threadsafe: Yes<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
</div>
|
||
|
|
||
|
<p>The <strong>munmap()</strong> function removes addressability to a range of
|
||
|
memory mapped pages of a process's address space. All pages starting with <em>
|
||
|
addr</em> and continuing for a length of <em>len</em> bytes are removed.</p>
|
||
|
|
||
|
<p>The address range specified must begin on a page boundary. Portions of the
|
||
|
specified address range which are not mapped, or were not established by the
|
||
|
<strong>mmap()</strong> function, are not affected by the <strong>
|
||
|
munmap()</strong> function.</p>
|
||
|
|
||
|
<p>If the mapping was created <strong><em>MAP_PRIVATE</em></strong> then any
|
||
|
private altered pages are discarded and the system storage associated with the
|
||
|
copies are returned to the system free space.</p>
|
||
|
|
||
|
<p>When the mapping is removed, the reference associated with the pages mapped
|
||
|
over the file is removed. If the file has no references other than those due to
|
||
|
memory mapping and the remaining memory mappings are removed by the <strong>
|
||
|
munmap()</strong> function, then the file becomes unreferenced. If the file
|
||
|
becomes unreferenced due to an <strong>munmap()</strong> function call and the
|
||
|
file is no longer linked, then the file will be deleted.</p>
|
||
|
|
||
|
<br>
|
||
|
<h3>Parameters</h3>
|
||
|
|
||
|
<dl>
|
||
|
<dt><strong><em>addr</em></strong></dt>
|
||
|
|
||
|
<dd>The starting address of the memory region being removed.
|
||
|
|
||
|
<p>The <em>addr</em> parameter must be a multiple of the page size. The value
|
||
|
zero or NULL is not a valid starting address. The <strong>sysconf()</strong>
|
||
|
function may be used to determine the system page size.<br>
|
||
|
<br>
|
||
|
</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><strong><em>len</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) The length of the address range. All whole pages beginning with
|
||
|
<em>addr</em> for a length of <em>len</em> are included in the address
|
||
|
range.</dd>
|
||
|
</dl>
|
||
|
|
||
|
<br>
|
||
|
<h3>Authorities</h3>
|
||
|
|
||
|
<p>No authorization is required.</p>
|
||
|
|
||
|
<br>
|
||
|
<h3>Return Value</h3>
|
||
|
|
||
|
<p>Upon successful completion, the <strong>munmap()</strong> function returns
|
||
|
0. Upon failure, -1 is returned and errno is set to the appropriate error
|
||
|
number.</p>
|
||
|
|
||
|
<br>
|
||
|
<h3>Error Conditions</h3>
|
||
|
|
||
|
<p>When the <strong>munmap()</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, for <strong>munmap()</strong> this may mean that the address range from <em>
|
||
|
addr</em> and continuing for a length of <em>len</em> is outside the valid
|
||
|
range allowed for a process. This error may also indicate that the value for
|
||
|
the <em>addr</em> parameter is not a multiple of the page size. A value of 0
|
||
|
for parameter <em>len</em> also will result in this error number.</p>
|
||
|
</td></tr>
|
||
|
|
||
|
<tr>
|
||
|
<td align="left" valign="top">
|
||
|
<em>[<a href="unix14.htm#ENOTAVAIL">ENOTAVAIL</a>]</em></td>
|
||
|
|
||
|
<td align="left" valign="top">
|
||
|
|
||
|
</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td align="left" valign="top">
|
||
|
<em>[<a href="unix14.htm#EUNKNOWN">EUNKNOWN</a>]</em></td>
|
||
|
|
||
|
<td align="left" valign="top">
|
||
|
|
||
|
</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 &1.</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td valign="top">CPF3CF2 E</td>
|
||
|
<td valign="top">Error(s) occurred during running of &1 API.</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td valign="top">CPF9872 E</td>
|
||
|
<td valign="top">Program or service program &1 in library &2 ended.
|
||
|
Reason code &3.</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
|
||
|
<br>
|
||
|
<h3>Usage Notes</h3>
|
||
|
|
||
|
<ol>
|
||
|
<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="mprotect.htm">mprotect()</a>--Change Access Protection for Memory
|
||
|
Mapping
|
||
|
</li>
|
||
|
|
||
|
<li><a href="msync.htm">msync()</a>--Synchronize Modified Data with Mapped
|
||
|
File</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, produces a memory mapping of the file
|
||
|
using mmap(), and then removes the mapping using the <strong>munmap()</strong>
|
||
|
function.</p>
|
||
|
|
||
|
<pre>
|
||
|
#include <stdio.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/mman.h>
|
||
|
|
||
|
main() {
|
||
|
char fn[]="creat.file";
|
||
|
char text[]="This is a test";
|
||
|
int fd;
|
||
|
int PageSize;
|
||
|
|
||
|
if ((fd =
|
||
|
open(fn, O_CREAT | O_RDWR | O_APPEND,S_IRWXU) < 0)
|
||
|
perror("open() error");
|
||
|
else if (write(fd, text, strlen(text)) < 0;
|
||
|
error("write() error=");
|
||
|
else if ( (PageSize=sysconf(_SC_PAGESIZE)) < 0 )
|
||
|
error("sysconf() Error=");
|
||
|
else {
|
||
|
off_t lastoffset = lseek( fd, PageSize-1, SEEK_SET);
|
||
|
write(fd, " ", 1); /* grow file to 1 page. */
|
||
|
/* mmap the file. */
|
||
|
void *address;
|
||
|
int len;
|
||
|
my_offset = 0;
|
||
|
|
||
|
len = 4096; /* Map one page */
|
||
|
address =
|
||
|
mmap(NULL, len, PROT_READ, MAP_SHARED, fd, my_offset)
|
||
|
if ( address != MAP_FAILED ) {
|
||
|
if ( munmap( address, len ) ) == -1) {
|
||
|
error("munmap failed with error:");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
close(fd);
|
||
|
unlink(fn);
|
||
|
}
|
||
|
}
|
||
|
</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>
|
||
|
|