设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Boost application performance using asynchronous I/O-ref(2)

发布时间:2021-01-24 16:31 所属栏目:118 来源:网络整理
导读:Finally,the asynchronous non-blocking I/O model is one of overlapping processing with I/O. The read request returns immediately,indicating that the? read ?was successfully initiated. The application

Finally,the asynchronous non-blocking I/O model is one of overlapping processing with I/O. The read request returns immediately,indicating that the?read?was successfully initiated. The application can then perform other processing while the background read operation completes. When theread?response arrives,a signal or a thread-based callback can be generated to complete the I/O transaction.

Typical Flow of the Asynchronous Non-Blocking I/O Model (AIO)

The ability to overlap computation and I/O processing in a single process for potentially multiple I/O requests exploits the gap between processing speed and I/O speed. While one or more slow I/O requests are pending,the CPU can perform other tasks or,more commonly,operate on already completed I/Os while other I/Os are initiated.

The next section examines this model further,explores the API,and then demonstrates a number of the commands.

From the previous taxonomy of I/O models,you can see the motivation for AIO. The blocking models require the initiating application to block when the I/O has started. This means that it isn't possible to overlap processing and I/O at the same time. The synchronous non-blocking model allows overlap of processing and I/O,but it requires that the application check the status of the I/O on a recurring basis. This leaves asynchronous non-blocking I/O,which permits overlap of processing and I/O,including notification of I/O completion.

The functionality provided by the?select?function (asynchronous blocking I/O) is similar to AIO,except that it still blocks. However,it blocks on notifications instead of the I/O call.

This section explores the asynchronous I/O model for Linux to help you understand how to apply it in your applications.

In a traditional I/O model,there is an I/O channel that is identified by a unique handle. In UNIX?,these are file descriptors (which are the same for files,pipes,sockets,and so on). In blocking I/O,you initiate a transfer and the system call returns when it's complete or an error has occurred.

AIO first entered the Linux kernel in 2.5 and is now a standard feature of 2.6 production kernels.

In asynchronous non-blocking I/O,you have the ability to initiate multiple transfers at the same time. This requires a unique context for each transfer so you can identify it when it completes. In AIO,this is an?aiocb?(AIO I/O Control Block) structure. This structure contains all of the information about a transfer,including a user buffer for data. When notification for an I/O occurs (called a completion),the?aiocb?structure is provided to uniquely identify the completed I/O. The API demonstration shows how to do this.

The AIO interface API is quite simple,but it provides the necessary functions for data transfer with a couple of different notification models. Table 1 shows the AIO interface functions,which are further explained later in this section.

API function aio_readaio_erroraio_returnaio_writeaio_suspendaio_cancellio_listio Each of these API functions uses the?aiocb?structure for initiating or checking. This structure has a number of elements,but Listing 1 shows only the ones that you'll need to (or can) use.

int aio_fildes; // File Descriptor
int aio_lio_opcode; // Valid only for lio_listio (r/w/nop)
volatile void *aio_buf; // Data Buffer
size_t aio_nbytes; // Number of Bytes in Data Buffer
struct sigevent aio_sigevent; // Notification Structure

/ Internal fields /
...

};

(编辑:ASP站长网)

Description
网友评论
推荐文章
    热点阅读