简述

linux下异步方式有两种:异步通知和异步IO(AIO),异步通知请参考:linux异步通知

Linux的I/O机制经历了一下几个阶段的演进:

1. 同步阻塞I/O: 用户进程进行I/O操作,一直阻塞到I/O操作完成为止。

2. 同步非阻塞I/O: 用户程序可以通过设置文件描述符的属性O_NONBLOCK,I/O操作可以立即返回,但是并不保证I/O操作成功。

3. 异步事件阻塞I/O: 用户进程可以对I/O事件进行阻塞,但是I/O操作并不阻塞。通过select/poll/epoll等函数调用来达到此目的。

4. 异步时间非阻塞I/O: 也叫做异步I/O(AIO),用户程序可以通过向内核发出I/O请求命令,不用等待I/O事件真正发生,可以继续做另外的事情,等I/O操作完成,内核会通过函数回调或者信号机制通知用户进程。这样很大程度提高了系统吞吐量。

The POSIX asynchronous I/O (AIO) interface allows applications to initiate one or more I/O operations that are performed asynchronously (i.e., in the background).

The application can elect to be notified of completion of the I/O operation in a variety of ways: by delivery of a signal, by instantiation of a thread, or no notification at all.

函数

The POSIX AIO interface consists of the following functions:

aio_read(3) Enqueue a read request. This is the asynchronous analog of read(2).

aio_write(3) Enqueue a write request. This is the asynchronous analog of write(2).

aio_fsync(3) Enqueue a sync request for the I/O operations on a file descriptor. This is the asynchronous analog of fsync(2) and fdatasync(2).

aio_error(3) Obtain the error status of an enqueued I/O request.

aio_return(3) Obtain the return status of a completed I/O request.

aio_suspend(3) Suspend the caller until one or more of a specified set of I/O requests completes.

aio_cancel(3) Attempt to cancel outstanding I/O requests on a specified file descriptor.

lio_listio(3) Enqueue multiple I/O requests using a single function call.

在异步非阻塞IO中,可以同时发起多个传输操作。这需要每个传输操作有唯一的上下文,这样才能在它们完成时区分到底是哪个传输操作完成了。在AIO中通过aiocb(AIO I/O control Block)结构体区分。

aiocb包含了有关传输的所有信息,以及为数据准备的用户缓冲区。在产生I/O通知(完成)时,aiocb结构就被用来唯一标识所完成的I/O操作。

           #include <aiocb.h>

           struct aiocb {
/* The order of these fields is implementation-dependent */ int aio_fildes; /* File descriptor */
off_t aio_offset; /* File offset */
volatile void *aio_buf; /* Location of buffer */
size_t aio_nbytes; /* Length of transfer */
int aio_reqprio; /* Request priority */
struct sigevent aio_sigevent; /* Notification method */
int aio_lio_opcode; /* Operation to be performed;
lio_listio() only */ /* Various implementation-internal fields not shown */
}; /* Operation codes for 'aio_lio_opcode': */ enum { LIO_READ, LIO_WRITE, LIO_NOP };

The fields of this structure are as follows:

aio_filedes The file descriptor on which the I/O operation is to be performed.

aio_offset This is the file offset at which the I/O operation is to be performed.

aio_buf This is the buffer used to transfer data for a read or write operation.

aio_nbytes This is the size of the buffer pointed to by aio_buf.

aio_reqprio This field specifies a value that is subtracted from the calling thread's real-time priority in order to determine the priority for execution of this I/O request(see pthread_setschedparam(3)). The specified value must be between 0 and the value returned by sysconf(_SC_AIO_PRIO_DELTA_MAX). This field is ignored for file synchronization operations.

aio_sigevent This field is a structure that specifies how the caller is to be notified when the asynchronous I/O operation completes. Possible values for aio_sigevent.sigev_notify are SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_THREAD. See sigevent(7) for further details.

aio_lio_opcode The type of operation to be performed; used only for lio_listio(3).

应用

应用主要有两种方式:信号机制和回调函数。

参考:

1. Linux 异步IO机制

2. 使用异步 I/O 大大提高应用程序的性能

