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

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

发布时间:2021-01-24 16:31 所属栏目:118 来源:网络整理
导读:The? sigevent ?structure tells AIO what to do when the I/O completes. You'll explore this structure in the AIO demonstration. Now I'll show you how the individual API functions for AIO work and how y

The?sigevent?structure tells AIO what to do when the I/O completes. You'll explore this structure in the AIO demonstration. Now I'll show you how the individual API functions for AIO work and how you can use them.

The?aio_read?function requests an asynchronous read operation for a valid file descriptor. The file descriptor can represent a file,a socket,or even a pipe. The?aio_read?function has the following prototype:

aio_read( struct aiocb *aiocbp );

The?aio_read?function returns immediately after the request has been queued. The return value is zero on success or -1 on error,where?errnois defined.

To perform a read,the application must initialize the?aiocb?structure. The following short example illustrates filling in the?aiocb?request structure and using?aio_read?to perform an asynchronous read request (ignore notification for now). It also shows use of the?aio_error?function,but I'll explain that later.

...

int fd,ret;
struct aiocb my_aiocb;

fd = open( "file.txt",O_RDONLY );
if (fd < 0) perror("open");

/ Zero out the aiocb structure (recommended) /
bzero( (char *)&my_aiocb,sizeof(struct aiocb) );

/ Allocate a data buffer for the aiocb request /
my_aiocb.aio_buf = malloc(BUFSIZE+1);
if (!my_aiocb.aio_buf) perror("malloc");

/ Initialize the necessary fields in the aiocb /
my_aiocb.aio_fildes = fd;
my_aiocb.aio_nbytes = BUFSIZE;
my_aiocb.aio_offset = 0;

ret = aio_read( &my_aiocb );
if (ret < 0) perror("aio_read");

while ( aio_error( &my_aiocb ) == EINPROGRESS ) ;

if ((ret = aio_return( &my_iocb )) > 0) {
/ got ret bytes on the read /
} else {
/ read failed,consult errno /
}

In Listing 2,after the file from which you're reading data is opened,you zero out your?aiocb?structure,and then allocate a data buffer. The reference to the data buffer is placed into?aio_buf. Subsequently,you initialize the size of the buffer into?aio_nbytes. The?aio_offset?is set to zero (the first offset in the file). You set the file descriptor from which you're reading into?aio_fildes. After these fields are set,you call?aio_readto request the read. You can then make a call to?aio_error?to determine the status of the?aio_read. As long as the status is?EINPROGRESS,you busy-wait until the status changes. At this point,your request has either succeeded or failed.

You can find the function prototypes and other necessary symbolics in the?aio.h?header file. When building an application that uses this interface,you must use the POSIX real-time extensions library (librt).

Note the similarities to reading from the file with the standard library functions. In addition to the asynchronous nature of?aio_read,another difference is setting the offset for the read. In a typical?read?call,the offset is maintained for you in the file descriptor context. For each read,the offset is updated so that subsequent reads address the next block of data. This isn't possible with asynchronous I/O because you can perform many read requests simultaneously,so you must specify the offset for each particular read request.

The?aio_error?function is used to determine the status of a request. Its prototype is:

aio_error( struct aiocb *aiocbp );

This function can return the following:

    EINPROGRESS,indicating the request has not yet completed
  • ECANCELLED,indicating the request was cancelled by the application
  • -1,indicating that an error occurred for which you can consult?errno

Another difference between asynchronous I/O and standard blocking I/O is that you don't have immediate access to the return status of your function because you're not blocking on the?read?call. In a standard?read?call,the return status is provided upon return of the function. With asynchronous I/O,you use the?aio_return?function. This function has the following prototype:

aio_return( struct aiocb *aiocbp );

(编辑:ASP站长网)

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