The MMAP method maps files to the memory.

Source: Internet
Author: User

Note: The sys/Mman. h header file does not have to be passed in Linux in windows.

Method Introduction:

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.

Void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset); the parameters are described below.

A simple example is as follows:

/** Method Introduction: void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset ); * map files to memory */# include <unistd. h> # include <iostream> # include <sys/Mman. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> using namespace STD; int main () {int FP; If (FP = open (". /leeboy.txt ", o_rdonly) <0) {cout <" error "<Endl; return 0;} void * fpbuf = MMAP (0, 1024, prot_read, map_shared, FP, 0); cout <(char *) fpbuf <Endl; // output the content in the file return 0 ;}

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.

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.