About IO synchronization, asynchronous, blocking, non-blocking

Source: Internet
Author: User
Tags ming

About the network IO synchronization, asynchronous, blocking, non-blocking articles on the web there are many, the search for a comparison, the views are different, even the wiki is not very clear to distinguish between asynchronous and non-blocking. Here I will combine the "UNIX Network Programming Volume 1" In the introduction, to speak of their own understanding.

IO Model

First of all, we need to know the current UNIX five types of IO models, namely:

    • Blocking IO (blocking I/O)
    • Non-blocking IO (noblocking I/O)
    • IO multiplexing (I/O multiplexing)
    • Signal driven (signal driven I/O)
    • Asynchronous IO (asynchronous I/O)



about these five kinds of models here is not detailed, we are interested to refer to this blog post, written in very detailed: http://blog.csdn.net/sunyubo458/article/details/6096723


Two phases of IO

1. Wait for the data to be ready

2. Copying data from a kernel buffer to a user process buffer


The difference between synchronous and asynchronous

So what exactly is the difference between synchronous and asynchronous? My summary is as follows:

    • Synchronous IO requires the user process to proactively copy the data stored in the kernel buffer into the user process.
    • Asynchronous Io, the kernel automatically copies the data from the kernel buffer to the user buffer before notifying the user.

In this way, the concept of synchronous and asynchronous is very obvious. The above five IO models, the first four are synchronous, only the fifth IO model is asynchronous IO.


Blocking and non-blocking

What about blocking and non-blocking? Take note of the above five models. Block Io, non-blocking IO, just two of the five models above. Blocking, non-blocking, is for a single process.

Let's take a quick look at the difference between blocking and nonblocking sockets: Typically, a file or device specified for a file descriptor works in two ways: blocking and non-blocking.

(1). Blocking mode means that when an attempt is made to read and write the file descriptor, if there is no data to read at that time, or if it is temporarily not writable, the program goes into a wait state until something is readable or writable.

(2). Non-blocking means: If no data is readable, or not writable, the read-write function returns immediately without waiting.

(3). For example, for example, Xiao Ming to find a goddess to chat, but the goddess is not. If Xiao Ming reluctant to go, only at the goddess Gate death, of course, Xiao Ming can rest. When the goddess comes, she wakes you up (because she's blocking her door) and that's how it's blocked. If Xiao Ming found that the goddess is not in, immediately leave, after every 10 minutes back to look at (using polling method), leave immediately, this is non-blocking mode.

(4). The only difference between blocking and non-blocking: whether to return immediately.

Let's take a deeper understanding,

When making calls to multiplexed Io, such as using poll. It should be noted that poll is a system call, when the call poll, in fact, is already trapped in the kernel, is the kernel thread running. Therefore, it is blocked at this time for the user process calling poll.

Because the bottom-level implementation of poll is to scan each file descriptor (FD), and if you want to scan the FD of interest, you can only set each descriptor to a non-blocking form (for user processes, set FD to be blocking or non-blocking, you can use System call FCNTL), This makes it possible to scan. If a scan is found that has a readable (if readable, user-interested) FD, then select is returned at the user process level and informs the user of the process which FD is readable.

At this point, the user process still needs to use the Read system call to copy the FD data from the kernel buffer to the user process buffer (which is also why the poll is synchronous IO).

So is read in this case blocking or non-blocking? This depends on the state of FD, if the FD is set to non-blocking, then read is non-blocking, or is blocked. However, the program has been implemented at this time, regardless of whether the FD is blocking or non-blocking, there is no difference, because the previous poll, is to know that the data is ready to return, that is, the kernel and buffer already have data, when read, is sure to be able to copy the data to the user process buffer.

But if the idea is to change, if the poll is returned because of a timeout, and we call a FD (when FD is polled by poll), then it is very meaningful to block or non-blocking at this point.


Conclusion

1. Determine if IO is synchronous or asynchronous and who is actively copying the data to the user process

2.select or Poll,epoll, is a synchronous call

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About IO synchronization, asynchronous, blocking, non-blocking

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.