From: http://blog.csdn.net/scorpio16/article/details/2059623
The second volume of UNIX Network Programming inter-process communication describes MMAP functions. This function has three main purposes:
1. Map a common file to the memory, which is usually used when files need to be read and written frequently. In this way, I/O reading and writing is replaced with memory reading and writing to achieve high performance;
2. Anonymous memory ing of special files can provide shared memory space for associated processes;
3. Provide shared memory space for unrelated processes. A common file is also mapped to the memory.
Function: void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offsize );
Start: the start address of the memory to be mapped. It is usually set to null, indicating that the system automatically selects the address. The address is returned after the ing is successful.
Parameter length: maps a large part of the file to the memory.
Parameter prot: the protection mode of the ing area. It can be a combination of the following methods:
The prot_exec ing area can be executed.
The prot_read ing area can be read.
The prot_write ing area can be written.
The prot_none ing area cannot be accessed.
The flags parameter affects the features of the ing area. You must specify map_shared or map_private when calling MMAP.
Map_fixed if the address specified by the start parameter cannot be mapped successfully, the ing is abandoned and the address is not corrected. This flag is generally not encouraged.
Map_shared copies data written to the ing area back to the file and allows other processes mapped to the file to share the data.
Map_private write operations on the ing area will generate a copy of The ing file, that is, private copy on write) no modifications made to this region will be written back to the original file content.
Map_anonymous creates an anonymous ing. The FD parameter is ignored, and files are not involved, and the ing area cannot be shared with other processes.
Map_denywrite only allows write operations on the ing area. Other operations that directly write data to the file will be rejected.
Map_locked locks the ing region, which means the region will not be replaced (SWAp ).
Parameter FD: The file descriptor to be mapped to the memory. If anonymous memory ing is used, map_anonymous is set in flags, and FD is set to-1. Some systems do not support anonymous memory ing. You can use fopen to open the/dev/Zero file and map the file to achieve the same effect of anonymous memory ing.
Offset: the offset of the file ing. It is usually set to 0, which indicates the correspondence starting from the beginning of the file. offset must be an integer multiple of the page size.
Return Value:
If the ing is successful, the start address of memory in the ing area is returned. Otherwise, map_failed (-1) is returned. The error cause is stored in errno.
Error code:
The ebadf parameter FD is not a valid file description.
The eacces access permission is incorrect. If map_private is used, the file must be readable. If map_shared is used, prot_write is required and the file must be writable.
Either start, length, or offset of the einval parameter is invalid.
The eagain file is locked or too much memory is locked.
Insufficient enomem memory.
The system calls MMAP () to share memory in two ways:
(1) Use the memory ing provided by common files:
Applicable to any process. In this case, you need to open or create a file and then call MMAP ()
The typical Call Code is as follows:
FD = open (name, flag, mode); If (FD <0 )...
PTR = MMAP (null, Len, prot_read | prot_write, map_shared, FD, 0 );
MMAP () provides many features and notes for shared memory communication. For more information, see the second volume of UNIX network programming.
(2) use special files to provide anonymous memory ing:
Applicable to kinship processes. Because of the special kinship between the parent and child processes, MMAP () is called first in the parent process, and fork () is then called (). After fork () is called, the child process inherits the address space after the anonymous ing of the parent process, and also inherits the address returned by MMAP, the Parent and Child processes can communicate with each other through the ing zone. Note that this is not a general inheritance relationship. Generally, child processes independently maintain some variables inherited from the parent process. The address returned by MMAP () is maintained by the parent and child processes. The best way to achieve shared memory for unrelated processes is to use anonymous memory ing. In this case, you do not need to specify a specific file. You only need to set the corresponding flag.
MMAP function usage