io_submit、io_setup和io_getevents示例

简介: 注:原发表在Hadoop技术论坛 io_submit、io_setup和io_getevents是LINUX上的AIO系统调用。
注:原发表在Hadoop技术论坛

io_submit、io_setup和io_getevents是LINUX上的AIO系统调用。这有一个非常特别注意的地方——传递给io_setup的aio_context参数必须初始化为0,在它的man手册里其实有说明,但容易被忽视,我就犯了这个错误,man说明如下:
ctxp must not point to an  AIO context that already exists, and must be initialized to 0 prior to the call

完整示例如下:// 包含必须头文件
#include
#include
#include
#include
#include
#include
#include
#include
#include

int main()
{
        io_context_t ctx;
        unsigned nr_events = 10;
        memset(&ctx, 0, sizeof(ctx));  // It's necessary,这里一定要的
        int errcode = io_setup(nr_events, &ctx);
        if (errcode == 0)
                printf("io_setup success\n");
        else
                printf("io_setup error: :%d:%s\n", errcode, strerror(-errcode));

        // 如果不指定O_DIRECT,则io_submit操作和普通的read/write操作没有什么区别了,将来的LINUX可能
        // 可以支持不指定O_DIRECT标志
        int fd = open("./direct.txt", O_CREAT|O_DIRECT|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); // 这个值必须按512字节对齐
        iocbpp[0].u.c.offset = 0; // 这个值必须按512字节对齐

        // 提交异步操作,异步写磁盘
        int n = io_submit(ctx, 1, &iocbpp);
        printf("==io_submit==: %d:%s\n", n, strerror(-n));

        struct io_event events[10];
        struct timespec timeout = {1, 100};
        // 检查写磁盘情况,类似于epoll_wait或select
        n = io_getevents(ctx, 1, 10, events, &timeout);
        printf("io_getevents: %d:%s\n", n, strerror(-n));

        close(fd);
        io_destroy(ctx);
        return 0;
}


测试环境Linux 2.6.16,SUSE Linux Enterprise Server 10 (x86_64)

struct iocb {
       /* 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 */

struct io_event {
       __u64           data;          /* the data field from the iocb */ // 类似于epoll_event中的ptr
       __u64           obj;            /* what iocb this event came from */ // 对应的用户态iocb结构体指针
       __s64           res;            /* result code for this event */ // 操作的结果,类似于read/write的返回值
       __s64           res2;          /* secondary result */
};

系统调用功能原型io_setup为当前进程初始化一个异步IO上下文int io_setup(unsigned nr_events,aio_context_t *ctxp);io_submit提交一个或者多个异步IO操作int io_submit(aio_context_t ctx_id,long nr, struct iocb **iocbpp);io_getevents获得未完成的异步IO操作的状态int io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);io_cancel取消一个未完成的异步IO操作int io_cancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result);io_destroy从当前进程删除一个异步IO上下文int io_destroy(aio_context_t ctx); 
相关文章
|
1月前
|
存储 缓存 安全
Java 中 IO 流、File文件
Java 中 IO 流、File文件
|
17天前
|
Java Unix Windows
|
2天前
|
Java 开发者
Java一分钟之-Java IO流:文件读写基础
【5月更文挑战第10天】本文介绍了Java IO流在文件读写中的应用,包括`FileInputStream`和`FileOutputStream`用于字节流操作,`BufferedReader`和`PrintWriter`用于字符流。通过代码示例展示了如何读取和写入文件,强调了常见问题如未关闭流、文件路径、编码、权限和异常处理,并提供了追加写入与读取的示例。理解这些基础知识和注意事项能帮助开发者编写更可靠的程序。
8 0
|
6天前
|
存储 缓存 Java
Java IO 流详解
Java IO 流详解
16 1
|
11天前
|
存储 Java
java IO接口(Input)用法
【5月更文挑战第1天】Java的`java.io`包包含多种输入输出类。此示例展示了如何使用`FileInputStream`从`input.txt`读取数据。首先创建`FileInputStream`对象,接着创建一个字节数组存储读取的数据,调用`read()`方法将文件内容填充至数组。然后将字节数组转换为字符串并打印,最后关闭输入流。注意,`InputStream`是抽象类,此处使用其子类`FileInputStream`。其他子类如`ByteArrayInputStream`、`ObjectInputStream`和`BufferedInputStream`各有特定用途。
21 2
|
13天前
|
存储 Java Linux
【Java EE】 文件IO的使用以及流操作
【Java EE】 文件IO的使用以及流操作
|
18天前
|
存储 Java 数据库
[Java 基础面试题] IO相关
[Java 基础面试题] IO相关
|
18天前
|
缓存 Java API
Java NIO和IO之间的区别
NIO(New IO),这个库是在JDK1.4中才引入的。NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多。在Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。
16 1
|
21天前
|
Java
Java基础教程(12)-Java中的IO流
【4月更文挑战第12天】Java IO涉及输入输出,包括从外部读取数据到内存(如文件、网络)和从内存输出到外部。流是信息传输的抽象,分为字节流和字符流。字节流处理二进制数据,如InputStream和OutputStream,而字符流处理Unicode字符,如Reader和Writer。File对象用于文件和目录操作,Path对象简化了路径处理。ZipInputStream和ZipOutputStream则用于读写zip文件。