Network Programming: synchronous, asynchronous, blocking, non-blocking

Source: Internet
Author: User
Tags epoll

I. Network Programming I/O models always involve these concepts. I have asked a lot of people, but few of them can clearly tell the difference between them. They even have many different opinions on the Internet. I don't know whether the meaning of Chinese text is profound or profound, these concepts are still irrelevant. Today, I will also explain my understanding of these concepts.

Since there are different opinions on the network, it is better to find an authoritative reference. This authority is "UNIX Network Programming: Volume 1" Chapter 6-I/O reuse. The book mentions the available I/O models for five types of UNIX:

  • Blocking I/O;

  • Non-blocking I/O;

  • I/O reuse select, poll, epoll ...);

  • Signal-driven I/OSIGIO );

  • Asynchronous I/OPOSIX aio _ series functions );

Blocking I/O model: By default, all sockets are blocked. How can this problem be solved? To understand this process, an input operation usually involves two different stages:

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


For input operations on a socket, the first step usually involves waiting for data to arrive from the network. When all the waiting groups arrive, it is copied to a buffer in the kernel. The second step is to copy data from the kernel buffer to the application buffer. Well, we will use the call diagram of the recvfrom block socket to describe the blocking.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12425LI0-0.jpg "title =" io.jpg "alt =" 202707564.jpg"/>

This part of the process marked with red is blocking, and it will not be returned until the blocking ends recvfrom.

Non-blocking I/O: The following sentence is very important: the process sets a socket to non-blocking and notifies the kernel. When the requested I/O operation must put the process into sleep to complete, instead of putting the process into sleep, an error is returned. How to perform the recvfrom operation on a non-blocking Socket

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12425H308-1.jpg "title =" non-provisioned io.jpg "alt =" 202858660.jpg"/>

We can see that recvfrom always returns immediately.

I/O multiplexing: Although I/O multiplexing functions are also blocked, they are different from the above two. I/O multiplexing is blocked in select, epoll and so on, without blocking on real I/O system calls such as recvfrom.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12425G4X-2.jpg "title =" multi-path reuse .jpg "alt =" 202009980.jpg"/>

Signal-driven I/O: It is rarely used and will not be explained. Direct

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12425I633-3.jpg "title =" signal drive io.jpg "alt =" 202028686.jpg"/>

Asynchronous I/O: The working mechanism of such functions is to inform the kernel to start an operation and notify us after the kernel completes the operation, including copying data from the kernel to our own buffer zone.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12425K320-4.jpg "title =" io.jpg "alt =" 202055894.jpg"/>

Note that the red line mark indicates that the interface can be immediately returned during the call, and will be notified when the function operation is complete.

Wait, you must have asked. Why didn't you involve synchronization? Don't worry. Let's take a look at the summary first. In fact, the first four I/O models are synchronous I/O operations. Their difference lies in the first stage, because their first stage is the same: during data replication from the kernel to the caller's buffer, the process blocks the recvfrom call. On the contrary, the asynchronous I/O model must be processed in both stages.

Now let's look at POSIX's definition of these two terms:

  • Synchronous I/O operation: the request process is blocked until the I/O operation is completed;

  • Asynchronous I/O operations: do not cause request process blocking.

Okay. I will summarize blocking, non-blocking, synchronous, and asynchronous in my language.

  • Blocking, non-blocking:Whether the data to be accessed by the process/thread is ready, and whether the process/thread needs to wait;

  • Synchronous and asynchronous:The data access method requires active Data Reading and Writing during synchronization. Blocking occurs during Data Reading and Writing. asynchronous data only requires notifications completed by I/O operations and does not actively read and write data, the operating system kernel reads and writes data.


This article from "forever friends" blog, please be sure to keep this source http://yaocoder.blog.51cto.com/2668309/1308899

Related Article

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.