趣谈Linux操作系统学习笔记-内存管理(25讲)--内存映射上

时光总嘲笑我的痴心妄想 提交于 2020-02-16 13:49:06

mmap 的原理

每一个进程都有一个列表 vm_area_struct

 1 struct mm_struct {
 2   struct vm_area_struct *mmap;    /* list of VMAs */
 3 ......
 4 }
 5 
 6 
 7 struct vm_area_struct {
 8   /*
 9    * For areas with an address space and backing store,
10    * linkage into the address_space->i_mmap interval tree.
11    */
12   struct {
13     struct rb_node rb;
14     unsigned long rb_subtree_last;
15   } shared;
16 
17  /*
18    * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
19    * list, after a COW of one of the file pages.  A MAP_SHARED vma
20    * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
21    * or brk vma (with NULL file) can only be in an anon_vma list.
22    */
23   struct list_head anon_vma_chain; /* Serialized by mmap_sem &
24             * page_table_lock */
25   struct anon_vma *anon_vma;  /* Serialized by page_table_lock */
26 
27 
28 
29 
30   /* Function pointers to deal with this struct. */
31   const struct vm_operations_struct *vm_ops;
32   /* Information about our backing store: */
33   unsigned long vm_pgoff;    /* Offset (within vm_file) in PAGE_SIZE
34              units */
35   struct file * vm_file;    /* File we map to (can be NULL). */
36   void * vm_private_data;    /* was vm_pte (shared mem) */

内存映射不仅仅是物理内存和虚拟内存之间的映射,还包括将文件中的内容映射到虚拟内存空间。

这个时候,访问内存空间就能够访问到文件里面的数据。

而仅有物理内存和虚拟内存的映射,是一种特殊情况

 

申请小块内存 : brk。brk 函数之前已经解析过了,这里就不多说了。

申请一大块内存,就要用 mmap。对于堆的申请来讲,mmap 是映射内存空间到物理内存

mmap 的原理
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!