当前位置:网站首页>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
边栏推荐
- Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
- Markdown编辑器
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
- File upload vulnerability summary
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
- Read of shell internal value command
- Inventory host list in ansible (I wish you countless flowers and romance)
- STM32F103 realize IAP online upgrade application
- Function pointer and pointer function in C language
- Weebly移动端网站编辑器 手机浏览新时代
猜你喜欢

U++4 接口 学习笔记

3GPP信道模型路损基础知识

Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str

U++ 元数据说明符 学习笔记

Depth first traversal template principle of tree and graph

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

How to choose an offer and what factors should be considered

If you‘re running pod install manually, make sure flutter pub get is executed first.

01 machine learning related regulations

R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
随机推荐
Servicemesh mainly solves three pain points
STM32F103 realize IAP online upgrade application
A line of R code draws the population pyramid
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
If you ask me about R code debugging, I will tell you head, STR, help
Windows are not cheap things
System framework of PureMVC
sublime使用技巧
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
Inventory host list in ansible (I wish you countless flowers and romance)
《五》表格
Canteen user dish relationship system (C language course design)
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
3. Type of fund
2.证券投资基金的概述
U++ 游戏类 学习笔记
JS 的 try catch finally 中 return 的执行顺序
深入解析Kubebuilder
How to package the parsed Excel data into objects and write this object set into the database?
JDBC link Oracle reference code