-
Notifications
You must be signed in to change notification settings - Fork 0
/
aio_test.cc
60 lines (50 loc) · 1.82 KB
/
aio_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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;
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;
long page_size = sysconf(_SC_PAGESIZE);
errcode = posix_memalign((void**)&buf, page_size, page_size);
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(errno));
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(errno));
close(fd);
io_destroy(ctx);
return 0;
}