文章:https://mp.weixin.qq.com/s?__biz=MzI4OTU3ODk3NQ==&mid=2247501947&idx=1&sn=02dc6d3e8cf8d90f88f938eab210589c&chksm=ec2f82cbdb580bdd829ec0d0bf193d2ea97a14ce50bebd1c00d95defea614d5ebaba5672c590&scene=132#/wechat_redirect

Linux内核读和写

image.png

读操作

基于传统的 I/O 读取方式,read 系统调用会触发 2 次上下文切换,1 次 DMA 拷贝和 1 次 CPU 拷贝。
发起数据读取的流程如下:

  1. 用户进程通过 read() 函数向 Kernel 发起 System Call,上下文从 user space 切换为 kernel space。
  2. CPU 利用 DMA 控制器将数据从主存或硬盘拷贝到 kernel space 的读缓冲区(Read Buffer)。
  3. CPU 将读缓冲区(Read Buffer)中的数据拷贝到 user space 的用户缓冲区(User Buffer)。
  4. 上下文从 kernel space 切换回用户态(User Space),read 调用执行返回。

    写操作

    基于传统的 I/O 写入方式,write() 系统调用会触发 2 次上下文切换,1 次 CPU 拷贝和 1 次 DMA 拷贝。
    用户程序发送网络数据的流程如下:
    用户进程通过 write() 函数向 kernel 发起 System Call,上下文从 user space 切换为 kernel space。
    CPU 将用户缓冲区(User Buffer)中的数据拷贝到 kernel space 的网络缓冲区(Socket Buffer)。
    CPU 利用 DMA 控制器将数据从网络缓冲区(Socket Buffer)拷贝到 NIC 进行数据传输。
    上下文从 kernel space 切换回 user space,write 系统调用执行返回。

    高性能I/O技术

  5. 页缓存技术(Page Cache)

  6. 零拷贝技术
  7. 多路复用技术