io_submit、io_setup和io_getevents示例
io_submit、io_setup和io_getevents和LINUX上的AIO系统调用。这有一个非常特别注意的地方——传递给io_setup的aio_context参数必须初始化为0,在它的man手册里其实有说明,但容易被忽视,我就犯了这个错误,man说明如下:[table=98%][tr][td]ctxp must not point to an AIO context that already exists, and must be initialized to 0 prior to the call[/td][/tr]
[/table]
完整示例如下:[color=#FF0000]// 包含必须头文件[/color]
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libaio.h>
int main()
{
io_context_t ctx;
unsigned nr_events = 10;
[color=#0000FF]memset(&ctx, 0, sizeof(ctx));[/color] [color=#FF0000]// It's necessary,这里一定要的[/color]
int errcode = [b]io_setup[/b](nr_events, &ctx);
if (errcode == 0)
printf("io_setup success\n");
else
printf("io_setup error: :%d:%s\n", errcode, strerror(-errcode));
// [b]如果不指定[color=#0000FF]O_DIRECT[color=#444444],则io_submit操作和普通的read/write操作没有什么区别了,将来的LINUX可能[/color][/color][/b]
[b][color=#0000FF][color=#444444] // 可以支持不指定[b][color=#0000FF]O_DIRECT[color=#444444][b][color=#0000FF][color=#444444]标志[/color][/color][/b][/color][/color][/b][/color][/color][/b]
int fd = open("./direct.txt", O_CREAT|[b][color=#0000FF]O_DIRECT[/color][/b]|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);
printf("open: %s\n", strerror(errno));
char* buf;
errcode = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), sysconf(_SC_PAGESIZE));
printf("posix_memalign: %s\n", strerror(errcode));
strcpy(buf, "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
struct iocb *iocbpp = (struct iocb *)malloc(sizeof(struct iocb));
memset(iocbpp, 0, sizeof(struct iocb));
iocbpp[0].data = buf;
iocbpp[0].aio_lio_opcode = IO_CMD_PWRITE;
iocbpp[0].aio_reqprio = 0;
iocbpp[0].aio_fildes = fd;
iocbpp[0].u.c.buf = buf;
iocbpp[0].u.c.nbytes = page_size;//strlen(buf); [color=#FF0000]// 这个值必须按512字节对齐[/color]
iocbpp[0].u.c.offset = 0; [color=#FF0000]// 这个值必须按512字节对齐[/color]
[color=#FF0000]// 提交异步操作,异步写磁盘[/color]
int n = [b]io_submit[/b](ctx, 1, &iocbpp);
printf("==io_submit==: %d:%s\n", n, strerror(-n));
struct io_event events[10];
struct timespec timeout = {1, 100};
[color=#FF0000]// 检查写磁盘情况,类似于epoll_wait或select[/color]
n = [b]io_getevents[/b](ctx, 1, 10, events, &timeout);
printf("io_getevents: %d:%s\n", n, strerror(-n));
close(fd);
[b]io_destroy[/b](ctx);
return 0;
}
[b]测试环境:[/b]Linux 2.6.16,SUSE Linux Enterprise Server 10 (x86_64) struct [b]iocb [/b]{
/* these are internal to the kernel/libc. */
__u64 aio_data; /* data to be returned in event\'s data */用来返回异步IO事件信息的空间,类似于epoll中的ptr。
__u32 PADDED(aio_key, aio_reserved1); /* the kernel sets aio_key to the req # */
/* common fields */
__u16 aio_lio_opcode; /* see IOCB_CMD_ above */
__s16 aio_reqprio; // 请求的优先级
__u32 aio_fildes; // 文件描述符
__u64 aio_buf; // 用户态缓冲区
__u64 aio_nbytes; // 文件操作的字节数
__s64 aio_offset; // 文件操作的偏移量
/* extra parameters */
__u64 aio_reserved2; /* TODO: use this for a (struct sigevent *) */
__u64 aio_reserved3;
}; /* 64 bytes */
[color=#000][font=arial, sans-serif]struct [b]io_event [/b]{
__u64 data; /* the data field from the iocb */ // [b][color=#FF0000]类似于epoll_event中的ptr[/color][/b]
__u64 obj; /* what iocb this event came from */ // 对应的用户态iocb结构体指针
__s64 res; /* result code for this event */ // 操作的结果,[b][color=#FF0000]类似于read/write的返回值[/color][/b]
__s64 res2; /* secondary result */
};[/font][/color] [table=98%]
[tr][td][b] 系统调用[/b][/td][td][b] 功能[/b][/td][td][b] 原型[/b][/td][/tr]
[tr][td] io_setup[/td][td] 为当前进程初始化一个异步IO上下文[/td][td] int io_setup(unsigned nr_events,aio_context_t *ctxp);[/td][/tr]
[tr][td] io_submit[/td][td] 提交一个或者多个异步IO操作[/td][td] int io_submit(aio_context_t ctx_id,long nr, struct iocb **iocbpp);[/td][/tr]
[tr][td] io_getevents[/td][td] 获得未完成的异步IO操作的状态[/td][td] int io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);[/td][/tr]
[tr][td] io_cancel[/td][td] 取消一个未完成的异步IO操作[/td][td] int io_cancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result);[/td][/tr]
[tr][td] io_destroy[/td][td] 从当前进程删除一个异步IO上下文[/td][td] int io_destroy(aio_context_t ctx);[/td][/tr]
[/table] [attach]265[/attach] 强烈建议阅读《内核阅读心得》
[attach]266[/attach] 不错,感谢分享 谢谢了,这些资料很宝贵。
页:
[1]