- 当指定的虚拟内存区域被加入到进程虚拟内存空间中时 , open 函数会被调用
- 当虚拟内存区域 VMA 从进程虚拟内存空间中被删除时 , close 函数会被调用
- 当进程访问虚拟内存时 , 访问的页面不在物理内存中 , 可能是未分配物理内存也可能是被置换到磁盘中 , 这时就会产生缺页异常 , fault 函数就会被调用 。
- 当一个只读的页面将要变为可写时 , page_mkwrite 函数会被调用 。
内核中这种类似的用法其实有很多 , 在内核中每个特定领域的描述符都会定义相关的操作 。比如在前边的文章 《从 Linux 内核角度探秘 JDK NIO 文件读写本质》 中我们介绍到内核中的文件描述符 struct file 中定义的 struct file_operations*f_op 。里面定义了内核针对文件操作的函数指针 , 具体的实现根据不同的文件类型有所不同 。
针对 Socket 文件类型 , 这里的 file_operations 指向的是 socket_file_ops 。

文章插图
在 ext4 文件系统中管理的文件对应的 file_operations 指向 ext4_file_operations , 专门用于操作 ext4 文件系统中的文件 。还有针对 page cache 页高速缓存相关操作定义的 address_space_operations。

文章插图
还有我们在 《从 Linux 内核角度看 IO 模型的演变》一文中介绍到 , socket 相关的操作接口定义在 inet_stream_ops 函数集合中 , 负责对上给用户提供接口 。而 socket 与内核协议栈之间的操作接口定义在 struct sock 中的 sk_prot 指针上 , 这里指向 tcp_prot 协议操作函数集合 。

文章插图
对 socket 发起的系统 IO 调用时 , 在内核中首先会调用 socket 的文件结构 struct file 中的 file_operations 文件操作集合 , 然后调用 struct socket 中的 ops 指向的 inet_stream_opssocket 操作函数 , 最终调用到 struct sock 中 sk_prot 指针指向的 tcp_prot 内核协议栈操作函数接口集合 。
5.7 虚拟内存区域在内核中是如何被组织的在上一小节中 , 我们介绍了内核中用来表示虚拟内存区域 VMA 的结构体 struct vm_area_struct , 并详细为大家剖析了 struct vm_area_struct 中的一些重要的关键属性 。
现在我们已经熟悉了这些虚拟内存区域 , 那么接下来的问题就是在内核中这些虚拟内存区域是如何被组织的呢?

文章插图
我们继续来到 struct vm_area_struct 结构中 , 来看一下与组织结构相关的一些属性:
struct vm_area_struct { struct vm_area_struct *vm_next, *vm_prev; struct rb_node vm_rb;struct list_head anon_vma_chain; struct mm_struct *vm_mm; /* The address space we belong to. */unsigned long vm_start;/* Our start address within vm_mm. */unsigned long vm_end;/* The first byte after our end addresswithin vm_mm. *//** Access permissions of this VMA.*/pgprot_t vm_page_prot;unsigned long vm_flags;struct anon_vma *anon_vma;/* Serialized by page_table_lock */struct file * vm_file;/* File we map to (can be NULL). */unsigned long vm_pgoff;/* Offset (within vm_file) in PAGE_SIZEunits */void * vm_private_data;/* was vm_pte (shared mem) *//* Function pointers to deal with this struct. */const struct vm_operations_struct *vm_ops;}在内核中其实是通过一个 struct vm_area_struct 结构的双向链表将虚拟内存空间中的这些虚拟内存区域 VMA 串联起来的 。
经验总结扩展阅读
- 神话中高要一步一步往上爬是第几集?
- 神话一步一步爬到最高是几集?
- 总以为她会永远陪我一步一步慢慢走是什么歌
- 爱情不会一步到位而是会经过颇多挫折才圆满的星座
- Dubbo 03: 直连式 + 接口工程
- 一篇文章带你了解热门版本控制系统——Git
- 一篇文章带你了解网页框架——Vue简单入门
- 一直在等待爱却不敢主动迈出一步的星座
- 我用canvas带你看一场流星雨
- 一篇文章带你掌握MyBatis简化框架——MyBatisPlus
