Synchronous and asynchronous, blocking and non-blocking, reactor and proactor

Source: Internet
Author: User
Document directory
  • In layman's terms
  • From a program perspective

Http://www.artima.com/articles/io_design_patterns.html

Http://note.sdo.com/u/1434409594/n/lvRFW ~ Kjr2n0lx0ne004_r

Http://www.cnblogs.com/xuxm2007/archive/2011/08/15/2139808.html, illustration

 

I have read some blogs, and few people can make it clear, and many people do not understand it at all.

In layman's terms

Blocking and non-blocking, and other problems. dummies are blocking, and waits is non-blocking.
Asynchronous and synchronous, whether there is a concurrency issue, synchronization only changes the order, now this is not ready, do something else, wait for ready to return. in asynchronous mode, I will only tell you what to do and then I will do something else. After you finish the task, I will be notified. (The concurrency here is not the multi-thread concurrency at the application layer, but the concurrency between the application layer and the system layer)

For another simple example, there are a lot of people going through banking procedures.
The worst way is queuing. This is the blocking method.
If you choose non-blocking mode,
A. Round-Robin: if there are too many people to check whether there are too many people. If there are too many people to do other things, it is still very troublesome to keep round-robin.
B. Notification method (synchronous mode). An acquaintance in a bank should call you when there are few people. This is more efficient and will be handled when there are few people, effectively saves the waiting and polling time
C. The VIP mode (asynchronous mode): Go to the bank and directly explain how much or what business I want to take, put the money in my safe, and notify me, then why.
This method is the most efficient. Because of the concurrency, when you do other things, the Bank procedures are also done.

Through this example, we can see that the higher the efficiency, the higher the cost of the method, there is no free lunch...
Blocking and non-blocking round robin are not much efficient, and even blocking may be more efficient. this can be proved by daily life that many people will choose to wait in queue instead of coming back. so their cost is basically the same. You can do it by yourself, without the help of others, in the program, you just need the client process to OK

In non-blocking synchronization mode, you need to be notified. in the program, in addition to the client process, you also need the event demultiplexor process (to notify the event)

Non-blocking asynchronous mode requires not only people to inform you, but also banks to help you with your business (provided that the Bank provides such a service). You also need to provide a safe deposit box to store the processed business and money. therefore, in the program, it is complicated. The client process is required, the event demultiplexor process is required (to notify the event), and the operating system process is required (provided that the operating system supports asynchronous I/O ), you also need to allocate a data cache (save results)

It is easy to explain the reactor and proactor. The reactor is a non-blocking synchronous method, while the proactor is a non-blocking Asynchronous Method.

So, which method is better? Depending on your scenario, consider complexity, program efficiency, cost, and OS support.

 

Tproactor scheme, using reactor to simulate asynchronous

As mentioned above, proactor is good, but the problem is that it depends on OS support. For example, Unix does not support this function, so it cannot be used, but what should I do if I want to use an asynchronous method?
The above article proposes a scheme, tproactor, which is used to simulate proactor

The specific practice is also very simple. It is also an example of a bank. The state-owned banks do not provide agency services.
No, I asked the person in charge to inform me to replace the bank for help, and then I will be notified.
In a program, the event demultiplexor is used to read data, store the data in a custom buffer, and notify the client.

From a program perspective

Blocking I/O

Blocking method: the process starts from calling the recvfrom System Call and continues to be blocked, from wait for data, copy data from kernel to user, to the final return

 

 

Nonblocking I/O, round robin

 

I/O multiplexing (select, poll,Epoll, kqueue)

This should be the most commonly used method. Because UNIX does not support asynchronous I/O, the reactor design mode is the mainstream mode.
Two system calls are required. First, the SELECT statement is used to return the ready socket, and then the recvfrom statement is called to read data.
Calling select itself is blocking, but select can monitor multiple I/O at the same time, so it can be understood as a non-blocking method (not blocking on one I/O)
Select sends the data ready notification as event demultiplexor.

Differences between select, poll, epoll, and kqueue
Select is the most basic version. Others are the upgraded version of select, but they are essentially the same.
Select, each time you need to traverse fd_setsize socket to check which data exists, so when there are many monitored sockets, It is very inefficient
Poll and select () are restricted by fd_setsize handles, but poll does not have this restriction, but it still polls all sockets. This method is called 'level trigger'

Epoll and kqueue are socket-based callback. Therefore, to avoid polling, an event notification is automatically triggered when the socket status changes. The disadvantage is that the event is sent only once when the status changes, if the subsequent status does not change, no event will be sent, so it will be troublesome if the event is lost. this method is called 'edge triggered' (Jonathon lemon introduced the terms in his bsdcon 2000 paper on kqueue ())

 

Signal driven I/O (sigio)

Only supported by UNIX systems. When signal and handler are registered, the system signal is used for notification during data ready, which is still synchronous.

 

Asynchronous I/O (the POSIX aio_functions)

Pure asynchronous mode, but it seems that only Windows iocp has better support

 

Comparation

It can be seen that from left to right, more and more advanced, to completely asynchronous, but as mentioned above, the implementation cost is also growing ......

 

 

Http://www.kegel.com/c10k.html, The c10k problem Translation

Http://en.wikipedia.org/wiki/C10k_problem

Http://wenku.baidu.com/view/33e41da6f524ccbff12184f9.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.