当前位置:网站首页>POSIX AIO -- Introduction to glibc version asynchronous IO
POSIX AIO -- Introduction to glibc version asynchronous IO
2022-06-27 13:32:00 【User 3147702】
1. summary
linux Most commonly used IO The model is synchronous IO, In this model , After the request is issued, the application will block until the condition is met ( Blocking IO), Or immediately return an error if the condition is not met ( Non blocking IO), The advantage of this is that the program is waiting IO When the request is completed, it will not occupy CPU. POSIX Asynchronous is defined IO Application program interface (AIO API),linux 2.6 The above versions of the kernel also implement kernel level asynchrony IO call . asynchronous IO The basic idea of is to allow processes to initiate many IO operation , Without blocking any of them , You don't have to wait for any operation to be completed , until IO Operation completion , The process can retrieve IO Results of operation .
linux There are mainly two sets of asynchronous IO, Namely glibc Implementation version , and linux Kernel Implementation 、libaio Packaged version .
2. IO Model introduction
write、read If not set O_NONBLOCK The identification is synchronous blocking IO, once IO launch , The process waits until the operation is completed . Set up O_NONBLOCK After the logo ,write、read Become non blocking IO, After the call, if the resource is available, perform the operation , And return immediately , If the resource is unavailable, an error is returned directly , In this case , Programs usually need to enter a busy wait state , Call again and again IO operation , Until normal return . Two of the above IO Although the model can well complete the single machine IO operation , But for concurrent requests, it cannot be realized .
For multiple concurrent requests , have access to IO Reuse model , Such as select、poll、epoll etc. , But the process must block until the operation is completed . If concurrency is required 、 Non blocking IO operation , such as CPU Intensive applications and slower IO Operation application scenarios , Use asynchronous IO It's a good choice .
2.1. Synchronous blocking type IO Model
2.2. Synchronous nonblocking IO Model
2.3. asynchronous IO Model
3. POSIX AIO — glibc Version asynchrony IO brief introduction
glibc Version asynchrony IO It mainly includes the following interfaces ( All defined on aio.h in , Must be called with POSIX Real time extension library librt):
glibc Version asynchrony IO Call interface
function | function | Prototype |
|---|---|---|
aio_read | Request asynchronous read operation | int aio_read(struct aiocb *aiocbp); |
aio_write | Request asynchronous write operations | int aio_write(struct aiocb *aiocbp); |
aio_error | Check the status of the asynchronous request | int aio_error(const struct aiocb *aiocbp); |
aio_return | Get the return status of the completed asynchronous request | ssize_t aio_return(struct aiocb *aiocbp); |
aio_suspend | Suspend the calling process , Until one or more asynchronous requests have been completed ( Or failure ) | int aio_suspend(const struct aiocb const list[], int nent, const struct timespec timeout); |
aio_cancel | Cancel asynchronous I/O request | int aio_cancel(int fildes, struct aiocb *aiocbp); |
lio_listio | Initiate multiple asynchronies at the same time IO transmission | int lio_listio( int mode, struct aiocb list[], int nent, struct sigevent sig ); |
3.1. aiocb structure
The above function uses a struct aiocb structure . It mainly contains the following fields :
struct aiocb {
int aio_fildes; // File descriptor to be read and written
volatile void *aio_buf; // Memory for read / write operations buffer
__off64_t aio_offset; // File offset of read / write operation
size_t aio_nbytes; // The length of bytes to be read and written
int aio_reqprio; // Request priority
struct sigevent aio_sigevent; // The signal or callback function after the completion of the asynchronous operation
/* Internal fields */
...
};One of the above structures aio_sigevent Domain , Used to define the notification signal or callback function when the asynchronous operation is completed .
struct sigevent
{
int sigev_notify; // Response type
int sigev_signo; // The signal
union sigval sigev_value; // Parameters of signal transmission
void (*sigev_notify_function)(union sigval); // Callback function
pthread_attr_t *sigev_notify_attributes; // Thread callback
}A consortium is used in the above structure sigval:
typedef union sigval
{
int sival_int;
void *sival_ptr;
} sigval_t;Usually called “ The signal 4 Byte value ”, The parameters of signal transmission are established .
4. Function description
4.1. aio_read、aio_write
int aio_read( struct aiocb *aiocbp );
int aio_write( struct aiocb *aiocbp );Call successfully returned 0, Failure to return -1 And set up errno.
Add request to request_queue. Through parameters aiocbp The structure pointed to can set the file descriptor 、 File offset 、 Attributes such as buffer and size , Function returns immediately after execution . about aio_write, If set O_APPEND, The file offset attribute is ignored .
4.2. aio_error
int aio_error( struct aiocb *aiocbp );Used to query the status of the request . The return value is as follows :
aio_error Function return value
Return value | significance |
|---|---|
EINPROGRESS | The request has not been completed |
ECANCELLED | The request has been cancelled by the application |
-1 | Call error , Error reason view errno |
4.3. aio_return
ssize_t aio_return( struct aiocb *aiocbp );Get asynchronous IO Return value . The successful call returns the number of characters read and written , Error return -1.
4.4. aio_suspend
int aio_suspend( const struct aiocb *const cblist[],
int n,
const struct timespec *timeout );Blocking process , Until an asynchronous request in the list is completed . cblist The completion of any asynchronous request in the , Functions will return 0, Error return -1.
4.5. aio_cancel
int aio_cancel( int fd, struct aiocb *aiocbp );Cancel an asynchronous request , The second parameter is NULL Then cancel all the fd Asynchronous requests on . Successful cancellation returns AIO_CANCELED, If the request has been completed, return AIO_NOTCANCELED. In the case of canceling multiple requests , If at least one request has not been cancelled , Then return to AIO_NOT_CANCELED, If no request can be cancelled , Then return to AIO_ALLDONE.
4.6. lio_listio
int lio_listio( int mode,
struct aiocb *list[],
int nent,
struct sigevent *sig );Initiate multiple asynchronous requests at the same time , It can greatly improve the performance of the system .
mode Parameters can be chosen LIO_WAIT or LIO_NOWAIT To declare whether the function is blocked . nent Parameters define list The maximum number of elements in the list . list The value in the list can be NULL Request , The request is ignored . sigevent The pointer to is defined in all IO The signal generated when all operations are completed or the callback function called .
边栏推荐
- Principle of printf indefinite length parameter
- #yyds干货盘点# 解决剑指offer:剪绳子(进阶版)
- 《预训练周刊》第51期:重构预训练、零样本自动微调、一键调用OPT
- [day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template
- 命令行编辑器 sed 基础用法总结
- ENSP cloud configuration
- Intranet learning notes (8)
- hue新建账号报错解决方案
- A statistical problem of shell script
- Cesium实现卫星在轨绕行
猜你喜欢

Quick news: Huawei launched the Hongmeng developer competition; Tencent conference released the "Wanshi Ruyi" plan

防火墙基础之华为华三防火墙web页面登录

新华三的千亿企业梦,还得靠吃ICT老本来实现?

ENSP cloud configuration

Realization of hospital medical record management system based on JSP

Make learning pointer easier (1)

How to set postman to Chinese? (Chinese)

Cesium实现卫星在轨绕行

Teach you how to build a permanent personal server!

How to download pictures with hyperlinks
随机推荐
Istio微服务治理网格流量管理核心资源控制器详解
How to open an account for CSI 500 stock index futures, what are the regular domestic stock index futures platforms, and where is the safest place to open an account?
PLM还能怎么用?
抖音实战~公开/私密短视频互转
每日刷题记录 (六)
What kind of air conditioner is this?
Principle of printf indefinite length parameter
打印输出数(递归方法解决)
MySQL index and its classification
面试官:Redis的共享对象池了解吗?
Infiltration learning diary day20
awk 简明教程
Clear self orientation
使用bitnamiredis-sentinel部署Redis 哨兵模式
《预训练周刊》第51期:重构预训练、零样本自动微调、一键调用OPT
【问题解决】Tensorflow中run究竟运行了哪些节点?
Can flush open an account for stock trading? Is it safe?
Intranet learning notes (8)
Configuration management center of microservices
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)