当前位置:网站首页>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,connectA 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
边栏推荐
- Matlab【路径规划】—— 无人机药品配送路线最优化
- Leaflet that supports canvas Path. Dashflow dynamic flow direction line
- Memory address mapping of u-boot
- Do you have to read for PMP?
- What occupation is suitable for PMP?
- 启牛商学院里面的券商账户是安全的吗?开户费率低吗
- SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
- PLC也能制作小游戏----Codesys编写猜数字小游戏
- PLC can also make small games ----- CoDeSys can write small games of guessing numbers
- 进程间通信-共享内存shmat
猜你喜欢

2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試

Theory + practice will help you master the dynamic programming method

Accelerating with Dali modules

OSM地图本地发布-如何生成各省市矢量地图

How does idea switch the interface to Chinese

Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022

Will the salary increase after obtaining PMP certification?

Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
![[matlab] two dimensional curve](/img/5a/bd69c78513fe4b59b2c63dc3679841.png)
[matlab] two dimensional curve

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
随机推荐
[matlab] matrix
array
KConfig
【HCIE论述】组播IGMP-A
数组
Start blogging
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
[matlab] polynomial calculation
2022年电工(初级)操作证考试题库及在线模拟考试
Tsinghua-Bosch Joint ML Center, THBI Lab:Chengyang Ying | 通过约束条件风险价值实现安全强化学习
Interprocess communication - shared memory shmat
Start of u-boot_ Armboot analysis (II)
[matlab] two dimensional curve
MySql索引
[matlab] basic knowledge
vs studio_ How to use scanf in 2022
MySQL index
[SciPy optimization tutorial] v. quick solution of univariate function optimization
华为云会议初体验【华为云至简致远】
如何利用华为云容灾解决方案替代灾备一体机