当前位置:网站首页>[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 .
边栏推荐
- The left sliding menu +menu item icon is grayed out
- Flutter Gaode map privacy compliance error
- String longest common prefix
- Puzzle (019.2) hexagonal lock
- Rxjs TakeUntil 操作符的学习笔记
- I have summarized the knowledge points of JS [intermediate and advanced] for you
- Floating window --- create an activity floating window (can be dragged)
- Webapi performance optimization
- How to "transform" small and micro businesses (I)?
- Shardingsphere proxy 4.1 sub database and sub table
猜你喜欢
【论文阅读|深读】LINE: Large-scale Information Network Embedding
【动态规划】—— 数字三角形
How to install SSL certificates in Microsoft Exchange 2010
i++ 和 ++i的真正区别
【论文阅读|深读】DRNE:Deep Recursive Network Embedding with Regular Equivalence
Get started quickly with jetpack compose Technology
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
Vscode attempted to write the procedure to a pipeline that does not exist
What is CRA
随机推荐
Basic use and principle of Minio
How to apply for a widget on wechat how to get a widget on wechat
Download the arm64 package of Debian on X86 computer
How to make a self-made installer and package the program to generate an installer
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
The path of Architects
Mqtt beginner level chapter
Flutter Gaode map privacy compliance error
Gradle download warehouse is slow
Oracle查询自带JDK版本
Flask博客实战 - 实现侧边栏文章归档及标签
How to install SSL certificates in Microsoft Exchange 2010
Processing picture class library
View. post VS Handler. Differences and usage scenarios of post
Learning notes of rxjs takeuntil operator
Methodchannel of flutter
How to build a wechat applet? How to open an applet?
Force buckle -104 Maximum depth of binary tree
Use evo
Create menu file