linux支持异步io吗,Linux 异步IO
io_submit、io_setup和io_getevents示例
[摘要:注:本宣布正在 io_submit、io_setup戰io_getevents戰LINUX上的AIO體系挪用。那有一個特別很是迥殊注重的中央——傳送給io_setup的aio_context參數必需初初化為0,正在它的man腳冊里實在有解釋]
注:原發表在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 successn");
else
printf("io_setup error: :%d:%sn", 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: %sn", strerror(errno));
char* buf;
errcode = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), sysconf(_SC_PAGESIZE));
printf("posix_memalign: %sn", 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:%sn", 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:%sn", 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);
總結
以上是生活随笔為你收集整理的linux支持异步io吗,Linux 异步IO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二元相图软件_FactSage 软件教程
- 下一篇: centos修改磁盘uuid_为什么My