天天看點

io_submit、io_setup和io_getevents示例

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

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); 

繼續閱讀