libaio简介
Posted ygtff
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libaio简介相关的知识,希望对你有一定的参考价值。
liaio介绍 linux kernel 提供了5个系统调用来实现异步IO。文中最后介绍的是包装了这些系统调用的用户空间的函数。 libaio系统调用 AIO 系统调用总共五个,后面会一一介绍。* int io_setup ( unsigned nr_events , aio_context_t * ctxp ); * int io_destroy ( aio_context_t ctx ); * int io_submit ( aio_context_t ctx , long nr , struct iocb * cbp []); * int io_cancel ( aio_context_t ctx , struct iocb *, struct io_event * result ); * int io_getevents ( aio_context_t ctx , long min_nr , long nr , struct io_event *events, struct timespec *timeout);
1.异步IO上下文 aio_context_t
>> aio_context_t.c >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define _GNU_SOURCE /* syscall() is not POSIX */系统调用io_setup会创建一个所谓的"AIO上下文"(即aio_context,后文也叫‘ AIO context’等) 结构体到在内核中。 aio_context是用以内核实现异步AIO的数据结构。它其实是一个无符号整形,位于头文件 /usr/include/linux/aio_abi.h 。
#include <stdio.h> /* for perror() */ #include <unistd.h> /* for syscall() */ #include <sys/syscall.h> /* for __NR_* definitions */ #include <linux/aio_abi.h> /* for AIO types and constants */
inline int io_setup ( unsigned nr , aio_context_t * ctxp ) return syscall ( __NR_io_setup , nr , ctxp );
inline int io_destroy ( aio_context_t ctx ) return syscall ( __NR_io_destroy , ctx ); int main () aio_context_t ctx ; int ret ; ctx = 0 ; ret = io_setup ( 128 , & ctx ); if ( ret < 0 ) perror ( "io_setup error" ); return - 1 ; printf ( "after io_setup ctx:%Ld\\n" , ctx ); ret = io_destroy ( ctx ); if ( ret < 0 ) perror ( "io_destroy error" ); return - 1 ; printf ( "after io_destroy ctx:%Ld\\n" , ctx ); return 0 ;
每个进程都可以有多个 aio_context_t。传入io_setup的第一个参数在这里是128,表示同时驻留在上下文中的IO请求的个数;第二个参数是一个指针,内核会填充这个值。 io_destroy的作用是销毁这个上下文 aio_context_t 上面的例子很简单,创建一个 aio_context_t并销毁。 编译运行,编译时需要连接库libaio(-laio):typedef unsigned long aio_context_t;
$ gcc - Wall aio_context_t . c - o aio_context_t - laio $ ./ aio_context_t after io_setup ctx : 139730712117248 after io_destroy ctx : 139730712117248
2.提交并查询IO
#define _GNU_SOURCE /* syscall() is not POSIX */
#include <stdio.h> /* for perror() */
#include <unistd.h> /* for syscall() */
#include <sys/syscall.h> /* for __NR_* definitions */
#include <linux/aio_abi.h> /* for AIO types and constants */
#include <fcntl.h> /* O_RDWR */
#include <string.h> /* memset() */
#include <inttypes.h> /* uint64_t */
inline int io_setup ( unsigned nr , aio_context_t * ctxp )
return syscall ( __NR_io_setup , nr , ctxp );
inline int io_destroy ( aio_context_t ctx )
return syscall ( __NR_io_destroy , ctx );
inline int io_submit ( aio_context_t ctx , long nr , struct iocb ** iocbpp )
return syscall ( __NR_io_submit , ctx , nr , iocbpp );
inline int io_getevents ( aio_context_t ctx , long min_nr , long max_nr ,
struct io_event * events , struct timespec * timeout )
return syscall ( __NR_io_getevents , ctx , min_nr , max_nr , events , timeout );
int main ()
aio_context_t ctx ;
struct iocb cb ;
struct iocb * cbs [ 1 ];
char data [ 4096 ];
struct io_event events [ 1 ];
int ret ;
int fd ;
int i ;
for ( i = 0 ; i < 4096 ; i ++)
data [ i ]= i % 50 + 60 ;
fd = open ( "./testfile" , O_RDWR | O_CREAT , S_IRWXU );
if ( fd < 0 )
perror ( "open error" );
return - 1 ;
ctx = 0 ;
ret = io_setup ( 128 , & ctx );
printf ( "after io_setup ctx:%ld" , ctx );
if ( ret < 0 )
perror ( "io_setup error" );
return - 1 ;
/* setup I/O control block */
memset (& cb , 0 , sizeof ( cb ));
cb . aio_fildes = fd ;
cb . aio_lio_opcode = IOCB_CMD_PWRITE ;
/* command-specific options */
cb . aio_buf = ( uint64_t ) data ;
cb . aio_offset = 0 ;
cb . aio_nbytes = 4096 ;
cbs [ 0 ] = & cb ;
ret = io_submit ( ctx , 1 , cbs ); if ( ret != 1 )
if ( ret < 0 )
perror ( "io_submit error" );
else
fprintf ( stderr , "could not sumbit ios" );
return - 1 ;
/* get the reply */
ret = io_getevents ( ctx , 1 , 1 , events , NULL );
printf ( "%d\\n" , ret );
struct iocb * result = ( struct iocb *) events [ 0 ]. obj ;
printf ( "reusult:%Ld" , result -> aio_buf );
ret = io_destroy ( ctx );
if ( ret < 0 )
perror ( "io_destroy error" );
Mysql出现问题:mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object解决方案error while loading shared libraries: libaio.so.1
Mysql开发实践:error while loading shared libraries: libaio解决方案