linux异步IO--aio的更多相关文章

  1. linux异步IO的两种方式【转】

    转自:https://blog.csdn.net/shixin_0125/article/details/78898146 知道异步IO已经很久了,但是直到最近,才真正用它来解决一下实际问题(在一个C ...

  2. linux 异步IO通信

    一. 回顾 做java开发的,一定对BIO,NIO,AIO通信很了解了,现在再在下面罗列一下: 同步阻塞IO(JAVA BIO):  同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时 ...

  3. Linux异步IO【转】

    转自:http://blog.chinaunix.net/uid-24567872-id-87676.html Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出 ...

  4. Linux异步IO操作

    Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出之后,应用程序就会阻塞,直到请求满足为止.这是很好的一种解决方案,因为调用应用程序在等待 I/O 请求完成时不需 ...

  5. JDK7新特性<八>异步io/AIO

    概述 JDK7引入了Asynchronous I/O.I/O编程中,常用到两种模式:Reactor 和 Proactor.Reactor就是Java的NIO.当有事件触发时,我们得到通知,进行相应的处 ...

  6. Linux 网络编程的5种IO模型:异步IO模型

    Linux 网络编程的5种IO模型:异步IO模型 资料已经整理好,但是还有未竟之业:复习多路复用epoll 阅读例程, 异步IO 函数实现 背景 上一讲< Linux 网络编程的5种IO模型:信 ...

  7. ORACLE数据库异步IO介绍

    异步IO概念 Linux 异步 I/O (AIO)是 Linux 内核中提供的一个增强的功能.它是Linux 2.6 版本内核的一个标准特性,当然我们在2.4 版本内核的补丁中也可以找到它.AIO 背 ...

  8. Socket-IO 系列(一)Linux 网络 IO 模型

    Socket-IO 系列(一)Linux 网络 IO 模型 一.基本概念 在正式开始讲 Linux IO 模型前,先介绍 5 个基本概念. 1.1 用户空间与内核空间 现在操作系统都是采用虚拟存储器, ...

  9. linux异步通知

    简述 linux下异步方式有两种:异步通知和异步IO(AIO),aio请参考:linux异步IO--aio 异步通知的含义是:一旦设备就绪,则主动通知应用程序,这样应用程序就不需要查询设备状态,准确称 ...

随机推荐

  1. python ipython spyder

    ipython usage: ipython qtconsole --pylab inline anacond usage: 1. spyder 1. source ~/anacond/bin/act ...

  2. PHP位运算用途

    在实际应用中可以做用户权限的应用 我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符.| 位或运行符.参与运算的如果是10进制数,则会被转换至2进制数参 ...

  3. VS Code 中文注释显示乱码 解决方法

    将设置中的"files.autoGuessEncoding"项的值改为true即可. 1.文件 2.首选项 3.设置 4.搜索 "files.autoGuessEncod ...

  4. libsvm参数说明[zz]

    English:libsvm_options:-s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-cla ...

  5. 使用MySQL Proxy和MySQL Replication实现读写分离

    MySQL Replication可以将master的数据复制分布到多个slave上,然后可以利用slave来分担master的读压力.那么对于前台应用来说,就要考虑如何将读的压力分布到多个slave ...

  6. js获取IP地址方法总结

    js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...

  7. Shiro系列(2) - 权限模型以及权限分配的两种方式

    1. 顶级账户分配权限用户需要被分配相应的权限才可访问相应的资源.权限是对于资源的操作一张许可证.给用户分配资源权限需要将权限的相关信息保存到数据库.这些相关内容包含:用户信息.权限管理.用户分配的权 ...

  8. SonarQube学习入门指南

    1. 什么是SonarQube? SonarQube 官网:https://www.sonarqube.org/ SonarQube®是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码异味.它可 ...

  9. [svc]ext4文件删除&访问原理

    文件名信息存放在哪里? LINUX的文件名是存在父目录的block里面,并指向这个文件的inode节点,这个文件的inode节点再标记指向存放这个文件的block的数据块.我们删除一个文件,实际上并不 ...

  10. Redis为什么使用单进程单线程方式

    Redis采用的是基于内存的采用的是单进程单线程模型的KV数据库,由C语言编写.官方提供的数据是可以达到100000+的qps.这个数据不比采用单进程多线程的同样基于内存的KV数据库Memcached ...