In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the memory management malloc and mmap of the Linux process". In the daily operation, I believe that many people have doubts about how to use the memory management malloc and mmap of the Linux process. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use the memory management malloc and mmap of the Linux process." Next, please follow the editor to study!
Malloc
In the linux standard libc library, the implementation of the malloc function decides which allocation function to use according to the size of the allocated memory. When size is less than or equal to 128KB, brk allocation is called; when size is greater than 128KB, mmap is called to allocate memory. Size can be adjusted by the M_MMAP_THRESHOLD option. As shown below:
The process of sys_brk allocation is mainly to adjust the location of brk.
In the process of sys_mmap allocation, the main task is to find a free virtual memory in the middle of the heap and the stack (memory mapping segment).
Brk
Heap memory grows from low address to high address. When allocating memory, the highest address pointer mm- > brk of the heap segment is extended to the higher address. When you free memory, shrink mm- > brk to a low address.
After the completion of this application, only an area has been opened up, and physical memory is usually not allocated immediately. The allocation of physical memory will be processed after a page fault exception occurs during access. We will analyze this further in this follow-up article.
SYSCALL_DEFINE1 (brk, unsigned long, brk)
{
.
/ / all require page alignment to facilitate mapping. Mm- > brk can be understood as end_brk, that is, the end of the current process heap.
Newbrk = PAGE_ALIGN (brk)
Oldbrk = PAGE_ALIGN (mm- > brk)
If (oldbrk = = newbrk)
Goto set_brk
/ * Always allow shrinking brk. , /
If (brk brk) {
/ / A pair of heap shrinks. Calling free will satisfy this condition, reduce the heap and execute unmap.
If (! do_munmap (mm, newbrk, oldbrk-newbrk, & uf))
Goto set_brk
Goto out
}
/ * Check against existing mmap mappings. , /
Next = find_vma (mm, oldbrk)
If (next & & newbrk + PAGE_SIZE > vm_start_gap (next))
Goto out
/ * Ok, looks good-let it rip. , /
/ / A pair of heap extensions, which are the core of the brk function, in which a vma is created, and then instert in the global linked list
If (do_brk_flags (oldbrk, newbrk-oldbrk, 0, & uf)
< 0) goto out; set_brk: //设置这次请求的brk到进程描述符mm->In brk
Mm- > brk = brk
Populate = newbrk > oldbrk & & (mm- > def_flags & VM_LOCKED)! = 0
Up_write (& mm- > mmap_sem)
Userfaultfd_unmap_complete (mm, & uf)
If (populate)
Mm_populate (oldbrk, newbrk-oldbrk)
Return brk
Out:
Retval = mm- > brk
/ / release semaphore
Up_write (& mm- > mmap_sem)
Return retval
}
The general process is as follows:
Mmap
Private anonymous mapping: commonly used for memory allocation, heap, stack
Shared anonymous mapping: typically used for sharing memory between processes, creating / dev/zero devices in the in-memory file system
Private file mapping: usually used to load dynamic libraries, code snippets, data segments
Shared file mapping: commonly used for file reading and writing and interprocess communication
If you want to explain further, please refer to the article "Farewell".
Unsigned long do_mmap (struct file * file, unsigned long addr)
Unsigned long len, unsigned long prot
Unsigned long flags, vm_flags_t vm_flags
Unsigned long pgoff, unsigned long * populate
Struct list_head * uf)
{
.
/ / get unmapped area
Addr = get_unmapped_area (file, addr, len, pgoff, flags)
If (offset_in_page (addr))
Return addr
Addr = mmap_region (file, addr, len, vm_flags, pgoff, uf)
.
Return addr
}
The collation process is as follows:
At this point, the study on "memory management of Linux processes how to use malloc and mmap" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.