MMAP/munmap Functions

Source: Internet
Author: User
Function Description:
MMAP maps a file or other objects to the memory. The file is mapped to multiple pages. If the file size is not the sum of the sizes of all pages, the unused space of the last page will be cleared. Munmap performs the opposite operation to delete the object ing of a specific address area.

File-based ing, the st_atime of the mapped file may be updated at any time during MMAP and munmap execution. If the st_atime field is not updated in the preceding circumstances, the value of this field will be updated when the first page index of the ing area is updated for the first time. The file ing established by using the prot_write and map_shared flag. The st_ctime and st_mtime
After writing to the ing area, it is updated before msync () is called using the ms_sync and ms_async flags.

Usage:
# Include <sys/Mman. h>

Void * MMAP (void * Start, size_t length, int Prot, int flags,
Int FD, off_t offset );

Int munmap (void * Start, size_t length );

Parameters:
Start: Start address of the ing area.
Length: the length of the ing area.
Prot: The expected memory protection flag. It cannot conflict with the file opening mode. Is a value of the following, which can be reasonably combined by the or operation

Prot_exec // page content can be executed
Prot_read // The page content can be read
Prot_write // page can be written
Prot_none // page inaccessible

Flags: specifies the type of the ing object, and whether the ing options and ing pages can be shared. Its value can be a combination of one or more of the following digits.

Map_fixed // use the specified ing start address. If the memory zone specified by the start and Len parameters overlaps the existing ing space, the overlapping part will be discarded. If the specified starting address is unavailable, the Operation will fail. And the start address must be on the boundary of the page.
Map_shared // share the ing space with all other processes mapped to this object. Writing to a shared area is equivalent to outputting data to a file. Until msync () or munmap () is called, the file will not actually be updated.
Map_private // create a private ing for copying when writing. Writing in the memory area does not affect the original file. This flag is mutually exclusive with the above one and can only be used.
Map_denywrite // this flag is ignored.
Map_executable // same as above
Map_noreserve // do not reserve swap space for this ing. When the swap space is retained, modification to the ing area may be guaranteed. When the swap space is not retained and the memory is insufficient, modifying the ing area will cause a segment violation signal.
Map_locked // lock the page in the ing area to prevent the page from being swapped out of memory.
Map_growsdown // used for the stack to tell the kernel VM system that the ing area can be extended down.
Map_anonymous // anonymous ing. The ing area is not associated with any files.
The alias map_anon // map_anonymous, which is no longer used.
Map_file // compatible flag, ignored.
Map_32bit // The ing area is 2 GB lower than the process address space. map_fixed indicates that the ing area is ignored. Currently this flag is supported only on the x86-64 platform.
Map_populate // prepare the page table by pre-reading for file ing. Subsequent access to the ing area will not be blocked by PAGE violations.
Map_nonblock // It is meaningful only when used with map_populate. Do not execute pre-read. only create a page table portal for pages that already exist in the memory.

FD: a valid file description. If map_anonymous is set, the value must be-1 for compatibility issues.
Offset: the starting point of the mapped object content.



Return description:
Upon successful execution, MMAP () returns the pointer to the ing area, and munmap () returns 0. When a failure occurs, MMAP () returns map_failed [the value is (void *)-1], and munmap returns-1. Errno is set to a value below
Eacces: Access Error
Eagain: The file is locked or too much memory is locked.
Ebadf: FD is not a valid file description.
Einval: one or more parameters are invalid.
Enfile: the system has reached the limit on opening files.
Enodev: The file system where the specified file is located does not support memory ing.
Enomem: the memory is insufficient, or the process has exceeded the maximum memory ing.
Eperm: insufficient power, operation not allowed
Etxtbsy: used to open a file in the write mode, and specify the map_denywrite flag.
SIGSEGV: Try to write data to the read-only zone
Sigbus: Try to access a memory area that does not belong to the Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.