当前位置:网站首页>[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
2022-06-25 10:19:00 【Mu Xin】
List of articles
1. I/O Model
1.1 User process and system process
In understanding I/O Before the process of operation , Let's first introduce what are user processes and system processes :
- User process : A process produced by executing a user process or a program outside the kernel , This kind of process can run or shut down under the control of the user .
- System processes : It can perform management operations such as memory resource allocation and process switching , System processes generally cannot be controlled with user permissions .
The area where the user process is located is called the user space , The area where the system process is located is called the kernel space .
User processes cannot directly access system data or resources , He has no way to get resources directly from kernel space , You need to copy data from kernel space to user space , Then get the data in the user space . Programs in user mode can only access user space , Only programs in kernel state can access user space and kernel space .
If the user process needs to access system resources or use system data , The interface provided by the operating system must be called . The process of calling the interface is a way for user mode processes to actively switch from user mode to kernel mode , That is, what we usually call a system call .
1.2 once I/O What happened to the operation ?
Take a data reading as an example , Regardless of disk I/O Or network? I/O, The first step is for the user process to wait for the kernel to prepare data , The data preparation phase of the kernel copies the data to kernel space , The user process also changes from user mode to kernel mode . disk I/O And the Internet I/O The only difference is the disk I/O The kernel reads disk data from the disk , And on the Internet I/O The kernel reads stream data from the network card . The data is ready to be received , The second step is to copy the data from the kernel space to the target user space for the user process to read .
1.3 I/O What is the model ?
Learned once I/O After the process of operation , So it's common to say I/O What is the model ?
I/O The model can be understood as data reading on the machine / Write a policy model for scheduling .
I/O The model can be understood from two dimensions :
1) Blocking and non blocking . User process initiation I/O After the operation , Depending on whether you need to wait I/O The operation cannot continue until it is completed , It can be divided into blocking and non blocking .
2) Synchronous and asynchronous . Initiated in the user process I/O After the operation , It will change from user mode to kernel mode , The kernel process executes after copying data I/O operation , After the operation is completed , You need to change from kernel mode to user mode , And you need to copy data .
- If it's synchronization I/O operation , In the process of copying data to user space , The user thread will block .
- If it's asynchronous I/O operation , Then the kernel thread directly copies the data to the user space , The user process is not notified until the operation of copying data is completed I/O Operation is completed , It will not cause the user thread to block when copying data directly . That is, the kernel process is responsible for copying data to the user process , User processes can perform subsequent operations directly .
The objects of synchronization and asynchrony are kernel processes .
Linux Yes 5 Kind of I/O Model , Namely :
- BIO(Blocking I/O): User process initiation I/O After the operation , The kernel enters the data preparation phase . When the data is not all ready , The kernel is always at the data preparation node , The user process is also blocked .
- NIO(Non-blocking I/O): User process initiation I/O After the operation , The kernel enters the data preparation phase . When the data is not all ready , The kernel has been in the data preparation stage , The user process does something else for the time being , But every once in a while, the user process asks if the data is ready . Once you're ready , The user process will continue the following operations .
- I/O Multiplexing: The model is mainly that a single process can handle multiple network connections at the same time I/O Model , In essence, the user process is still blocked , however select Not blocked ,select/poll/epoll The method of constantly polling is responsible for all Socket, When a Socket When data arrives , Notify the corresponding user process .
- Singal Driven I/O: When the user process executes I/O operation ( Read or write ) when , The kernel returns immediately , The process continues to run , After the kernel process completes I/O operation ( Or something wrong ) after , Signal the process .
- AIO(Asychronous I/O): The kernel thread directly executes the process of copying data , The user process is not notified until the data replication is completed I/O Operation is completed , It will not cause the user thread to block when copying data .
The first four are synchronous , The last one is asynchronous .
2. Java Yes I/O Encapsulation of models
Java Yes I/O The encapsulation of models can be divided into BIO、NIO and AIO Three .
2.1 BIO

