当前位置:网站首页>Understand common network i/o models
Understand common network i/o models
2022-07-07 05:03:00 【Barry Yan】
For network I/O Model learning , It is a very important part of the operating system , because I/O It is also a crucial aspect and factor to be considered in our system design , So I want to use an article to analyze , For now , Five kinds of networks in the industry I/O Classification of models , Mainly divided into the following :
- Blocking IO Model
- Non blocking IO Model
- Multiplexing IO Model
- Signal driven IO Model
- asynchronous IO Model
According to my own study and some references , Make the following diagram :
1 Brief concept
According to these classifications in the diagram , We can compare the concepts :
(1) Synchronous and asynchronous The concept of describes how user threads interact with the kernel :
Synchronization refers to user thread initiation IO Wait or poll kernel after request IO Do not continue until the operation is completed ;
Asynchrony means that the user thread initiates IO Continue execution after request , When kernel IO Notify the user thread when the operation is completed , Or call the callback function registered by the user thread .
(2) Blocking and non-blocking The concept describes the user thread calling the kernel IO How to operate :
Obstruction means IO Operation needs to be completed completely before returning to user space ;
Non blocking means IO A status value is returned to the user immediately after the operation is called , No need to wait IO Operation complete .
(3) Multiplexing I/O
- I/O Multiplexing is through a mechanism , Can monitor multiple descriptors , Once a descriptor is ready ( Generally read ready or write ready ), Can inform the program to read and write accordingly .
(4) Event driven I/O
- Event driven I/O In essence, it's going to be I/O Operations are transformed into events that the program needs to handle , Reduced waiting mechanism , Instead, it uses the mechanism triggered by events , Increased efficiency .
2 socket- Synchronous blocking I/O
socket In interprocess communication, it can communicate across hosts . about Socket The programming process of We are also familiar with :
(1) Server call socket() function
(2) Then the server calls bind() function , to Socket Bind one IP Address and port
(3) Binding completed IP After address and port , call listen() Function listening
(4) After the server enters the listening state , By calling accept() function , To get the client connection from the kernel , If there is no client connection , will Blocking Waiting for the arrival of the client connection
(5) The client is creating Socket after , call connect() Function to initiate the connection , The parameters of this function should indicate the of the server IP Address and port number
(6) then TCP Three handshakes
(7) After the connection is established , The client and server begin to transmit data to each other , Both sides can pass read() and write() Function to read and write data

