加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Boost application performance using asynchronous I/O-ref

发布时间:2021-01-29 10:10:05 所属栏目:站长百科 来源:网络整理
导读:副标题#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 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.

(编辑:网站开发网_马鞍山站长网)

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