Brk () and mmap () memory ing, brkmmap memory ing
Reference blog: http://www.cnblogs.com/huxiao-tee/p/4660352.html
- Virtual Address Space Distribution of linux processes on the x86 Platform (versions earlier than 2.6.7)
The mmap and stack regions are growing, and only 1 GB of continuous virtual address space is available.
- Virtual Address Space Distribution of linux processes on the x86 Platform (Versions later than 2.6.7)
Random stack offset: Because the address of the previous stack is fixed, it is easy to be attacked by stack overflow. Here, each stack has an offset.
RLIMIT_STACK: when the data capacity in the stack exceeds the stack capacity, page fault is triggered. If an exception occurs, the nearest virtual address space is detected and the abnormal address is adjacent to the stack, it will increase the stack size (generally 8 Mb ). If the stack is extended, the stack needle will not be shrunk when it is rolled back. If the stack overflow occurs, the segment fault will occur.
Memory Mapping Segment: Memory ing position, an efficient I/O, which will be described later.
- Heap operation functions brk () and sbrk ()
Int brk (void * addr );
Void sbrk (intptr_t increment );
In the kernel data structure mm_struct, start_brk is the starting address (the starting address of heap) dynamically allocated by the process, and brk is the last address of the heap.
First, program break is the position of the current brk, so it is the first position of heap after the data segment initialization ends, rather than the end of heap.
Sbrk () is a library function, and brk () is a system call. Compared with a library function, system call generally provides relatively simple work. It is to change the brk value to expand and contract the heap (when the increment is negative ).
1. Basic Concepts
Mmap is a memory ing method that maps a file or other objects to the address space of a process to implement a one-to-one correspondence between the file disk address and the virtual address of the process. Kernel space changes to this area are also directly reflected in the user space, to achieve file sharing of different processes.
The Linux kernel uses the vm_area_struct structure to represent an independent virtual memory area. A process uses multiple vm_area_struct to represent different types of virtual memory areas.
When the number of vm_area_struct instances is small, the structure is organized in the form of a single love table in ascending order. AVL Tree is used for many instances.
V
The mmap function creates a new vm_area_struct structure and connects it to the physical address.
2. mmap memory ing principle
There are three phases
- The process starts the ing process and creates a virtual ing area for the ing in the virtual address space.
1> the process calls mmap in the user space.
Prototype: void * mmap (void * start, size_t length, int prot, int flags, int fd, off_t offset );
2> In the virtual address space of the current process, find a continuous virtual address that meets the requirements of the space.
3> assign a vm_area_struct structure to the virtual area, and initialize the fields of the structure.
4> Insert the new virtual structure (vm_area_struct) into the linked list or several types of virtual address areas of the process.
- Call the kernel space system call function mmap (different from the user space) to implement a one-to-one ing between the physical address of the file and the virtual address of the process.
5> after a new virtual address area is assigned to the ing, find the corresponding file descriptor in the file descriptor table through the file pointer to be mapped and add it to the struct file.
6> the file_operation structure in linux defines the device driver functions corresponding to different events, including int mmap (struct file * filp, struct vm_area_struct * vma ), in fact, this function is used to connect the user space to the device memory, that is, access to the virtual address is converted into access to the device.
7> use the inode module to find the corresponding file, that is, the physical address of the disk.
8> Create a page table to map the file address and virtual address area. Only the ing relationship is established here, and there is no data corresponding to the physical address in the primary storage.
- The process initiates access to this ing space, leading to page-missing exceptions and copying the file content to the primary storage.
9> process read/write: by querying the page table, you can find that the address is no longer on the physical page, leading to a page missing exception.
10> identify page missing exceptions and apply for page Adjustment
11> first, check that no memory pages need to be accessed in the swap cache. If nopage is not called, load all the missing pages from the disk to the primary storage.
12> later, you can perform read and write operations, and there will be a delay of some time. Call msync () to update immediately.
3. Summary of mmap advantages
1> the read operations on files crossed the page cache, reducing the number of data copies and replacing the I/O operations with memory reads, improving the File Read efficiency.
2> efficient interaction between user space and kernel space
3> shared memory and communication between processes
4> efficient large-scale data transmission
High-end memory ing with kernel: https://www.cnblogs.com/wuchanming/p/4360277.html