libaio介绍和使用
libaio介绍
The Linux-native asynchronous I/O facility ("async I/O", or "aio") has a richer API and capability set than the simple POSIX async I/O facility. This library, libaio, provides the Linux-native API for async I/O. The POSIX async I/O facility requires this library in order to provide kernel-accelerated async I/O capabilities, as do applications which require the Linux-native async I/O API.
libaio项目 Overview - libaio - Pagure.iohttps://pagure.io/libaio
centos下安装libaio库
# sudo yum install libaio-devel
libaio提供四个函数如下表:
函数 | 功能 | 原型 |
io_setup | 创建一个异步IO上下文(io_context_t是一个句柄) | int io_setup(int maxevents, io_context_t *ctxp); |
io_destroy | 销毁一个异步IO上下文(如果有正在进行的异步IO,取消并等待它们完成) | int io_destroy(io_context_t ctx); |
io_submit | 提交异步IO请求 | long io_submit(aio_context_t ctx_id, long nr, struct iocb **iocbpp); |
io_cancel | 取消一个异步IO请求 | long io_cancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result); |
io_getevents | 等待并获取异步IO请求的事件(也就是异步请求的处理结果) | long io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout); |
五个宏定义:
void io_set_callback(struct iocb *iocb, io_callback_t cb);
void io_prep_pwrite(struct iocb *iocb, int fd, void *buf, size_t count, long long offset);
void io_prep_pread(struct iocb *iocb, int fd, void *buf, size_t count, long long offset);
void io_prep_pwritev(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset);
void io_prep_preadv(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset);
这五个宏定义都是操作struct iocb的结构体。struct iocb是libaio中很重要的一个结构体,用于表示IO,但是其结构略显复杂,为了保持封装性不建议直接操作其元素而用上面五个宏定义操作。
测试
测试源代码 test.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<libaio.h>
#include<errno.h>
#include<unistd.h>
int main(void)
{int output_fd;char *content="hello world!";char *outputfile="hello.txt";io_context_t ctx;struct iocb io,*p=&io;struct io_event e;struct timespec timeout;memset(&ctx,0,sizeof(ctx));if(io_setup(10,&ctx)!=0){//initprintf("io_setup error\n");return -1;}if((output_fd=open(outputfile,O_CREAT|O_WRONLY,0644))<0){perror("open error");io_destroy(ctx);return -1;}io_prep_pwrite(&io,output_fd,content,strlen(content),0);io.data=content;if(io_submit(ctx,1,&p)!=1){io_destroy(ctx);printf("io_submit error\n");return -1;}while(1){timeout.tv_sec=0;timeout.tv_nsec=500000000;//0.5sif(io_getevents(ctx,0,1,&e,&timeout)==1){close(output_fd);break;}printf("haven't done\n");sleep(1);}io_destroy(ctx);return 0;
}
Makefile:
test:gcc test.c -o test -laio
clean:rm -rf test
其他参考文档:
Linux Asynchronous I/O (oxnz.github.io)https://oxnz.github.io/2016/10/13/linux-aio/
libaio介绍和使用相关推荐
- 12.1 LNMP架构介绍;12.2 MySQL安装;12.3-2.4 PHP安装(上下);12.5
扩展: Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html apache和ng ...
- LAMP架构介绍,MySQL、MariaDB介绍,MySQL安装
2019独角兽企业重金招聘Python工程师标准>>> LAMP架构介绍: MySQL.MariaDB介绍: Community 社区版本 =开源版本 Enterprise 企业 ...
- 4.21 LNMP环境介绍 4.22/23/24 Mariadb安装 4.25 服务管理
2019独角兽企业重金招聘Python工程师标准>>> LNMP环境介绍 Mariadb安装 服务管理 LNMP环境介绍 Linux+Nginx+Mysql/mariadb+php ...
- 分布式块存储QoS限速算法介绍与实践以及对上层应用的影响
分布式块存储QoS限速算法以及对上层应用的影响 QoS限速算法介绍 令牌桶 Token Bucket 漏桶 Leaky Bucket Leaky bucket as a meter Leaky buc ...
- LAMP介绍-MySQL安装
2019独角兽企业重金招聘Python工程师标准>>> LAMP: linux-apache-mysql-php (安装方式有:rpm,源码,二进制免编译) linux-操作系统 ...
- MySQL介绍及安装(一)
一.关系型数据库和非关系型数据库 1.1:关系型数据库 关系型数据库是把复杂的数据结构归结为简单的二元关系(即二维表格的形式),在关系型数据库中,对数据的操作几乎全部建立在一个或多个关系表格上的,通过 ...
- Linux AIO的新归宿:io_uring(介绍,系统调用)
目录 背景 io_uring io_uring 准备阶段 IO 提交 IO 收割 其它高级特性 IORING_REGISTER_FILES IORING_SETUP_IOPOLL IORING_REG ...
- libaio源码安装_MySQL5.7.17 编译安装及二进制安装详解
MySQL 的安装方式有很多,最常见的就是编译安装和二进制安装: 在这里我将两种安装方式都介绍一下,自由选择: 首先我们来看一下编译安装的步骤: 首先,到官方网站中下载源码包: Download My ...
- LNMP架构介绍与搭建
笔记内容: 12.1 LNMP架构介绍 12.2 MySQL安装 12.3/12.4 PHP安装 12.5 Nginx介绍 笔记日期:2017.10.18 <br> 12.1 LNMP架构 ...
- Percona XtraBackup 安装介绍篇
XtraBackup介绍 XtraBackup是Percona公司的开源项目,用以实现类似Innodb官方的热备份工具InnoDB Hot Backup的功能,它支持在线热备份(备份时不影响数据读写) ...
最新文章
- php get_extension_funcs,get_extension_funcs()
- Java命令行界面(第2部分):args4j
- 冠军方案 | 第二届中国“高分杯”美丽乡村大赛第一名总结
- Intel® Nehalem/Westmere架构/微架构/流水线 (1) - 特性概述
- 职教云python题和答案_智慧职教云课堂Python程序设计基础题目及答案
- 跟着李开复去硅谷,你有疑惑我帮问 | 互动一则
- CS 61A FALL 2020 Project-cats
- Vue安装及环境配置、开发工具
- Origin C编程 -- 自定义函数按位解读数据列
- Linux安装flux护眼软件
- 5.7 UITableView单元格间隔背景色 [原创iOS开发-Xcode教程]
- 微信中括号常用表情对应emoji相关json数据整理
- 文件属性安全组误删恢复
- 用html5做一个介绍自己家乡的页面_(近万字)一篇文章带你了解HTML5和CSS3开发基础与应用-适合前端面试必备...
- 用计算机说唱的 那个男的是谁,中国十大嘻哈歌手排名:有名的rapper都有谁
- 一键开启/禁用以太网
- 没有团员证,怎么查询入团时间?
- Excel实用操作技巧
- 百度站长资源平台自动批量添加站点域名工具(站长站群工具)
- 小程序引导关注公众号