关于异步IO使用的一个例子
2011-01-14 17:20
956 查看
#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));
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));
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 = sysconf(_SC_PAGESIZE);
iocbpp[0].u.c.offset = 0;
int n = io_submit(ctx, 1, &iocbpp);
printf("==io_submit==: %d:\n", n);
struct io_event events[10];
struct timespec timeout = {1, 100};
//n = io_getevents(ctx, 1, 10, events, &timeout);
//printf("io_getevents: %d:\n", n);
close(fd);
io_destroy(ctx);
return 0;
}
编译方法:gcc -0 myaio myaio.c -laio -D_GNU_SOURCE
#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));
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));
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 = sysconf(_SC_PAGESIZE);
iocbpp[0].u.c.offset = 0;
int n = io_submit(ctx, 1, &iocbpp);
printf("==io_submit==: %d:\n", n);
struct io_event events[10];
struct timespec timeout = {1, 100};
//n = io_getevents(ctx, 1, 10, events, &timeout);
//printf("io_getevents: %d:\n", n);
close(fd);
io_destroy(ctx);
return 0;
}
编译方法:gcc -0 myaio myaio.c -laio -D_GNU_SOURCE
相关文章推荐
- 一个关于jqGrid使用的小例子(行按钮)
- 关于checkboxtree选中的一个小例子非常使用
- C 关于位段使用的一个简单例子
- 关于多线程程序中使用volatile关键字的一个小例子
- 一个关于server使用 aidl的例子
- 这是一个demo 关于pagecontroller 和ScrollView 混合使用的例子
- 一个关于类别(category)使用的例子
- 关于ColorBox使用的一个简单例子
- 关于二级指针的使用测试小例子,仅供测试--参数传递的时候用指针最好,因为压栈的时候指针,只是压入一个地址的值,最多4个字节(32位机上),提供的3个测试例子,基于VC6.0的环境。
- 一个关于jqGrid使用的小例子(行按钮)
- 一个关于jqGrid使用的小例子(行按钮)
- 记一个关于C语言函数指针使用的小例子
- 一个关于looper和handler的好玩例子(子线程中使用handler)
- 关于MSSQL存储过程中使用游标的一个小例子(学习)
- 一个关于Comparator的使用例子
- 一个不错的关于sql数组的使用例子
- 关于Java中的继承和组合的一个错误使用的例子
- 关于Java中的继承和组合的一个错误使用的例子
- 自定义View的一个例子(这里使用了使用了自定义Button)
- C++使用临界区(互斥体)来控制多线程访问同一个变量(经典例子)