File map io (mmap-mprotect-msync-munmap) functions

Source: Internet
Author: User

This article describes the POSIX environment uses file mapping IO method of operation, file mapping IO also known as storage mapping IO , for ordinary files, many times it is efficient, it actually reduces the duplication of data, and it can also be used in special places for communication between processes, a way of sharing memory.


We can think of a file as a continuous piece of data, from a purely data point of view, which can be understood by any ordinary document. The file map actually corresponds to a piece of memory in our program, and when we manipulate the memory, it looks like it's actually manipulating the file. This is the concept of file mapping. This concept is great, it directly avoids a layer of data replication between the kernel and the user, so many times it will be faster than the other way of file operation, especially for ordinary disk files.


To create a map area: mmap

we pass the function mmap to tell the operating system which file to map which block of memory, and set we may not be able to manipulate this memory, that is, the same as the file.

#include <sys/mman.h>
void*mmap(void*addr,size_tLen,intPort,intFlag,intfiledes, off_t off)

return value: The mapped memory address was successfully returned, failed to returnmap_fialed

Parameters Addr

This is only in rare cases 0 , this parameter tells the kernel to use Addr specifies the value to map the specified file. When specified as 0 , tells the kernel what address to return within its own decision. It is always undesirable to specify this value manually unless you have a good understanding of the system process pattern or are familiar with the current environment.

Parameters Len

Specifies the length of the mapped memory area.

parameters  port

open The permission bit of the function, which we can specify as: prot_read prot_write prot_exec prot_none open open port_write

parameters  flag

This parameter specifies some other properties of the map area, and the properties of the permission are already Port specified in the. The typical values that may exist here are:map_fixed, for the addr Property, if this bit is specified, then it is often undesirable to require the system to map at the specified address; Map_shared, this flag indicates that the specified map area is shared, meaning that the operation of the memory is relative to the operation of the file, and it cannot be used with the map_private flag. Because they are expressing the opposite;map_private, this flag indicates that the map area is private, at which time the mapped memory can only be used by the current mileage, when the memory of the process operation will produce a copy of the original file.

map_fixed//using the specified mapping start address, if the memory area specified by the start and Len parameters overlap the existing mapping space, the overlapping portions will be discarded. If the specified start address is not available, the operation will fail. And the start address must fall on the boundary of the page. map_shared//share the mapping space with all other processes that map this object. Writes to the shared area are equivalent to outputting to a file. The file is not actually updated until Msync () or Munmap () is called. map_private//Create a private mapping of a write-time copy. The write to the memory area does not affect the original file. This flag is mutually exclusive to the above logo and can only be used with one. Map_denywrite//This flag is ignored. map_executable//Ibid .Map_noreserve//Do not reserve swap space for this mapping. When the swap space is preserved, modifications to the map area may be guaranteed. When the swap space is not preserved and the memory is low, modifications to the mapping area can cause a segment violation signal. map_locked//Locks the page of the map area, thus preventing the page from being swapped out of memory. Map_growsdown//For the stack, telling the kernel VM system that the mapping area can be scaled down. map_anonymous//anonymous mapping, the map area is not associated with any files. Map_anon//map_anonymous's nickname, no longer used. Map_file//compatible flag, ignored. map_32bit//The mapping area is ignored when the low 2gb,map_fixed of the process address space is specified. Currently this flag is only supported on the X86-64 platform. Map_populate//Prepare the page table for file mapping by pre-reading. Subsequent access to the map area is not blocked by page violations. Map_nonblock//is only meaningful when used with map_populate. Does not perform a read-ahead, only creates a page table entry for a page that already exists in memory.



FD: A valid file description word. It is generally returned by the open () function, and its value can also be set to-1, at which point the Map_anon in the flags parameter needs to be specified to indicate that an anonymous mapping is in progress. Off_toffset: The starting point for the content of the mapped object.


to set up and synchronize a map area: Mprotect , Msync

in the mmap we have a number of options to control the last obtained map area of some properties, in the call mmap function, I can still make adjustments to some of these properties by Mprotect function is complete. Also, after we have updated the contents of the memory, we may want to synchronize the contents to the file on disk, which is done by the msync function.

Mprotect function to change the access rights of a mapped area that already exists.

#include <sys/mman.h>
int   Mprotect ( void * &NBSP;ADDR,&NBSP; size_t &NBSP;LEN,&NBSP; int  port )

return value: Successfully returned 0 - 1

Parameters Addr

This parameter is mmap the value returned, at which point it is Mprotect range of functions.

Parameters Len

Specifies the length of the map area, which needs to be mmap the same as specified in.

Parameters Port

in the above we have introduced Port the possible values, Mprotect function is to put this Port the specified property is applied to the corresponding map area.

The content in the map area is updated, the kernel is not the real-time synchronization map area and the file, but the kernel is seldom active to synchronize, unless we call the function msync

#include <sys/mman.h>
int   Msync ( void * &NBSP;ADDR,&NBSP; size_t &NBSP;LEN,&NBSP; int  flags )

return: function is 0 - 1

Parameters Addr with the Len

These two parameters are exactly the same as Mprotect The corresponding parameters in the.

Parameters Flags

we can do this by specifying Flags The appropriate synchronization operations for the kernel are required for different values: Ms_async , which actually does not require the kernel to do anything, let the kernel autonomously to perform synchronization; Ms_sync , requiring the kernel to complete the write operation before returning; ms_invalidate is an optional flag that tells the kernel to discard parts that are not synchronized.


unmap Area: Munmap

in process find out or we call munmap

#include < Sys/mman.h>
int  munmap ( void * &NBSP;ADDR,&NBSP; size_t  len )

return: successful return 0 - 1

Munmap The parameter meanings are obvious and trivial, so they are not described here, they are exactly the same as the previous meanings.


Note: Signals, processes, pages, files

operation on the map area may cause two signals: sigsegv   sigbus SIGSEGV sigbus signal, which can be used normally before again accessing the area, but once again, both will receive the signal sigbus Span style= "Color:black". So the general situation is to call before mapping such as lseek+write

Process Call Fork The child process copies the address space of the parent process when a class of functions, so the mapped area is also mapped, so the mapping area is shared by the parent-child process. The memory sharing and communication of parent-child processes can be realized in this way.

from the kernel point of view, the memory is managed by the page, so when the map is mapped, the kernel is rounded up by the page on a specified size basis. The size of the page is System-dependent and can be determined by invoking the sysconf function in a POSIX environment .

only when calling mmap when you specify map_shared the contents of the file will change, otherwise the contents of the file will not be synchronized, even if we call the Msync or de-mapping or process termination.


File map io (mmap-mprotect-msync-munmap) functions

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.