"Turn" reactor and proactor two modes of difference

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/cbscan/articles/2107494.html

Two IO multiplexing scenarios: Reactor and Proactor

In general, the I/O multiplexing mechanism requires event Demultiplexor [1, 3]. The role of event sharing, will be those who read and write event source distributed to the processing of read and write events, like the delivery of a courier downstairs shouting: Who sent something, come and take it. The developer needs to register the event of interest at the beginning, and provide the appropriate handler (event handlers), or a callback function; The event-sharing device distributes the requested events to these handler or callback functions when appropriate.

Two modes involving event-sharing are called: Reactor and Proactor [1]. The reactor mode is based on synchronous I/O, while the proactor mode is associated with asynchronous I/O. In the reactor mode, the event splitter waits for an event or the state of the application or operation to occur (such as the file descriptor can read or write, or the socket can read and write), the event splitter will pass this event to the pre-registered event handler function or callback function, the latter to do the actual read and write operations.

In Proactor mode, an event handler (or a generation initiated by an event splitter) directly initiates an asynchronous read-write operation (equivalent to a request), and the actual work is done by the operating system. At the time of initiation, the parameters to be provided include the buffers used to hold the data read, the size of the data being read, or the buffers used to store the outgoing data, and the callback function after the request. The event Splitter learns the request, silently waiting for the request to complete, and then forwards the completion event to the appropriate event handler or callback. [1] For example, an event processor on Windows posted an asynchronous IO operation (known as overlapped technology), event Splitter, and so on IoCompletion event completion. The typical implementation of this asynchronous pattern is based on the underlying asynchronous API of the operating system, so we can call it "system-level" or "really" asynchronous because the specific read and write is handled by the operating system.

Give another example to better understand the difference between the two modes of reactor and Proactor. Here we only focus on the read operation, because the write operation is similar. Here is the reactor approach:

    • An event handler claims that it is interested in reading events on a socket;
    • The event separator is waiting for the event to occur;
    • When an event occurs, the event splitter is awakened, which notifies the previous event handler;
    • The event handler receives the message and then goes to the socket to read the data. If needed, it again claims to be interested in the read event on the socket, repeating the above steps;

Let's take a look at how the true meaning of asynchronous mode Proactor is done:

    • The event handler sends a write operation directly (of course, the operating system must support this asynchronous operation). At this point, the event handler simply does not care about reading the event, it just sends the request, it is the completion of this write operation event . This handler is very drag, send a command, regardless of the specific things, just waiting for someone else (System) to help him fix the time to give him back a message.
    • The event splitter waits for the completion of this read event (compared to the reactor);
    • When the event separator silently waiting to complete the matter, the operating system has started to work on the side, it reads data from the target, into the user-provided buffer, and finally notify the event separator, this thing I finished;
    • Event-sharing notification before the event handler: the things you commanded were settled;
    • The event handler will then find that the data he wants to read is already in the cache that he has provided, and what to do with it. If necessary, the event handler initiates another write operation as before, as in the previous steps.

"Turn" reactor and proactor two modes of difference

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.