Synchronous/asynchronous, blocking/non-blocking

Source: Internet
Author: User
Tags epoll

The code has been coded once, and it's summed up:

First of all, synchronous/asynchronous is a relatively large concept, it applies to many aspects, blocking/non-blocking is not only in the socket, of course, we discuss the following mainly in the socket aspect.

Synchronization is serial, to a complete, can not jump away, similar to the government to do a procedure, only the last department to run the next department to do the next formalities.

Asynchronous is to have a notification mechanism, for example, after the transformation of the government functions, you can give his relevant departments to call to say what you want to do, he will notify you after the good work, in his process you can do other. This notification implies that the specific code is the signal or callback function.

Blocking is basically saying that a function waits until it has data, without getting the data she wants.

Non-blocking is to see if there is data, some words to continue the following work, did not return, you need to do is repeatedly to see if there is data.

There are 4 combinations of synchronous/asynchronous blocking/non-blocking:

Synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking.

1. Synchronization blocking is most common to, usually code, not special settings is this mode.

2. Synchronous non-blocking, relatively easy, as long as set the socket properties can be, in general add a This can be:

if (Fcntl (LISTENFD, F_SETFL, o_nonblock) = = 1)
{
printf ("Fcntl Error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}

This is the UNIX approach, window has its own function settings.

This mode, in fact, is very inefficient, should not use much.

3. Asynchronous blocking

I see a lot of places to put the Select and poll here, then put it, in fact, I think his 2 is just a variant of the synchronization non-blocking.

A lot of non-blocking is monitored by a call, and if an FD or some information is returned, process the information.

4. Asynchronous non-blocking

The real thing is AIO, where he has set up signal processing functions or callback functions, it really does not need you to go back to processing the received information.

Like Epoll This, more is the poll of an improvement, of course it's changed a bit large, his callback function fixed, the advantage is only to return the information of FD.

Now the voice on the Internet should be epoll than AIO better, I did not actually measured.

Http://www.cnblogs.com/hustcat/archive/2012/10/08/1708909.html

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.