Synchronous blocking IO, IO nonblocking synchronous, asynchronous blocking (IO multiplexing), the difference between non-blocking asynchronous IO

Common 4 Zhong IO model

(1) synchronous blocking IO (Blocking IO): namely the traditional IO model.

(2) synchronous non-blocking IO (Non-blocking IO): The default socket created is blocking, non-blocking IO requirements socket is set to NONBLOCK.

Note talking about here is not Java's NIO NIO (New IO) library.

(3) IO multiplexing (IO Multiplexing): namely the classic Reactor design patterns, sometimes called asynchronous blocking IO, Java in the Selector and Linux epoll is this model.

(4) asynchronous IO (Asynchronous IO): That classic Proactor design pattern, also known as asynchronous non-blocking IO.

I / 0 operations basic concepts

1 data preparation, data is loaded into the kernel cache (load data to the operating system)

2 kernel loads the data cache to the user buffer (copied from the application to the operating system)

3 is how users interact with the kernel threads of synchronous and asynchronous concepts described

4 blocking and non-blocking concepts described in the user kernel thread calls the IO operation mode

Synchronous blocking IO

Synchronous blocking IO IO model is the simplest model, the user thread is blocked IO operations in the kernel.

 https://images2018.cnblogs.com/blog/874126/201808/874126-20180815215329688-621626362.png

IO thread calls the user initiates a read operation read through the system from user space to kernel space. When the kernel thread waiting for the user data will cause obstruction suspended, until the packet reaches the core, and then copy the received data into user space, to complete the read operation.

Synchronous non-blocking IO

Synchronous non-blocking IO is based on synchronous blocking IO on the socket set to NIO (NONBLOCK).

https://images2018.cnblogs.com/blog/874126/201808/874126-20180815215551173-501412222.png

Made to the socket is non-blocking, so users thread returns immediately initiate IO requests. The return code is likely to be a code representing the data being processed, this time sending the polling thread has been circulating IO request until the data is ready in the kernel really get the data. Nonblocking herein means: request has been occupied by the kernel thread polling time slice without being suspended kernel state. Save cancel user threads pending resource consumption efficiency, but also accompanied by a large number of CPU operations. The user is concerned may be more quickly received data request.

IO multiplexing (asynchronous blocking)

IO multiplexing model is built based separation function select multiplexer provided over the core,

Use the select function to avoid problems synchronous non-blocking IO model polled waiting.

https://images2018.cnblogs.com/blog/874126/201808/874126-20180815215732938-781387747.png

Users will first need to add socket IO operations to select, and then select block waiting for system calls to return.

When data arrives, socket is activated, select the function returns. User thread officially launched read request, reads the data and continue.

From the process point of view, use the select function request and synchronous blocking IO model is not much difference,

Adding even more to monitor socket, as well as additional operations call the select function, efficiency worse.

But later use select the biggest advantage is that users can simultaneously handle multiple socket IO request within a thread.

Users can register multiple socket, and then continue to read the call to select activated socket, can achieve the purpose within the same thread handle multiple IO requests.

In synchronous blocking model, you must in order to achieve this goal through a multi-threaded approach.

However, the advantage of using the select function is not limited thereto.

Although the above-described embodiment allows a plurality of IO requests within a single thread, but the process for each of the IO request is blocked (blocking select function on), the average time even longer than the synchronous blocking IO model.

If the user thread only registered socket or IO request're interested in, and then do their own thing, and then wait for the arrival of data processing, it is possible to improve the utilization of the CPU.

Asynchronous IO (non-blocking)

"Real" operating system asynchronous IO needs stronger support.

In IO multiplexing model, the event loop to handle status notification event file to the user thread, data themselves read by the user thread, data processing.

In asynchronous IO model, when a user thread is notified, read kernel data has been completed and placed in the buffer specified user threads, the kernel thread notifies the user after completion of IO can be used directly.

https://images2018.cnblogs.com/blog/874126/201808/874126-20180815220557814-526945852.png

Compared to IO multiplexing model, asynchronous IO is not very common, a lot of high-performance concurrent services that use multiplexed IO model + multithreaded processing architecture to meet the basic needs.

Moreover, the current operating system support for asynchronous IO is not particularly perfect, more is the use of asynchronous IO IO multiplexing model to simulate the way.

 

Guess you like

Origin blog.csdn.net/datuzijean/article/details/90520902