加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Boost application performance using asynchronous I/O-ref

发布时间:2021-01-24 20:21:15 所属栏目:Linux 来源:网络整理
导读:副标题#e# Linux asynchronous I/O is a relatively recent addition to the Linux kernel. It's a standard feature of the 2.6 kernel,but you can find patches for 2.4. The basic idea behind AIO is to allow a process to initiate a number of I/O

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 );

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读