当前位置:网站首页>Explain bio, NiO, AIO in detail
Explain bio, NiO, AIO in detail
2022-06-12 23:56:00 【< I can't do anything;】
1》 IO Block
OS Proactively anticipate this Block Will take the initiative Block, for example TCP When connecting data , Find out Socket Buffer No data in , It means that the sender has not sent data yet , Then you can Block
Like reading data from the hard disk ,OS I don't know when the data will get stuck , So it won't happen IO Block
2》BIO( Synchronous blocking )
- Blocking IO, It is similar to the network
read
,write
,connect
A class of system calls get stuck - Single threaded network service , If you do this, you will get stuck . Because when waiting , The whole thread will be suspended , Unable to execute , Can't do other work
- Block Only the current thread will be affected , Does not affect other threads
- In development , What is needed is to call read When the function reads data , Read the data and use , Switch processes to do other things without data , Unwanted Block Blocking , therefore NIO On stage
3》 NIO( Synchronous nonblocking )
stay NIO In mode , call read, If no data has arrived , Will return immediately -1, also errno Is set to
EAGAIN
.Simulation code :
struct timespec sleep_interval{ .tv_sec = 0, .tv_nsec = 1000}; ssize_t nbytes; while (1) { /* Attempt to read */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { if (errno == EAGAIN) { // No data perror("nothing can be read"); } else { perror("fatal error"); exit(EXIT_FAILURE); } } else { // There's data process_data(buf, nbytes); } // Deal with other things , When you're done, just wait , Try again nanosleep(sleep_interval, NULL); }
NIO There will also be problems :
- If you have a large number of file descriptors, you have to wait , Then one by one read. This will bring a lot of Context Switch
- It's not easy to take a break , If it is too long , The response time of the program will be very long ; If it is too short , This will lead to frequent process switching , cause CPU waste
To solve the above problem ,IO Multiplexing is on the scene
NIO Realization principle :
NIO It consists of three core parts :Channel、Buffer、Selector
- be-all IO stay NIO All from one Channel Start , Data can be obtained from Channel Read buffer, You can also get it from buffer Read in Channel
- buffer Is a piece of memory that can read and write data
- Selector Allow a single thread to handle multiple Channel
Buffer Contains three important attributes :capacity、position、limit
- capacity: Maximum memory size
- position: Similar to read / write pointer , Indicates the current read / write location
- limit: Write mode indicates how much data is written at most , here limit==capacity; Read mode is equivalent to position, Indicates the current read location . The relationship is as follows :
4》 AIO( Asynchronous non-blocking )
- AIO And NIO Different , The read / write operation only needs to call read、write The method can , Both methods are asynchronous , I will take the initiative to Callback function
- AIO yes Commissioned OS Manage read and write operations . When reading is requested ,OS The data to be read will be loaded into read Function buffer , And notify the application to read the data ; When writing is requested , The written data will pass through OS The buffer is written to the specified location , The application will also be notified when the write is complete
summary :
- Synchronous blocking : The user process initiates a IO After the request , You need to wait IO Other processes cannot run until the operation is completed .
- Such as : Go to the bank by yourself ATM Machine withdrawals and queues
- Synchronous nonblocking : User process initiation IO After asking, you can do other things , No need to suspend waiting , But you need to ask from time to time IO Is the operation over .
- Such as : Take it by yourself VIP Go to the front desk of the bank to withdraw money , There's no need to line up , Play with your mobile phone when you withdraw money , But I need to take a look at the counter from time to time .
- Asynchronous non-blocking : User process initiation IO You can do other things after asking , There is no need to ask , kernel IO The application process will be automatically notified after the operation .
- Such as : Send a little brother to hold it VIP Card to withdraw money , After withdrawing the money, I will call myself and say that I have got the money
边栏推荐
- [leetcode] understanding and usage of map[key]+
- 在 Golang 中构建 CRUD 应用程序
- Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
- Case sharing of online real queuing system reconfiguration -- practical part
- How does the PMP handle the withdrawal?
- What can PMP bring to you
- How to control the display and hiding of layergroup through transparency in leaflet
- 3、 Storage system
- leaflet如何优雅的展示重叠点位的气泡窗口
- Start of u-boot_ Armboot analysis (II)
猜你喜欢
[hcie discussion] multicast igmp-a
Case sharing of online real queuing system reconfiguration -- practical part
SAP QM qp03 displays an inspection plan with multiple specs inspection features
【HCIE论述】组播IGMP-A
dict和set的基本操作
OSM map local publishing - how to generate vector maps of provinces and cities
Software development tools [3] theoretical basis of software development tools
[literature translation - Part] revealing the structure of clinical EEG signals by self supervised learning (SSL and RP principles / data / preprocessing)
一篇文章学会子网划分
How to publish OSM maps locally and customize the mapping
随机推荐
【HCIE论述】RR-A
2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne
KConfig
【Matlab】三维曲线与三维曲面
Start of u-boot_ Armboot analysis (I)
你真的会用PostGIS中的buffer缓冲吗?
VHDL编程实验练习题合集
How does the PMP handle the withdrawal?
Free lottery --- PMP renewal PDU | PMP knowledge map
【Matlab】二维曲线
一篇文章学会子网划分
Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login
最全预告!华为云精彩议程火速收藏
Xi'an Jiaotong 22nd autumn e-commerce technology online expansion resources (IV) [standard answer]
[hcie discussion] STP-A
实战 | UI 自动化测试框架设计与 PageObject 改造
[matlab] symbol calculation
【HCIE论述】组播IGMP-A
Based on three JS offshore wind power digital twin 3D effect
Vscode实现PHP在浏览器实时预览