当前位置:网站首页>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 :
  •  Insert picture description here

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
原网站

版权声明
本文为[&lt; I can't do anything;]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280603123388.html