Linux-MMAP function Introduction

Source: Internet
Author: User
MMAP function is a system call in Unix/Linux. Let's take a look at the introduction to MMAP in section 12.2 of UNIX netword programming:
The MMAP function maps either a file or a POSIX shared memory object into the address space of a process. We use this function for three purposes:
1. With a regular file to provide memory-mapped I/O
2. With special files to provide anonymous memory Mappings
3. With shm_open to provide POSIX shared memory between unrelated Processes

MMAP system calls are not designed completely for Memory Sharing. It provides different access methods for common files. A process can perform operations on common files like read/write memory. POSIX or System V's shared memory IPC is purely used for sharing purposes. Of course, MMAP ()'s shared memory is also one of its main applications.
MMAP system calls enable shared memory between processes by ing the same common file. After a common file is mapped to the process address space, the process can access the file like accessing the common memory without calling read (), write (), and other operations.

MMAP is widely used in our programs, and MMAP is used to access files like accessing common memory. It has been proved that calling MMAP is much faster than using conventional methods to frequently access a file and move the pointer back and forth.
Let's take a look at MMAP's definition:
Void * MMAP (void * ADDR, size_t Len, int Prot, int flags, int FD, off_t offset );

The FD parameter is the description of the file to be mapped to the process space. It is generally returned by open (). At the same time, FD can be specified as-1. In this case, map_anon must be specified in the flags parameter, it indicates that anonymous ing is performed (no specific file name is involved, avoiding File Creation and opening. Obviously, it can only be used for inter-process communication with kinship ).

Len is the number of bytes mapped to the address space of the calling process. It starts from the offset byte at the beginning of the mapped file.

The prot parameter specifies the access permission for the shared memory. The following values can be obtained: prot_read (readable), prot_write (writable), prot_exec (executable), and prot_none (inaccessible ).

Flags are specified by the following common values: map_shared, map_private, and map_fixed. Among them, map_shared and map_private are mandatory, while map_fixed is not recommended.
If map_shared is specified, the modifications made to the mapped memory also affect the file. If map_private is used, the modifications made to the mapped memory are only visible to this process and have no effect on the files.

The offset parameter is generally set to 0, indicating that the ing starts from the file header.

The ADDR parameter specifies that the file should be mapped to the starting address of the process space. Generally, a null pointer is specified. At this time, the task of selecting the starting address is left to the kernel for completion. The Return Value of the function is the address mapped from the last file to the process space. The starting address of a process operation can be the valid address of the value.

Take a look at the figure below (from section 2, section 12.2, Unix netword programming) to further impress MMAP:

Here, we will not detail MMAP parameters. You can refer to the MMAP manual page or section 12.2 of UNIX netword programming for further information. Finally, let's end this section with an example. As mentioned in section 4.2, the fileinformation array is written in binary form into a file named inforindex. The code is similar to this when you want to access the fileinformation array:
Struct stat st;
Char buffer = "inforindex ";
Fileinformation * _ fileinfoindexptr = NULL;
If (STAT (buffer, & St) <0)
{
Fprintf (stderr, "error to stat % s/n", buffer );
Exit (-1 );
} // MMAP the inforindex to _ fileinfoindexptr
Int FD = open (buffer, o_rdonly );
If (FD <0)
{
Printf ("error to open % s/n", buffer );
Exit (-1 );
}
_ Fileinfoindexptr = (fileinformation *) MMAP (null, st. st_size, prot_read, map_shared, FD, 0 );
If (map_failed ==_ fileinfoindexptr)
{
Printf ("error to MMAP % s/n", buffer );
Close (FD );
Exit (-1 );
}
Close (FD );

 

Address: http://hi.baidu.com/weichao_zju/blog/item/53224ed96a99442d10df9b5b.html

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.