( The picture is from :https://blog.csdn.net/qq_34827674/article/details/115619261)
Multi process model :
( The picture is from :https://blog.csdn.net/qq_34827674/article/details/115619261)
Introduction of multithreading :
( The picture is from :https://blog.csdn.net/qq_34827674/article/details/115619261)
Sum up , The above Socket Whether it's single process mode , Or multi process or multi thread mode , Their principle is blocking , That is, the processing of the next client process needs to wait. Of course, the client request is processed , Empathy , The server will also be in the listening state all the time , Don't deal with it until you accept the request .
3 select- Synchronous blocking & Multiplexing I/O
select The way to realize multiplexing is : Connect connected Socket Put them into a collection of file descriptors , And then call select Function copies the set of file descriptors into the kernel , Let the kernel check whether there are network events , The way of inspection is rough , By traversing the set of file descriptors , When an event is detected , Put this Socket Mark as readable or writable , Then copy the whole set of file descriptors back to the user state , Then the user state also needs to find readable or writable through traversal Socket, Then deal with it .
select Use fixed length BitsMap, Represents a collection of file descriptors , And the number of supported file descriptors is limited , The default maximum value is 1024, Can only monitor 0~1023 File descriptor for .
( The picture is from :https://www.cnblogs.com/yanguhung/p/10145755.html)
Pictured , about select This way, , Need to carry out 2 Traverse the file descriptor collection once , Once in kernel state , One is in the user state , And it will happen 2 Secondary copy file descriptor set , First pass in kernel space from user space , Modified by the kernel , And then out to user space .
4 poll- Synchronous blocking & Enhanced multiplexing I/O
poll No more BitsMap To store the file descriptor of interest , Instead, use dynamic arrays , Organize... In the form of a linked list , Break through the select Limit the number of file descriptors , Of course, it is also limited by the system file descriptor .
however poll and select There is not much essential difference , They are all concerned with using linear structure to store processes Socket aggregate , Therefore, you need to traverse the set of file descriptors to find readable or writable Socket, The time complexity is O(n), It is also necessary to copy the set of file descriptors between user state and kernel state , This method increases with the number of concurrency , The loss of performance will increase exponentially .
5 epoll- Synchronous blocking & Enhanced multiplexing & Event driven I/O
epoll comparison select/poll These two aspects are enhanced :
The first point ,epoll Use the red black tree in the kernel to track all the file descriptors of the process to be detected , Put what needs to be monitored socket adopt epoll_ctl() The function is added to the red black tree in the kernel , Red black tree is an efficient data structure , The general time complexity of adding, deleting and querying is O(logn), By operating on this black mangrove , So you don't need to be like select/poll The entire... Is passed in each operation socket aggregate , Just pass in a to be detected socket, Reduce a large number of data copies and memory allocation in kernel and user space .
Second point , epoll Use an event driven mechanism , A linked list is maintained in the kernel to record ready events , When a socket When something happens , Through the callback function, the kernel will add it to the ready event list , When the user calls epoll_wait() Function time , Only the number of file descriptors with events will be returned , No need to be like select/poll So poll and scan the whole socket aggregate , The efficiency of detection is greatly improved .

( The picture is from :https://blog.csdn.net/qq_34827674/article/details/115619261)
epoll Even if it's listening Socket The more the numbers are , The efficiency will not be greatly reduced , Can monitor at the same time Socket It's also very large , The upper limit is the maximum number of file descriptors opened by the system defined process . thus ,epoll It's called the solution C10K The sharp weapon of the problem .
Tips:
epoll Is the solution C10K The sharp weapon of the problem , It solves the problem of select/poll The problem of :
(1)epoll Use... In the kernel 「 Red and black trees 」 To focus on all processes to be tested Socket, Red black tree is an efficient data structure , The general time complexity of adding, deleting and querying is O(logn), Through the management of this black mangrove , No need to be like select/poll The entire... Is passed in each operation Socket aggregate , Reduce a large number of data copies and memory allocation in kernel and user space .
(2)epoll Use an event driven mechanism , The kernel maintains a 「 Linked list 」 To record ready Events , There will only be events Socket The collection is passed to the application , No need to be like select/poll Then poll and scan the entire collection ( With and without events Socket ), The efficiency of detection is greatly improved .
6 asynchronous I/O
asynchronous I/O At present, only the concept exists .
asynchronous IO Concept and synchronization IO relative . When an asynchronous procedure call is issued , The caller can't get the result immediately . The part that actually handles this call is done , Passing state 、 Notifications and callbacks to notify callers . In a CPU Intensive application , Some data that needs to be processed may be placed on disk . Know these numbers in advance Location of data , Therefore, asynchronous is initiated in advance IO Read request . Wait until you really need to use these data , Wait for asynchronous IO complete . Asynchronous used IO, Is initiating IO From the request to the actual use of data , The program can continue to do other things
7 summary
Through the Internet I/O Model learning , From blocking to non blocking , From synchronous to asynchronous to event driven , It can be said that the efficiency is greatly improved , And it extends from the network to the setting surface , such as Spring Framework of the Reactor Mechanism ,Kafka Message queuing and so on .
Today's article is here ,bye~
Reference article :
https://www.cnblogs.com/yanguhung/p/10145755.html
https://blog.csdn.net/qq_34827674/article/details/115619261
https://baike.baidu.com/item/%E5%BC%82%E6%AD%A5IO/6018433?fr=aladdin
边栏推荐
- Analysis -- MySQL statement execution process & MySQL architecture
- app内嵌h5---iphone软键盘遮挡输入文字
- 为什么很多人对技术债务产生误解
- MySQL数据库(基础篇)
- 装饰器基础学习02
- 一文搞懂常见的网络I/O模型
- Two methods of chromosome coordinate sequencing
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
- 九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
- Weebly移动端网站编辑器 手机浏览新时代
猜你喜欢

Tree map: tree view - draw covid-19 array diagram

高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍

MySQL数据库(基础篇)

Error: No named parameter with the name ‘foregroundColor‘

A row of code r shows the table of Cox regression model

AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘

Function pointer and pointer function in C language

Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
![[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III](/img/13/d598bb53b71fbadd4a97c603152124.png)
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III

九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
随机推荐
Tree map: tree view - draw covid-19 array diagram
LabVIEW在打开一个新的引用,提示内存已满
Decorator basic learning 02
指针与数组在函数中输入实现逆序输出
JDBC link Oracle reference code
STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
如何设计 API 接口,实现统一格式返回?
Test interview | how much can you answer the real test interview question of an Internet company?
What is Web3
Leetcode notes
U++ 元数据说明符 学习笔记
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
Windows are not cheap things
Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
Vscode automatically adds a semicolon and jumps to the next line
窗口可不是什么便宜的东西
C语言中函数指针与指针函数
Function pointer and pointer function in C language
Talk about the importance of making it clear