博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux异步IO--aio
阅读量:6863 次
发布时间:2019-06-26

本文共 3621 字,大约阅读时间需要 12 分钟。

简述

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

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 
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. 

2. 

转载地址:http://raqyl.baihongyu.com/

你可能感兴趣的文章
烂泥:NFS存储与VSphere配合使用
查看>>
烂泥:mysql数据库使用的基本命令
查看>>
js清除缓存方法
查看>>
ALGEBRA-3 线性映射
查看>>
C# 利用ReportViewer生成报表
查看>>
下拉菜单
查看>>
knockout.js 练习一
查看>>
Asp.Net Core SignalR 与微信小程序交互笔记
查看>>
os.linesep提取当前平台使用的换行符
查看>>
到底什么是故事点(Story Point)?
查看>>
修改用户定义的数据类型
查看>>
网络流24题10
查看>>
多域名THINKPHP利用MEMCACHE方式共享SESSION数据(转)
查看>>
C#基础 for 穷举、迭代
查看>>
2018.3.17 模拟赛——(2)删数
查看>>
视图层
查看>>
Django跨域解决方法
查看>>
冒泡排序
查看>>
Mysql字段合并
查看>>
五笔反查工具
查看>>