工作代码片段-aio

来源:岁月联盟 编辑:exp 时间:2011-12-31

aio是linux上的异步IO实现,具体测试代码如下:

aiocb ab; 
bzero( (char *)&ab, sizeof(struct aiocb) ); 
int fd = open("a.txt", O_RDWR | O_APPEND); 
ab.aio_buf = malloc(201); 
ab.aio_fildes = fd; 
ab.aio_nbytes = 20; 
ab.aio_offset = 0; 
int r = aio_read(&ab); 
std::cout << "return:" << r << std::endl; 
std::cout << "buff:" << (char *)ab.aio_buf << std::endl; 
r = aio_return(&ab); 
std::cout << "return:" << r << std::endl; 
char *b = "good boy!!"; 
ab.aio_buf = b; 
aio_write(&ab); 

关键点在于,struct aiocb结构体的填充。


摘自 缘起