Proactor 学习2

Comparing Two High-Performance I/O Design Patterns

by Alexander Libman with Vladimir Gilbourd

Reactor and Proactor: two I/O multiplexing approaches

In general, I/O multiplexing mechanisms rely on an event demultiplexor,an object that dispatches I/O events from a limited
number of sources to the appropriate read/write event handlers.

The developer registers interest in specific
events and provides event handlers, or callbacks. The event demultiplexor delivers the requested events to the event handlers.

Two
patterns that involve event demultiplexors are called Reactor and Proactor 。

The
Reactor patterns involve synchronous I/O, whereas the Proactor pattern involves asynchronous I/O.

In Reactor, the event demultiplexor waits for events that indicate when a file descriptor or
socket is ready for a read or write operation.

The demultiplexor passes this event to the
appropriate handler, which is responsible for performing the actual read or write.

In
the Proactor pattern, by contrast, the handler—or the event demultiplexor on behalf of the handler—initiates asynchronous read and write operations.The
I/O operation itself is performed by the operating system (OS). The parameters passed to the OS include the addresses of user-defined data buffers from which the OS gets data to write, or to which the OS puts data read.The
event demultiplexor waits for events that indicate the completion of the I/O operation, and forwards those events to the appropriate handlers.

For example, on Windows a handler could initiate async I/O (overlapped in Microsoft terminology)
operations, and the event demultiplexor could wait for IOCompletion events 。The implementation of this classic asynchronous pattern is based on an asynchronous OS-level API, and we will call this implementation
the "system-level" or "true" async, because the application fully relies on the OS to execute actual I/O.

An
example will help you understand the difference between Reactor and Proactor. We will focus on the read operation here, as the write implementation is similar. Here‘s a read in Reactor:

  • An event handler declares interest in I/O events that indicate readiness for read on a particular socket
  • The event demultiplexor waits for events
  • An event comes in and wakes-up the demultiplexor, and the demultiplexor calls the appropriate handler
  • The event handler performs the actual read operation, handles the data read, declares renewed interest in I/O events, and returns control to the dispatcher

By comparison, here is a read operation in Proactor (true async):

  • A handler initiates an asynchronous read operation (note: the OS must support asynchronous I/O). In this case, the handler does not care about I/O readiness events, but is instead registers interest in receiving completion events.
  • The event demultiplexor waits until the operation is completed
  • While the event demultiplexor waits, the OS executes the read operation in a parallel kernel thread, puts data into a user-defined buffer, and notifies the event demultiplexor that the read is complete
  • The event demultiplexor calls the appropriate handler;
  • The event handler handles the data from user defined buffer, starts a new asynchronous operation, and returns control to the event demultiplexor.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 14:11:02

Proactor 学习2的相关文章

Proactor 学习1

Proactor    An Object Behavioral Pattern for Demultiplexingand Dispatching Handlers for Asynchronous Events Douglas C. Schmidt Known Uses The following are some widely documented uses of the Proctor pattern: I/O Completion Ports in Windows NT: The Wi

Linux C++学习之路(转自网络)

Module01 - Linux系统基础 由于本系列课程基于Linux(或UNIX),熟悉Linux操作系统是必要的前提. 该模块的课程包含以下方面的内容: 常用Unix/Linux命令    熟悉文件管理.文本处理.进程管理.网络.系统管理等各个方面大约100个常用的命令.    深入了解bash    了解Linux默认shell: bash 的语法.命令执行.I/O重定向.任务控制等.    正则表达式基础    由于UNIX/Linux中很多强大的文本处理命令如:grep.awk.sed

两种高性能 I/O 设计模式 Reactor 和 Proactor

Reactor 和 Proactor 是基于事件驱动,在网络编程中经常用到两种设计模式. 曾经在一个项目中用到了网络库 libevent,也学习了一段时间,其内部实现所用到的就是 Reactor,所知道的还有 ACE:Proactor 模式的库有 Boost.Asio,ACE,暂时没有用过.但我也翻阅了一些文档,理解了它的实现方法.下面是我在学习这两种设计模式过程的笔记. Reactor Reactor,即反应堆.Reactor 的一般工作过程是首先在 Reactor 中注册(Reactor)感

Libevent学习之SocketPair实现

Libevent设计的精化之一在于把Timer事件.Signal事件和IO事件统一集成在一个Reactor中,以统一的方式去处理这三种不同的事件,更确切的说是把Timer事件和Signal事件融合到了IO多路复用机制中. Timer事件的融合相对清晰简单,其套用了Reactor和Proactor模式(如Windows上的IOCP)中处理Timer事件的经典方法,其实Libevent就是一个Reactor嘛.由于IO复用机制(如Linux下的select.epoll)允许使用一个最大等待时间(即最

转: IO设计模式:Reactor和Proactor对比

转: https://segmentfault.com/a/1190000002715832 平时接触的开源产品如Redis.ACE,事件模型都使用的Reactor模式:而同样做事件处理的Proactor,由于操作系统的原因,相关的开源产品也少:这里学习下其模型结构,重点对比下两者的异同点: 反应器Reactor Reactor模式结构 Reactor包含如下角色: Handle 句柄:用来标识socket连接或是打开文件: Synchronous Event Demultiplexer:同步事

设计模式-前摄器模式(Proactor)

本周要进行boost asio库的学习,在学习之前发现最好需要先了解一下前摄器模式,这样对asio库的理解很有帮助,故写下此文 我之前写的随笔XShell的模拟实现中的链接方式可以说是同步的(服务器阻塞等待链接),这样当有服务器端在等待链接的时候就浪费了大量的资源,我们可以让服务器异步等待客户端的链接,服务器在等待链接的同时可以做别的事情,等到客户端链接请求到来的时候,调用一个回调执行链接,这就很灵活. 先来一段关于前摄器模式的官话:前摄器模式支持多个事件处理器的多路分离和分派,这些处理器由异步

IO设计模式:Reactor和Proactor对比

IO设计模式:Reactor和Proactor对比 平时接触的开源产品如Redis.ACE,事件模型都使用的Reactor模式:而同样做事件处理的Proactor,由于操作系统的原因,相关的开源产品也少:这里学习下其模型结构,重点对比下两者的异同点: 反应器Reactor Reactor模式结构 Reactor包含如下角色: Handle 句柄:用来标识socket连接或是打开文件: Synchronous Event Demultiplexer:同步事件多路分解器:由操作系统内核实现的一个函数

转载:reactor模式学习

最近又重新看了下netty背后的设计思想,接触到了reactor模型.发现以前虽然也看过reactor和proactor模型的介绍,但是真的是只了解了个皮毛. 再重新学习了一遍,有了更深刻的认识.但是手边并没有实际的项目再用这类技术,所以这次学习只是理论上理解的更深刻了,也没有实际使用经验. 那就...转载一些文章: 1. 转载自并发编程网 – ifeve.com本文链接地址: http://ifeve.com/netty-reactor-4/ 2. http://www.infoq.com/c

linux 网络编程需要学习的内容

Linux C++培训发 课程模块 Linux C++全科班课程由以下模块组成: Module01 - Linux系统基础 由于本系列课程基于Linux(或UNIX),熟悉Linux操作系统是必要的前提. 该模块的课程包含以下方面的内容: 常用Unix/Linux命令熟悉文件管理.文本处理.进程管理.网络.系统管理等各个方面大约100个常用的命令. 深入了解bash了解Linux默认shell: bash 的语法.命令执行.I/O重定向.任务控制等. 正则表达式基础由于UNIX/Linux中很多