Linux memory-mapped function mmap () function detailed

Source: Internet
Author: User
Tags requires

I. Overview

Memory mapping, in short, is to map a section of memory area of user space to the kernel space, after the successful mapping, user changes to this area of memory can be directly reflected to the kernel space, the same, the kernel space for this section of the changes also directly reflect the user space. The efficiency is very high for both kernel space <----> user space, which requires a large amount of data transfer.

The following is a schematic diagram of an area of memory that maps universal files to user space.

Figure I:

Second, the basic function

The MMAP function is a system call under Unix/linux, and the details can refer to "Unix Netword Programming" Vol. 12.2 section.

Mmap system calls are not designed entirely for shared memory. It itself provides a different way of accessing normal files, and processes can manipulate normal files like memory. The shared memory IPC for POSIX or System V is purely for shared purposes, and of course mmap () is one of the main applications of shared memory.

Mmap system calls enable shared memory to be realized by mapping the same common file between processes. After the normal file is mapped to the process address space, the process can access the file like normal memory without having to call read (), write (), and so on. Mmap does not allocate space, but maps files to the address space of the calling process (but takes up your virutal memory), and then you can write files with memcpy and so on without write () . After writing, the contents of the memory are not immediately updated to the file, but there is a time delay, you can call Msync () to explicitly sync, so that you can write content immediately saved to the file. This should be related to the driver. However, by Mmap to write a file this way can not increase the length of the file, because the length of the mapping is called mmap () when the decision. If you want to cancel the memory map, call Munmap () to cancel the memory map

<span style= "Background-color:rgb (255, 255, 255);" >void * mmap (void *start, size_t length, int prot, int flags, int fd, off_t offset) </span>

Mmap is used to map files to memory space, and simply mmap is to make an image of the contents of a file in memory. After the success of the mapping, the user changes to the memory area can be directly reflected to the kernel space, the same, the kernel space changes to this area directly reflect the user space. The efficiency is very high for both kernel space <----> user space, which requires a large amount of data transfer.

Start: The starting address of the memory area to map to, usually null (0). Null indicates that the memory address is specified by the kernel

Length: The size of the memory area to map

Prot: The desired memory protection flag cannot conflict with the open mode of the file. is one of the following values that can be reasonably grouped together by or operation  
prot_exec//page content can be executed  
prot_read//page content can be  read  
prot_write//page can be written  
prot_ NONE  //Page not accessible

Flags: Specifies the type of mapping object, whether the mapping options and the mapping page can be shared. Its value can be a combination of one or more of the following bits

Map_fixed: Using the specified mapping start address, if the memory area specified by the start and Len parameters overlaps the existing mapping space, the overlapping portions are discarded. If the specified starting address is not available, the operation will fail. And the starting address must fall on the page boundary.

Map_shared: Write data to the mapped region is copied back to the file, and other processes that map the file are shared.

Map_private: Creates a private mapping of a write-time copy. Memory area writes do not affect the original file. This flag is mutually exclusive with the above flags and only one of them can be used.

Map_denywrite: This flag is ignored.

Map_executable: Ditto

Map_noreserve: Do not reserve swap space for this mapping. When the swap space is retained, the modification of the mapping area may be guaranteed. When the swap space is not preserved and the memory is low, modifications to the mapping area can cause an offending signal.

map_locked: Locks the page of the mapping area to prevent the page from being swapped out of memory.

Map_growsdown: For the stack, tell the kernel VM system that the mapping area can be scaled down.

Map_anonymous: Anonymous mapping, the mapping area is not associated with any files.

Map_anon:map_anonymous's nickname is no longer used.

Map_file: Compatible flag, ignored.

Map_32bit: The mapping area is ignored when it is specified in the low 2gb,map_fixed of the process address space. The current logo is supported only on the X86-64 platform.

Map_populate: Prepares the page table for file mapping by prefetching. Subsequent access to the map area will not be blocked by the page violation.

Map_nonblock: It makes sense only when used with Map_populate. Does not perform prefetching, only creating page table entries for pages that already exist in memory.

FD: File descriptor (returned by the Open function)

Offset: Indicates that the mapped object (that is, the file) starts from there, usually with 0. The value should be an integer multiple of the size page_size

This article URL address: http://www.bianceng.cn/OS/Linux/201410/45413.htm

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.