Reactor and Proactor models for high performance IO design

Source: Internet
Author: User

In High-performance I/O design, there are two well-known patterns reactor and Proactor modes, where the reactor mode is used to synchronize I/O, while Proactor is applied to asynchronous I/O operations.

Before you compare these two patterns, we first understand a few concepts, what is blocking and non-blocking, what is synchronous and asynchronous, synchronous and asynchronous are for application and kernel interaction, synchronization refers to the user process triggering IO operations and waiting or polling to see if the IO operation is ready, Asynchronous means that the user process triggers the IO operation and then starts to do its own thing, and when the IO operation has been completed, it will be notified of IO completion. And blocking and non-blocking are aimed at the process when accessing the data, depending on how the IO operation is ready, it is a way to read or write a function that reads or writes to the function, and the read or write functions will wait instead of blocking, and the read or write function immediately returns a status value.

Generally speaking I/O model can be divided into: synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking IO

Synchronous Blocking IO:

In this way, the user process must wait for the IO operation to complete after an IO operation is initiated, and the user process will not run until the IO operation is actually completed. The Java traditional IO model belongs in this way.

Synchronous non-blocking IO:

In this way, the user process initiates an IO operation to return to doing other things, but the user process needs to ask the IO operation to be ready from time to time, which requires the user process to constantly ask, thus introducing unnecessary waste of CPU resources. The current Java NiO belongs to synchronous non-blocking io.

Asynchronous blocking IO:

This means that after the application initiates an IO operation, do not wait for kernel IO operations to complete, and so the kernel completes the IO operation will notify the application, this is actually synchronous and asynchronous the most critical difference, synchronization must wait or actively ask IO is completed, then why is it blocked? Because this is done by a select system call, and the Select function itself is implemented in a blocking way, the advantage of the Select function is that it can listen for multiple file handles at the same time, increasing 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, and when the IO operation is actually completed, the application will be notified of the IO operation, at which point the user process needs only to process the data, without the need for actual IO read and write operations, Because the true IO read or write operation has been completed by the kernel. There is currently no support for this IO model in Java.

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

First look at the reactor mode, where the reactor mode applies to synchronous I/O scenarios. We take the reading and write operations as an example to see the specific steps in reactor:

Read operations:

1. Application registration read on-demand events and associated event handlers

2. Event separator Waiting for event to occur

3. The event separator invokes the event handler registered in the first step when an event is read on demand

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

A write operation is similar to a read operation, except that the first step registers a write-ready event.

Let's look at the process of read and write operations in Proactor mode:

Read operations:

1. The application initializes an asynchronous read operation and registers the appropriate event handler, at which point the event handler is not concerned with the read-ready event, but instead focuses on the read completion event, which is the key difference from the reactor.

2. Event separator waits for a read operation to complete an event

3. When the event separator waits for the read operation to complete, the operating system calls the kernel thread to complete the read operation and puts the read content into the cache that the user passes over. This is also a bit different from reactor, where the application needs to pass the buffer. Proactor

4. Event separator, after capturing the read completion event, activates the event handler registered by the application, and the event handler reads the data directly from the buffer, without the need for actual read operations.

Write operations and read operations in Proactor, except that the events of interest are write completion events.

As you can see from the above, the main difference between the reactor and Proactor modes is who the real read and write operations are, and reactor need the application to read or write the data themselves, and in Proactor mode, the application does not need to do the actual read and write process, It only needs to be read or written from the buffer, and the operating system reads the buffer or writes the buffer to the real IO device.

To sum up, synchronous and asynchronous are relative to the application and the kernel of the interaction, synchronization needs to actively ask, and asynchronous when the kernel in the IO event to notify the application, and blocking and non-blocking is only the system calls the system when the function of the implementation of the way.

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.