Five types of IO models under Linux

Source: Internet
Author: User

Objective
    • Blocking io (blocking IO)
    • Non-blocking IO (nonblocking io)
    • IO multiplexing (IO multiplexing)
    • Signal-driven IO (signal driven IO (SIGIO))
    • Asynchronous IO (Asynchronous IO (the POSIX aio_functions))
One, blocking IO

Summary: The process will block until the data copy is complete.
The application calls an IO function that causes the application to block and wait for the data to be ready. If the data is not ready, wait all the time. The data is ready to be copied from the kernel to the user space.
After execution, the IO function returns a successful response to the application, and after the application responds, it is no longer blocked and does the work behind it.
The IO block in the network is as follows:

Example:
My girlfriend and I after the meal, do not know when to do well, had to sit in the restaurant and so on, until finished eating, to go shopping, in the middle of waiting for the cooking time wasted. This is the blockage.

Second, non-blocking IO

Summary: Non-blocking IO calls the IO function repeatedly through the process (multiple system calls and returns immediately), while the process is blocked during the copy of the data.
An IO function is called by the application, and this IO operation returns immediately from the kernel (an error is returned when the IO operation cannot be completed). But what this IO function does specifically (write data) may not be done.
For the application, though, the IO operation returns quickly, but it doesn't know if the IO operation is really successful. In order to know if the IO operation is successful, there are generally two strategies:
One is the need for the application to actively cycle to ask, until the data is ready (this method is synchronous non-blocking IO), in this constantly asked process, will be a lot of CPU time;
The second is the use of IO notification mechanism, such as: Io Multiplexing (this method is asynchronous blocking IO) or signal-driven IO (this method belongs to asynchronous non-blocking IO).
Network IO is non-blocking as shown in:

Example:
My girlfriend is not willing to wait in vain, and want to go shopping malls, but also worried about the food good. So we stroll for a while, come back to ask the waiter meal good no, come back and forth several times. This is non-blocking.

Iii. IO multiplexing (asynchronous blocking IO)

Summary: There is no advantage over blocking IO, the key is to enable monitoring of multiple IO ports simultaneously.
The IO multiplexing model uses the Select, poll, Epoll functions, which also block the process, but unlike blocking IO, these two functions can block multiple IO operations at the same time. It is also possible to detect multiple read operations, IO functions of multiple writes, and then actually invoke the IO operation function until there is data readable or writable.
The difference between asynchronous blocking IO and the second synchronous nonblocking IO is that synchronous non-blocking IO requires the application to proactively cycle to ask if there are operational data to operate, Asynchronous blocking Io, through an IO multiplexing function like Select and poll, detects multiple event handlers at the same time to tell the application whether data manipulation is possible.
Network IO multiplexing is as shown in:

Example:
The restaurant installed an electronic screen to show the status of the order, so that my girlfriend and I go shopping for a while, come back without having to ask the waiter, directly to see the electronic screen on it. So everyone's meal is good, all directly to see the electronic screen can be, this is the IO multiplexing.

Iv. Signal-driven IO (asynchronous non-blocking IO)

First we allow a socket interface for signal-driven IO and a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the IO operation function in the signal processing function to process the data.

V. Asynchronous IO

Introduction: Tells the kernel to initiate an operation and let the kernel notify us when the entire operation is complete, and the process does not need to block when copying data.
When an asynchronous procedure call is made, the caller cannot get the result immediately, and the function that actually processes the call notifies the caller of the input-output operation through state, notification, and callback after completion.
The difference between this model and the signal-driven IO is that the signal-driven IO is the kernel that notifies us when an IO operation can be initiated, which is implemented by a user-defined signal function, and the asynchronous IO model is the kernel that tells us when the IO operation is complete.
Linux provides an AIO library function for asynchronous implementations, but with little use. There are many open-source asynchronous IO libraries, such as Libevent, Libev, LIBUV, and so on.
The network asynchronous IO looks like this:

Example:
Girlfriend do not want to go shopping, home rest, so we call takeout, rice good sent to the home. Just make a phone call and say, then you can do your own thing, the meal is ready to send, this is asynchronous.

Summary:
Synchronous IO causes the process to block until the IO operation is complete.
Asynchronous IO does not cause the process to block.
Io multiplexing is blocked by a select call first.
Blocking io, nonblocking io, multiplexed io, and signal-driven IO all belong to synchronous mode, because the real IO operations (functions) will block the process, and only the asynchronous IO model really implements the async of IO operations.

Five types of IO models under Linux

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.