After a client's connection request is received by the receiver , A thread will be created on the server to communicate with the client , This thread will generally perform reading 、 decode 、 Calculation 、 code 、 Send these steps .
This model is very resource consuming , First, on the server side, you need a receiver that is always blocked waiting for new client requests , Second, whenever a new client requests a connection , You need to create a new thread to process the request , Each thread needs to be destroyed after processing the request . The creation and destruction of threads will occupy system resources .
Therefore, the design of thread pool is added to this architecture , Here's the picture :
When the server receives the connection request from the client , Throw to the thread pool as a task , Let the thread pool allocate threads to execute logic that communicates with the client .
Although the design of adding thread pool can improve the system performance , But when the number of requests increases 、 When the number of threads is too high , The cost of frequent thread pool switching will also be exposed ; And on the server side, a thread is required to block the connection request waiting for the client , The thread on the client side will also block and wait because the server has not returned the request result .
2.2 NIO
Java NIO and Linux Non blocking in I/O There are some differences , He is more like Linux Nonblocking multiplexing in I/O, It can help Java Concurrent construction of synchronous nonblocking multiplexing I/O Applications , At the same time, it provides a data operation mode closer to the underlying high performance , And it supports select/poll Model , stay JDK1.5 After that, a pair of epoll Support for .
Here are three important concepts :
Channel( passageway ):Java There are four Channel, Namely FileChannel( For file operations )、SocketChannel( For clients TCP operation )、ServerSocketChannel( For the server TCP operation )、DatagramChannel( be used for UDP operation ).
- Channel It is a two-way data reading 、 Write channel . Channel It can read and write at the same time , It supports both blocking and non blocking modes .
- One client connection corresponds to one Channel, When there is a new connection request , Will send to Selector Register one Channel, And will Channel Bind with the corresponding event handler .
- The event handler contains the processing logic corresponding to various events in the channel , Event handlers have a total of 4 in , They are read data event handlers 、 Write data event handler 、 Connect event handler 、 Accept event handlers .
Buffer( buffer ): Can be Buffer Think of it as a container for storing data ,Channel It can be done to Buffer Read / Write operations , And read all the data / All writes go through the buffer . stay NIO in , There are two different buffers :
- Direct buffer : Can be operated directly JVM Out of the heap memory , That is, the buffer allocated in the system kernel cache , adopt allocateDirect() Method can allocate a direct buffer .
- Indirect buffer : You can only operate JVM Memory in the heap of , adopt allocate() Method can allocate an indirect buffer .
Buffer There are many implementations , such as ByteBuffer、CharBUffer、LongBuffer etc. , Corresponding to the implementation of a data type .
Selector( Selectors ):Selector By constantly polling the Channel To choose to distribute events that are ready to be processed , There are four kinds of events here , Connection events 、 Receive connection Events 、 Read events and write events .
- Selector Multiple... Can be polled and monitored at the same time Channel, When Selector Find a Channel When the data state of changes , Will pass SelectorKey Departure related events , And the relevant logic is executed by the event handler interested in this event , The data is from Buffer In order to get , Write back after execution Buffer, In order to offer Channel Read .
After understanding the above concepts , Whole NIO The process of processing is simple and clear . From the above processing structure diagram, you can see , When a client connection arrives , The server will create a for this new connection Channel, And will the Channel Sign up to Selector On ,Selector It will monitor all registered on itself Channel, Once a Channel The data state of has changed , For example, the data is read , Relevant events will be triggered .Channel The data read and write of are only related to Buffer Interact .
To be continued below ~
2.3 NIO Of Rector Pattern
2.3.1 single Rector Single thread model
2.3.2 single Rector Multithreading model
2.3.3 Master-slave Rector Multithreading model
2.4 AIO
Reference material :《 In depth understanding of RPC Framework principle and implementation 》 Written by Hua Zhongming .
边栏推荐
- Force buckle -104 Maximum depth of binary tree
- 原生小程序开发注意事项总结
- Webapi performance optimization
- [MySQL learning notes 20] MySQL architecture
- 新学派:不诈骗经济学
- Create menu file
- Jetpack compose layout (II) - material components and layout
- Learning notes of rxjs takeuntil operator
- BUG-00x bug description + resolve ways
- The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
猜你喜欢

Mongodb's principle, basic use, clustering and partitioned clustering

Floating window --- create an activity floating window (can be dragged)

字符串 实现 strStr()

Modbus protocol and serialport port read / write

Modbus协议与SerialPort端口读写

Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

How much does a small program cost? How much does a small program cost? It's clear at a glance

Vscode attempted to write the procedure to a pipeline that does not exist

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points

【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
随机推荐
【论文阅读|深读】LINE: Large-scale Information Network Embedding
View. post VS Handler. Differences and usage scenarios of post
[MySQL learning notes 21] storage engine
The path of Architects
Kotlin Foundation
DDS learning notes of opendds
Get started quickly with jetpack compose Technology
Webapi performance optimization
How much does a small program cost? How much does a small program cost? It's clear at a glance
Methodchannel of flutter
Cocopod error failed: undefined method `map 'for nil:nilclass
DigiCert和GlobalSign单域名OV SSL证书对比评测
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
How to install SSL certificates in Microsoft Exchange 2010
虚幻引擎图文笔记:使用VAT(Vertex Aniamtion Texture)制作破碎特效(Houdini,UE4/UE5)上 Houdini端
Kotlin advanced - class
Shardingsphere proxy 4.1 Sous - base de données sous - table
Redis(二)分布式锁与Redis集群搭建
Yolov5更换上采样方式
Shardingsphere proxy 4.1 sub database and sub table