当前位置:网站首页>NIO总结文——一篇读懂NIO整个流程
NIO总结文——一篇读懂NIO整个流程
2022-07-27 08:38:00 【芒骁】
概括NIO做了什么不难,讲清楚NIO如何具体实现的,那就有些难度了,原因是NIO实现过程中涉及的参数太多,许多参数看起来不是很重要,但是却在整个流程分析中不可缺少,往往整个过程分析下来还是没办法形成一个清晰的过程。
所以在分析的过程中先不直接从前往后具体分析,而是从宏观到微观分析
接下来将以NIO在Windows平台实现的具体细节展开,以后也会对linux平台进行分析(分析在windows平台的实现不好之处就是不开源,不知道IO多路复用如何实现的),不过也能把NIO的整个过程分析的差不多。
我们都知道NIO中有几个重要角色
Channel、Selector、SelectionKey
我们先来分析Selector的轮询功能实现。
Selector负责轮询,在Windows平台中交给它的子类SubSelector来负责。我们知道IO多路复用的实现有select、poll、epoll等实现方法,windows平台我们不知道具体如何实现,但是总的流程一定是和这些实现方法相仿

return this.poll0(WindowsSelectorImpl.this.pollWrapper.pollArrayAddress, Math.min(WindowsSelectorImpl.this.totalChannels, 1024), this.readFds, this.writeFds, this.exceptFds, WindowsSelectorImpl.this.timeout);
我们可以看到包括几个参数:
- WindowsSelectorImpl.this.pollWrapper.pollArrayAddress
- Math.min(WindowsSelectorImpl.this.totalChannels, 1024)
- this.readFds,
- this.writeFds,
- this.exceptFds,
- WindowsSelectorImpl.this.timeout)
我们不进行逐一分析,只需要了解用户空间通过调用poll0的底层方法来实现与内核空间的交互,也就是把这三个数组this.readFds, this.writeFds,this.exceptFds交给内核,内核具体的实现细节我们这里不分析,只需要知道内核会帮我们更新这几个数组来告诉我们哪些感兴趣的事件到来。
poll前
poll后
可以看到通过poll方法让内核帮我们监控感兴趣的事件到来,这时一个感兴趣的事件到来,是个可读事件,它的fdval是1228。
selector的轮询功能的第一步:从内核得到感兴趣事件的到来任务就这样实现了,Selector需要实现的第二步,将这个数据更新到我们的程序上。

通过上面的poll方法我们已经了解,内核会给我们返回三个更新过的数组,分别表示三类事件。

Selector先回对事件进行处理,比较是否是我们感兴趣的事件,然后将SelectionKeysImpl放入Selector专门存放的数据结构
之后就从该数据结构中取出SelectionKeysImpl专门进行处理
用一个去掉一个,直到把set中的全部
边栏推荐
- Breadth first search
- P7 Day1 get to know the flask framework
- Use of elastic box / expansion box (Flex)
- Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
- JS basic exercises
- Openresty + keepalived 实现负载均衡 + IPV6 验证
- Query and association of flask to database
- Vertical align cannot align the picture and text vertically
- 阿里云国际版回执消息简介与配置流程
- The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
猜你喜欢

OPPO 自研大规模知识图谱及其在数智工程中的应用

All in one 1251 - Fairy Island for medicine (breadth first search)

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

It's better to be full than delicious; It's better to be drunk than drunk

First experience of tryme in opengauss

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed

Sliding conflict of view

One book 1201 Fibonacci sequence

On Valentine's day, I drew an object with characters!

Cache consistency and memory barrier
随机推荐
Using ecological power, opengauss breaks through the performance bottleneck
4275. Dijkstra sequence
OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
STM32小bug汇总
Realize SPU management in the background
Minio 安装与使用
Vcenter7.0 managing esxi7.0 hosts
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
情人节,我用字符画出了一个对象!
How to merge multiple columns in an excel table into one column
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
Map structure
[penetration test tool sharing] [dnslog server building guidance]
Openresty + keepalived to achieve load balancing + IPv6 verification
3428. 放苹果
3311. Longest arithmetic
Flask login implementation
Alibaba cloud international receipt message introduction and configuration process
Openresty + keepalived 实现负载均衡 + IPV6 验证


