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

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

发布时间:2021-01-24 16:31 所属栏目:118 来源:网络整理
导读:This function is called only after the? aio_error ?call has determined that your request has completed (either successfully or in error). The return value of? aio_return ?is identical to that of the?

This function is called only after the?aio_error?call has determined that your request has completed (either successfully or in error). The return value of?aio_return?is identical to that of the?read?or?write?system call in a synchronous context (number of bytes transferred or?-1?for error).

The?aio_write?function is used to request an asynchronous write. Its function prototype is:

aio_write( struct aiocb *aiocbp );

The?aio_write?function returns immediately,indicating that the request has been enqueued (with a return of?0?on success and?-1?on failure,witherrno?properly set).

This is similar to the?read?system call,but one behavior difference is worth noting. Recall that the offset to be used is important with the?read?call. However,with?write,the offset is important only if used in a file context where the?O_APPEND?option is not set. If?O_APPEND?is set,then the offset is ignored and the data is appended to the end of the file. Otherwise,the?aio_offset?field determines the offset at which the data is written to the file.

You can use the?aio_suspend?function to suspend (or block) the calling process until an asynchronous I/O request has completed,a signal is raised,or an optional timeout occurs. The caller provides a list of?aiocb?references for which the completion of at least one will causeaio_suspend?to return. The function prototype for?aio_suspend?is:

aio_suspend( const struct aiocb *const cblist[],int n,const struct timespec *timeout );

Using?aio_suspend?is quite simple. A list of?aiocb?references is provided. If any of them complete,the call returns with?0. Otherwise,?-1?is returned,indicating an error occurred. See Listing 3.

/ Clear the list. /
bzero( (char *)cblist,sizeof(cblist) );

/ Load one or more references into the list /
cblist[0] = &my_aiocb;

ret = aio_read( &my_aiocb );

ret = aio_suspend( cblist,MAX_LIST,NULL );

Note that the second argument of?aio_suspend?is the number of elements in?cblist,not the number of?aiocb?references. Any?NULL?element in the?cblist?is ignored by?aio_suspend.

If a timeout is provided to?aio_suspend?and the timeout occurs,then?-1is returned and?errno?contains?EAGAIN.

The?aio_cancel?function allows you to cancel one or all outstanding I/O requests for a given file descriptor. Its prototype is:

aio_cancel( int fd,struct aiocb *aiocbp );

To cancel a single request,provide the file descriptor and the?aiocb?reference. If the request is successfully cancelled,the function returnsAIO_CANCELED. If the request completes,the function returns?AIO_NOTCANCELED.

To cancel all requests for a given file descriptor,provide that file descriptor and a?NULL?reference for?aiocbp. The function returns?AIO_CANCELEDif all requests are canceled,?AIO_NOT_CANCELED?if at least one request couldn't be canceled,and?AIO_ALLDONE?if none of the requests could be canceled. You can then evaluate each individual AIO request using?aio_error. If the request was canceled,?aio_error?returns?-1,and?errno?is set to?ECANCELED.

Finally,AIO provides a way to initiate multiple transfers at the same time using the?lio_listio?API function. This function is important because it means you can start lots of I/Os in the context of a single system call (meaning one kernel context switch). This is great from a performance perspective,so it's worth exploring. The?lio_listio?API function has the following prototype:

lio_listio( int mode,struct aiocb *list[],int nent,struct sigevent *sig );

The?mode?argument can be?LIO_WAIT?or?LIO_NOWAIT.?LIO_WAIT?blocks the call until all I/O has completed.?LIO_NOWAIT?returns after the operations have been queued. The?list?is a list of?aiocb?references,with the maximum number of elements defined by?nent. Note that elements of?list?may be?NULL,which?lio_listio?ignores. The?sigevent?reference defines the method for signal notification when all I/O is complete.

(编辑:ASP站长网)

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