Reactor and proactor modes designed for high-performance Io

Source: Internet
Author: User


In the high-performance I/O design, there are two well-known modes: reactor and proactor. The reactor mode is used to synchronize I/O, while the proactor is used for asynchronous I/O operations.

 

Before comparing these two modes, we should first understand several concepts, what is blocking and non-blocking, and what is synchronous and asynchronous, synchronization and Asynchronization are for the interaction between applications and the kernel. Synchronization means that the user process triggers Io operations and waits or polls to check whether the IO operations are ready, asynchronous means that the user process starts to do his/her own things after triggering the IO operation, and when the IO operation is completed, it will receive the notification of Io completion (Asynchronous is characterized by notification ). Blocking and non-blocking are different methods for processes to access data according to the ready status of Io operations. To put it bluntly, they are an implementation method for reading or writing operation functions, reading or writing a function in blocking mode will wait until the function is read or written, but not in blocking mode, a status value will be returned immediately after the function is read or written.

Generally, I/O models can be divided into synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking Io

Synchronous blocking IO:

In this way, after initiating an IO operation, the user process must wait for the completion of the IO operation. Only after the IO operation is completed can the user process run. This method is applicable to traditional Java I/O models!

 

Synchronous non-blocking IO:

In this way, the user process can return other things after initiating an IO operation, but the user process needs to ask from time to time whether the IO operation is ready, this requires the user process to keep asking, so as to introduce unnecessary CPU resource waste. Currently, Java NiO is a non-blocking synchronous Io.

 

 

Asynchronous blocking IO:

This method means that after an application initiates an IO operation, it does not wait until the kernel Io operation is completed. After the kernel completes the IO operation, it will notify the application, this is actually the key difference between synchronization and Asynchronization. Synchronization must wait or actively ask whether Io is completed. Why is it blocking? In this case, the Select function is called by the Select system, and the implementation method of the select function is blocking, the benefit of using the select function is that it can listen to multiple file handles at the same time (from the perspective of UNP, select is a synchronization operation. Because after the SELECT statement, the process also needs to read and write data) to improve the concurrency of the system!

 

 

Asynchronous non-blocking IO:

In this mode, the user process only needs to initiate an IO operation and then return immediately. After the IO operation is completed, the application will be notified of the completion of the IO operation, at this time, the user process only needs to process the data, and does not need to perform actual Io read/write operations, because the real Io read or write operations have been completed by the kernel. Currently, Java does not support this Io model.

After figuring out the above concepts, let's look back at the reactor mode and proactor mode.


(In fact, both blocking and non-blocking can be understood as the concept of synchronization. For Asynchronization, non-blocking will not be implemented. After receiving an asynchronous notification, the user process can directly operate the data in the user State Space of the process .)


First, let's take a look at the reactor mode. The reactor mode is applied to synchronous I/O scenarios. Let's take the read and write operations as an example to see the specific steps in the reactor:

Read operation:

1. Applications register read-ready events and associated event Processors

2. Wait for the occurrence of the event.

3. When a read-ready event occurs, the event splitter calls the event processor registered in step 1.

4. The event processor first performs the actual read operation, and then further processes the read content.

Write operations are similar to read operations, but the first step is to register a write-ready event.

 


Let's take a look at the read and write operations in proactor mode:

Read operation:

1. The application initializes an asynchronous read operation and registers the corresponding event processor. At this time, the event processor does not focus on read-ready events, but on read completion events, which is the key to distinguishing it from the reactor.

2. The event splitter waits for the read operation to complete the event

3. when the event splitter waits for the read operation to complete, the operating system calls the kernel thread to complete the read operation (asynchronous Io is the buffer Supply Program operation that the operating system is responsible for reading and writing data to the application, the operating system plays an important role), and puts the read content into the cache zone passed by the user. This is also different from the reactor. In proactor, the application needs to pass the cache area.

4. After the event splitter captures the read completion event, it activates the event processor registered by the application. The event processor directly reads data from the cache without actual read operations.

Write and read operations in proactor, but the event of interest is the write completion event.


From the above, we can see that the main difference between the reactor and proactor modes is who completes the real read and write operations. In the reactor, the application needs to read or write data by itself, in proactor mode, an application does not need to read or write data from the cache, the operating system reads the cache or writes the cache to the real I/O device.

 

To sum up, synchronization and Asynchronization are relative to the interaction mode between the application and the kernel. synchronous requests need to be initiated, while asynchronous requests notify the application when the kernel sends an IO event, blocking and non-blocking are only the implementation methods of functions when the system calls the system.

 

 

From http://xmuzyq.javaeye.com/blog/783218

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.