Linux MMAP memory ing

Source: Internet
Author: User

In Linux, MMAP and mmap2 are called to create a process address space ing. MMAP and mmap2 are commonly used in the following scenarios:

1. file ing common file I/O operations, read and write the memory after MMAP ing, you can complete file read and write operations, kernel and userspace operations are the same physical memory; while regular file read and write operations, you need to call the Read Write lseek interface cyclically. Each read/write operation requires kernel to userspace, or copy userspace to the memory of the kernel. Therefore, MMAP operations increase the file access speed. However, because MMAP ing occupies a large amount of memory, the amount of memory that other operations can use will be affected (although the kernel will reclaim the memory when the memory is insufficient ).

If the file to be mapped is large, it is best to perform field ing on the file. Although the linear address space of the Linux Process is large, it is still limited.

2. Use MMAP anonymous ing to set a special flag map_anonymous when calling MMAP. Anonymous ing can be used to allocate a large memory area, and userspace can call MMAP to allocate memory. In addition, when malloc is used to allocate memory in libc, if the applied memory is a small block of memory (such as smaller than a critical value), malloc will call BRK to allocate memory from the heap of the process address space; otherwise, MMAP/mmap2 will be called to allocate memory directly.

In the kernel, the implementation of BRK is actually a simplified do_mmap implementation. BRK assumes that the linear zone does not map files on the disk.

3. Shared Memory ing. It is mainly used for inter-process communication. Any modification to the ing is visible to other processes mapped to this file, and the modification results will be reflected in the following files. It can communicate with processes such as MPs queues and message queues to improve Memory Sharing efficiency.

MMAP and mmap2

The C standard library uses the uniform function entry MMAP function to create mappings. MMAP's C library implementation selects MMAP or mmap2 for actual ing creation.

At the kernel system call level, two systems call MMAP and mmap2. The difference between MMAP and mmap2 lies in the parameter offset. This parameter specifies the starting position of the file ing. For MMAP, the unit is bytes, and for mmap2, the unit is page_size.

Map_locked and map_unlocked

Map_locked makes the page mapped to the virtual address space of the process no longer in the missing page status. In some cases, you do not want to access the linear address to be switched to the swap space.

Map_unlocked works in the opposite way.

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.