#include <sys/types.h> #include <sys/mman.h> int munmap ( void *addr, size_t len );Service Program Name: QP0LLIB1
The munmap() function removes addressability to a range of memory mapped pages of a process's address space. All pages starting with addr and continuing for a length of len bytes are removed.
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 mmap() function, are not affected by the munmap() function.
If the mapping was created MAP_PRIVATE then any private altered pages are discarded and the system storage associated with the copies are returned to the system free space.
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 munmap() function, then the file becomes unreferenced. If the file becomes unreferenced due to an munmap() function call and the file is no longer linked, then the file will be deleted.
The addr parameter must be a multiple of the page size. The value
zero or NULL is not a valid starting address. The sysconf()
function may be used to determine the system page size.
No authorization is required.
Upon successful completion, the munmap() function returns 0. Upon failure, -1 is returned and errno is set to the appropriate error number.
When the munmap() function fails, it returns -1 and sets errno as follows.
Error condition | Additional information |
---|---|
[EINVAL] |
For example, for munmap() this may mean that the address range from addr and continuing for a length of len is outside the valid range allowed for a process. This error may also indicate that the value for the addr parameter is not a multiple of the page size. A value of 0 for parameter len also will result in this error number. |
[ENOTAVAIL] | |
[EUNKNOWN] |
The following messages may be sent from this function.
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
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.
See Code disclaimer information for information pertaining to code examples.
The following example creates a file, produces a memory mapping of the file using mmap(), and then removes the mapping using the munmap() function.
#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); } }
Top | UNIX-Type APIs | APIs by category |