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

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

发布时间:2021-01-24 16:31 所属栏目:118 来源:网络整理
导读:The request for? lio_listio ?is slightly different than the typical? read ?or? write ?request in that the operation must be specified. This is illustrated in Listing 4. ... / Prepare the first aiocb

The request for?lio_listio?is slightly different than the typical?read?or?write?request in that the operation must be specified. This is illustrated in Listing 4.

...

/ Prepare the first aiocb /
aiocb1.aio_fildes = fd;
aiocb1.aio_buf = malloc( BUFSIZE+1 );
aiocb1.aio_nbytes = BUFSIZE;
aiocb1.aio_offset = next_offset;
aiocb1.aio_lio_opcode = LIO_READ;

...

bzero( (char *)list,sizeof(list) );
list[0] = &aiocb1;
list[1] = &aiocb2;

ret = lio_listio( LIO_WAIT,list,NULL );

The read operation is noted in the?aio_lio_opcode?field with?LIO_READ. For a write operation,?LIO_WRITE?is used,but?LIO_NOP?is also valid for no operation.

Now that you've seen the AIO functions that are available,this section digs into the methods that you can use for asynchronous notification. I'll explore asynchronous notification through signals and function callbacks.

The use of signals for interprocess communication (IPC) is a traditional mechanism in UNIX and is also supported by AIO. In this paradigm,the application defines a signal handler that is invoked when a specified signal occurs. The application then specifies that an asynchronous request will raise a signal when the request has completed. As part of the signal context,the particular?aiocb?request is provided to keep track of multiple potentially outstanding requests. Listing 5 demonstrates this notification method.

...

/ Set up the signal handler /
sigemptyset(&sig_act.sa_mask);
sig_act.sa_flags = SA_SIGINFO;
sig_act.sa_sigaction = aio_completion_handler;

/ Set up the AIO request /
bzero( (char *)&my_aiocb,sizeof(struct aiocb) );
my_aiocb.aio_fildes = fd;
my_aiocb.aio_buf = malloc(BUF_SIZE+1);
my_aiocb.aio_nbytes = BUF_SIZE;
my_aiocb.aio_offset = next_offset;

/ Link the AIO request with the Signal Handler /
my_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
my_aiocb.aio_sigevent.sigev_signo = SIGIO;
my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;

/ Map the Signal to the Signal Handler /
ret = sigaction( SIGIO,&sig_act,NULL );

...

ret = aio_read( &my_aiocb );

}

void aio_completion_handler( int signo,siginfo_t info,void context )
{
struct aiocb *req;

/ Ensure it's our signal /
if (info->si_signo == SIGIO) {

req = (struct aiocb *)info->si_value.sival_ptr;

/* Did the request complete? */
if (<strong>aio_error</strong>( req ) == 0) {

  /* Request completed successfully,get the return status */
  ret = <strong>aio_return</strong>( req );

}

}

return;
}

In Listing 5,you set up your signal handler to catch the?SIGIO?signal in the?aio_completion_handler?function. You then initialize theaio_sigevent?structure to raise?SIGIO?for notification (which is specified via the?SIGEV_SIGNAL?definition in?sigev_notify). When your read completes,your signal handler extracts the particular?aiocb?from the signal's?si_value?structure and checks the error status and return status to determine I/O completion.

For performance,the completion handler is an ideal spot to continue the I/O by requesting the next asynchronous transfer. In this way,when completion of one transfer has completed,you immediately start the next.

An alternative notification mechanism is the system callback. Instead of raising a signal for notification,this mechanism calls a function in user-space for notification. You initialize the?aiocb?reference into the?sigevent?structure to uniquely identify the particular request being completed; see Listing 6.

...

/ Set up the AIO request /
bzero( (char *)&my_aiocb,sizeof(struct aiocb) );
my_aiocb.aio_fildes = fd;
my_aiocb.aio_buf = malloc(BUF_SIZE+1);
my_aiocb.aio_nbytes = BUF_SIZE;
my_aiocb.aio_offset = next_offset;

(编辑:ASP站长网)

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