(reprint) Network programming: synchronous, asynchronous, blocking, non-blocking

Source: Internet
Author: User
Tags posix

The I/O model of network programming always involves these concepts. Asked a lot of people, not a few can clearly tell the difference between them, and even in the network there are many different points of view, do not know that the Chinese text interpretation of the profound, or the original concept is around people tireless. Today I will also explain to you my understanding of these concepts.

Since there are divergent opinions on the Internet, it is better to find an authoritative reference, this authority is "UNIX Network programming: Volume One," The sixth chapter--I/O reuse. The book mentions 5 types of I/O models available under Unix:

    • Block-type I/O;

    • Non-blocking I/O;

    • I/O multiplexing (Select,poll,epoll ... );

    • Signal-driven I/O (SIGIO);

    • asynchronous I/O (POSIX aio_ series functions);

blocking I/O model : By default, all sockets are blocked. How do you understand that? To understand this process first, an input operation typically consists of two different stages:

(1) Wait for the data to be ready;
(2) Copy data from the kernel to the process.


For an input operation on a socket, the first step usually involves waiting for data to arrive from the network. When all wait packets arrive, it is copied to a buffer in the kernel. The second step is to copy the data from the kernel buffer to the application buffer. OK, here's a call to block socket recvfrom that shows blocking

This part of the process of red is blocking until the end of the block recvfrom to return.

non-blocking I/O: The following sentence is important: the process sets a socket to non-blocking is the notification kernel, when the requested I/O operation must put the process into sleep to complete, do not put the process into sleep, but return an error. See how the recvfrom of non-blocking sockets work

It can be seen that recvfrom always returns immediately.

I/O multiplexing: Although the I/O multiplexing function is also blocked, but it differs from the above two, I/O multiplexing is blocking above system calls such as Select,epoll, without blocking on real I/O system calls such as Recvfrom.

Signal-driven I/O: With very little, do not explain. Direct

asynchronous I/O: This type of function works by telling the kernel to initiate an operation and letting the kernel notify us when the entire operation, including copying data from the kernel to the user space, is complete.

Note that the Red line mark indicates that it can be returned immediately at the time of invocation, and we will be notified when the function operation is complete.

Wait, everybody must ask, synchronization this concept why didn't you involve? Don't worry, you should read the summary first. In fact, the first four I/O models are synchronous I/O operations, their difference is in phase one, and their second phase is the same: when the data is copied from the kernel to the application buffer (user space), the process blocks to the recvfrom call. Instead, the asynchronous I/O model is handled in both phases.

Look at POSIX's definition of these two terms:

    • Synchronous I/O operation: Causes the request process to block until the I/O operation is complete;

    • asynchronous I/O operation: does not cause the request process to block.

Okay, I'll summarize it in my language. Blocking, non-blocking, synchronous, asynchronous

      • Blocking, non-blocking: whether the process/thread is ready to access the data, whether the process/thread needs to wait;

      • Synchronous, asynchronous: access to data, synchronization needs to actively read and write data, in the process of reading and writing data is still blocked, asynchronous only requires I/O operation to complete the notification, not actively read and write data, the operating system kernel to complete the data read and write.


Original address: http://yaocoder.blog.51cto.com/2668309/1308899

(reprint) Network programming: synchronous, 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.