当前位置:网站首页>io模型初探
io模型初探
2022-06-28 17:45:00 【MrPeng1991】
1. IO系列——io模型初探
2.IO系列——select/poll/epoll
1.什么是IO
操作系统负责计算机的资源管理和进程调度。应用需要经过操作系统,才能做一些特殊操作,如磁盘读写,内存读写等。
应用程序要把数据写入磁盘,只能通过调用操作系统开放出来的API来操作。
应用程序在用户空间,不存在实质的io过程,真正的io在操作系统执行,应用程序发起一次IO操作包含两个阶段:
- IO调用 :应用程序向操作系统内核发起调用
- IO执行: 操作系统内核完成IO操作
操作系统内核完成IO操作还包括两个过程:
- 准备数据阶段:内核等待I/O设备准备号数据
- 拷贝数据阶段:将数据从内核缓冲区,拷贝到用户进程缓冲区

其实io就是把进程内部数据转移到外部设备,或者把外部设备的数据迁移到进程内部。外部设备一般指 硬盘,socket通讯的网卡。一个完整的io过程包括这几个步骤
- 应用向操作系统发起io调用请求
- 操作系统准备数据,把io外部设备的数据,加载到内核缓冲区
- 操作系统拷贝数据,即将内核缓冲区的数据,拷贝到用户进程缓冲区
3. IO模型
3.1 阻塞io
知道什么是io了,什么是阻塞io呢?
应用程序进程发起io调用,如果内核的数据还没有准备好的话,应用程序进程就一直在阻塞等待,一直等内核数据准备好了,从内核拷贝到用户空间,才返回成功提示,此次io操作,称之为阻塞io
- 阻塞IO比较经典的应用就是阻塞socket、Java BIO。
- 缺点:内核数据一直没准备好,用户进程一直阻塞,浪费性能,
3.2 非阻塞io
内核数据没准备好,可以先返回错误信息给用户进程,让他不需要等待,而是通过轮询的方式再来请求,这就是非阻塞io

非阻塞IO的流程如下:
应用进程向操作系统内核,发起recvfrom读取数据。
操作系统内核数据没有准备好,立即返回EWOULDBLOCK错误码。
应用程序进程轮询调用,继续向操作系统内核发起recvfrom读取数据。
操作系统内核数据准备好了,从内核缓冲区拷贝到用户空间。
完成调用,返回成功提示。
即NIO,即non-blocking io,
缺点:相对于阻塞io,虽然大幅度提升性能,频繁的轮询,导致频繁的系统调用,同事会消耗大量的cpu资源,可以考虑io复用模型。
3.3 io多路复用模型
既然nio无效轮询导致cpu资源消耗,等我们内核数据准备好了,主动通知应用进程再去进行系统调用
文件描述符 fd File Descriptor,是计算机科学一个术语,形式上是一个非负整数,当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。
io复用模型核心思路:系统给我们提供一类函数(select poll epoll),可以同时监控多个fd操作。任何一个返回内核数据就绪,应用进程再发起recvfrom系统调用
3.3.1 多路复用之select
应用进程通过调用select函数,可以同时监控多个fd,在select函数监控的fd中,只要有任何一个数据装填准备就绪了,select函数就会返回可读状态。这时应用进程再发起recvfrom请求去读取数据

非阻塞IO(NIO)模型中,需要N此轮询系统调用,然而借助select的IO多路复用模型,只需要发起一次询问就够了,大大优化了性能,但是有缺点:
- IO最大连接数有限,linux系统上一般1024
- select函数返回后,是通过遍历fdset,找到就绪的描述符fd,(仅知道I/O事件发生,却不知道哪几个流,所以遍历所有流)
3.4.2 poll
与select相比,poll解决了连接数限制问题,但是select和poll一样,需要通过遍历文件描述符来获取已经就绪的socket,如果同时连接大量客户端,在一时刻只能只有极少数处于就绪状态,伴随监视的描述符数量增长,效率也会线性下降。因此epoll诞生了
3.4.3 epoll
epoll采用事件驱动来实现

epoll通过epoll_ctl()来注册一个fd,一旦基于某个fd就绪时,内核会采用回调机制,迅速激活这个fd,当进程调用epoll_wait()时便得到通知,这里去嗲了遍历文件描述符这个操作,而是采用监听事件回调的机制,这就是epoll的亮点。

3.4 io模型之信号驱动
信号驱动IO不再用主动询问的方式去确认数据是否就绪,而是向内核发送要给信号(调用sigaction的时候建立一个SIGIO的信号)再通过SIGIO信号通知应用进程,数据准备好后的可读状态,应用用户进程收到信号之后,立即调用recvfrom去读取数据

信号驱动IO模型,在应用进程发出信号后,是立即返回的,不会阻塞进程。它已经有异步操作的感觉了。但是你细看上面的流程图,发现数据复制到应用缓冲的时候,应用进程还是阻塞的。回过头来看下,不管是BIO,还是NIO,还是信号驱动,在数据从内核复制到应用缓冲的时候,都是阻塞的。还有没有优化方案呢?AIO(真正的异步IO)!
3.5 io模型 异步IO AIO
前面讲的BIO,NIO和信号驱动,在数据从内核复制到应用缓冲的时候,都是阻塞的,因此都不算是真正的异步。AIO实现了IO全流程的非阻塞,就是应用进程发出系统调用后,是立即返回的,但是立即返回的不是处理结果,而是表示提交成功类似的意思。等内核数据准备好,将数据拷贝到用户进程缓冲区,发送信号通知用户进程IO操作执行完毕。

异步IO的优化思路很简单,只需要向内核发送一次请求,就可以完成数据状态询问和数据拷贝的所有操作,并且不用阻塞等待结果,

边栏推荐
- Home based efficient remote office | community essay solicitation
- Win10 compiles curl library source code with cmake3.22 and vs2019 and calls
- WSUS client access server exception error -0x8024401f "suggestions collection"
- Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference
- Small program graduation design based on wechat driving school examination small program graduation design opening report function reference
- Small program graduation project based on wechat examination small program graduation project opening report function reference
- Small program graduation project based on wechat recruitment small program graduation project opening report function reference
- nuc980心跳灯
- Common DOS commands
- 7-user input and while loop
猜你喜欢

MySQL十种锁,一篇文章带你全解析

DNSLog注入

WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
![[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto

Currency circle earthquake: earned 1million last year and lost 5million this year

The fourth largest operator cannot be a "catfish"

Exploration and practice of reinforcement learning in yellow page merchants' intelligent chat assistant

Architecture autonomy service: building data-driven architecture insight

2022危险化学品经营单位主要负责人复训题库及在线模拟考试

Anesthesia is not as simple as "one injection". Painless, safe and comfortable anesthesia is the first choice for patients
随机推荐
Dpdk 20.11 compiling, installing and running program
9 excellent bitmap services
Matlb| visual learning (plot and bar)
Leetcode 6. Zigzag transformation (awesome, solved)
Google launches advanced API security to protect APIs from security threats
为什么 insert 配置 'SELECT LAST_INSERT_ID()' 返回个0呢?
DNSLog注入
Small program graduation project based on wechat campus lost and found graduation project opening report function reference
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
Go descending sort takes top n
你们采集oracle数据,数据延迟大约有多少啊?我这边就维持在3秒了,降不下去了。有没有个业内参考啊
单片机修改网络硬件驱动(phy芯片更换)
Nuc980 heartbeat light
2022 recurrent training question bank and online simulation examination for main principals of hazardous chemicals business units
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
nuc980心跳灯
Redis principle - hash
July2022 plan (unreal)
Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference
Does DMS SQL result set export support parameter transfer?