Introduction to IO Model and the difference between synchronous asynchronous blocking and non-blocking

Source: Internet
Author: User
Tags epoll

Blocking: When a user process accesses data, if IO is not completed, wait for IO to complete or make a system call to determine whether IO is complete
Non-blocking: When a user process accesses data, it returns a status value immediately, whether or not it completes

Sync: Polling kernel state after user process initiates IO (ready to judge)
Asynchronous: After the user process initiates IO, can do other things, waiting for the kernel to notify

Introduction to the IO model

The network IO model is the same as the file IO model, with 5 models of Io, including blocking io, non-blocking io, multiplexing io, signal-driven IO, and asynchronous IO.

One IO consists of two processes, kernel data preparation, copy of data from kernel space to user space.

1. Blocking IO (recvfrom system call) is blocked in two process applications.

2, non-blocking io (recvfrom system call) is the application of the IO operation can be returned immediately, through the polling disk to determine whether the data is ready to block the application in the copy data phase.

3. Multiplexing Io (SELECT, recvfrom system call) is a blocking call to select to find available sockets, and if a socket is available, block the call (Recvfrom) to complete the copy process of the data. Linux Select is this model, the disadvantage is that a select will scan all the sockets.

4, Signal-driven IO (Sigio, recvfrom) is the application issued after the sig Io returned immediately after the data in the kernel is ready to notify the application, blocked by the application of Recvfrom calls from the kernel copy data. Linux Epoll is based on the event-ready notification method, eliminating all the socket scanning overhead.

Epoll,kqueue than select Advanced, select is in the kernel polling operations, Epoll is the use of callback mechanism, consumes less resources. More sockets, each time select () by traversing the socket to complete the scheduling, regardless of which socket is active, are traversed again. This can waste a lot of CPU time. Epoll registers a callback function with the socket and, when they are active, completes the operation automatically, avoiding polling.

5, asynchronous IO (aio-read) is the application issued Aio-read immediately after the return, data ready, by the operating system to copy the data to the application, and notify the application of data copy completed.

Of course, in addition to the IO mode above, there are direct IO (applications bypassing the kernel to access disk files directly), Memory mapping mmap (building the mapping relationship between memory and files), the two models in Linux 2.6 essentially no difference.

Direct IO in the database engine used more, the database buffer bypass file system cache, direct access to the hard disk. In MongoDB, mmap is used without invoking the read, write system calls.

There are several combinations of synchronization, asynchrony, blocking, and non-blocking:

Synchronous non-blocking, typically Java NIO

Asynchronous blocking, typically represented by Select,epoll

Asynchronous non-blocking, typically an AIO

The pattern in concurrent design has the following two kinds:

Reactor (NIO) and Preactor (AIO) are handled by the dispatch/detach operation IO event.
The Distribute/detach event is the notification of individual IO events to the upper module. The difference is that Preactor is used for Asynchrony, reactor for synchronization.
As you can see, the same point in the two patterns is the event notification for an IO event (that is, to tell a module that this IO operation can or has been completed).
Structurally, the two also have the same point: dispatch is responsible for submitting IO operations (asynchrony), querying whether the device is operable (synchronizing), and then callback handler when the condition is met.
The difference is that, in an asynchronous case (Preactor), when the callback is handler, the IO operation is completed, the synchronization case (reactor), and the callback handler, represents
The IO device can perform an operation (can read or can write), handler this time to start the commit operation.

The main difference between the reactor and Proactor modes is who the true read and write operations are, and the reactor requires the application to read or write the data itself, and in Proactor mode, the application does not need the actual read and write process. It only needs to be read or written from the buffer, and the operating system reads the buffer or writes the buffer to the real IO device.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.