本文来自个人博客:https://dunkwan.cn
文章目录
异步I/O
POSIX异步I/O
异步I/O接口使用AIO控制块来描述I/O操作。aiocb
结构定义了AIO控制块。该结构至少包括如下这些字段(视具体实现还可能有额外的字段):
struct aiocb{
int aio_fildes; /* file descriptor */
off_t aio_offset; /* file offset for I/O */
volatile void *aio_buf; /* buffer for I/O */
size_t aio_nbytes; /* number of bytes to transfer */
int aio_reqprio; /* priority */
struct sigevent aio_sigevent; /* signal information */
int aio_lio_opcode; /* operation for list I/O */
};
aio_fildes
:用于表示被打开用来读或写的文件描述符。
aio_offset
:用于指定读或写操作的开始位置。
aio_buf
:用于指定读和写操作将数据复制入其中和将数据从其读出的缓冲区。
aio_nbytes
:包含了要读或写的字节数。
aio_reqprio
:用于为异步I/O请求提示顺序。
aio_lio_opcode
:用于基于列表的异步I/O。
aio_sigevent
:控制在I/O事件完成后,如何通知应用程序。
sigevent
结构如下:
struct sigevent{
int sigev_notify; /* notify type */
int sigev_signo; /* signal number */
union sigval sigev_value; /* notify argument */
void (*sigev_notify_function)(union sigval); /* notify function */
pthread_attr_t *sigev_notify_attributes; /* notify attrs */
};
sigev_notify
字段控制通知的类型。取值情况如下:
SIGEV_NONE
:异步I/O请求完成后,不通知进程。SIGEV_SIGNAL
:异步I/O请求完成后,产生由sigev_signo
字段指定的信号。如果应用程序已选择捕捉信号,且在建立信号处理程序的时候指定了SIG_SIGINFO
标志,那么该信号将被入队(如果支持排队信号)。信号处理程序会传送给一个siginfo
结构,该结构的si_value
字段设置为sigev_value
(如果使用了SA_SIGINFO标志
)。SIGEV_THREAD
:当异步I/O请求完成时,由sigev_notify_function
字段指定的函数被调用。sigev_value
字段被传入作为它的唯一参数。除非sigev_notify_attributes
字段被设定为pthread
属性结构的地址,且该结构指定了一个另外的线程属性,否则该函数将在分离状态下的一个单独的线程中执行。
在进行异步I/O之前需要先初始化AIO控制块,调用aio_read
和aio_write
函数来进行异步的读写操作。
#include <aio.h>
int aio_read(struct aiocb *aiocb);
int aio_write(struct aiocb *aiocb);
两个函数的返回值:若成功,返回0;若出错,返回-1。
在上述两个函数中有调用成功者,就会将异步I/O请求加入操作系统的等待队列。若想强制所有等待中的异步操作不等待而写入持久化的存储中,可以设立一个AIO控制块并调用aio_fsync
函数。
#include <aio.h>
int aio_fsync(int op, struct aiocb *aiocb);
返回值:若成功,返回0;若出错,返回-1。
为了获知一个异步读、写或者同步操作的完成状态,需要调用aio_error
函数。
#include <aio.h>
int aio_error(const struct aiocb *aiocb);
该函数返回值如下:
0
:异步操作成功完成。
-1
:对aio_error
的调用失败。
EINPROGRESS
:异步读、写或同步操作仍在等待。
其他情形
:其他任何返回值是相关的异步操作失败返回的错误码。
如果异步操作成功,可以调用aio_return
函数来获取异步操作的返回值。
#include <aio.h>
ssize_t aio_return(const struct aiocb *aiocb);
该函数返回值如下:
-1
:该函数调用失败,并设置errno
。
其他情形
:返回异步操作的结果。
执行I/O操作时,如果还有其他事务要处理而不想被I/O操作阻塞,就可以使用异步I/O。然而,如果在所有事务都完成时,还有异步操作未完成,可以调用aio_suspend
函数来阻塞进程,直到操作完成。
#include <aio.h>
int aio_suspend(const struct aiocb *const list[], int nent, const struct timespec *timeout);
返回值:若成功,返回0;若出错,返回-1。
当还有我们不想再完成的等待中的异步操作时,可以尝试使用aio_cancel
函数来取消它们。
#include <aio.h>
int aio_cancel(int fd, struct aiocb *aiocb);
该函数返回值情形如下:
AIO_ALLDONE
:所有操作再尝试取消它们之前已经完成。
AIO_CANCELED
:所有要求的操作已被取消。
AIO_NOTICANCELED
:至少有一个要求的操作没有被取消。
-1
:对aio_cancel
的调用失败,错误码将被存储在errno
中。
lio_listio
函数既能以同步方式进行使用,又能以异步方式使用。它负责提交一系列有AIO控制块列表描述的I/O请求。
#include <aio.h>
int lio_listio(int mode, struct aiocb *restrict const list[restrict], int nent, struct sigevent *restrict sigev);
返回值:若成功,返回0;若出错,返回-1。
mode
参数决定了是否是异步的。取值可为LIO_WAIT
(同步)和LIO_NOWAIT
(异步)。
测试示例:
异步I/O读写文件。
#include "apue.h"
#include <ctype.h>
#include <fcntl.h>
#include <aio.h>
#include <errno.h>
#define BSZ 4096
#define NBUF 8
enum rwop {
UNUSED = 0,
READ_PENDING = 1,
WRITE_PENDING = 2
};
struct buf {
enum rwop op;
int last;
struct aiocb aiocb;
unsigned char data[BSZ];
};
struct buf bufs[NBUF];
unsigned char
translate(unsigned char c)
{
/* same as before */
if (isalpha(c)) {
if (c >= 'n')
c -= 13;
else if (c >= 'a')
c += 13;
else if (c >= 'N')
c -= 13;
else
c += 13;
}
return(c);
}
int
main(int argc, char* argv[])
{
int ifd, ofd, i, j, n, err, numop;
struct stat sbuf;
const struct aiocb *aiolist[NBUF];
off_t off = 0;
if (argc != 3)
err_quit("usage: rot13 infile outfile");
if ((ifd = open(argv[1], O_RDONLY)) < 0)
err_sys("can't open %s", argv[1]);
if ((ofd = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, FILE_MODE)) < 0)
err_sys("can't create %s", argv[2]);
if (fstat(ifd, &sbuf) < 0)
err_sys("fstat failed");
/* initialize the buffers */
for (i = 0; i < NBUF; i++) {
bufs[i].op = UNUSED;
bufs[i].aiocb.aio_buf = bufs[i].data;
bufs[i].aiocb.aio_sigevent.sigev_notify = SIGEV_NONE;
aiolist[i] = NULL;
}
numop = 0;
for (;;) {
for (i = 0; i < NBUF; i++) {
switch (bufs[i].op) {
case UNUSED:
/*
* Read from the input file if more data
* remains unread.
*/
if (off < sbuf.st_size) {
bufs[i].op = READ_PENDING;
bufs[i].aiocb.aio_fildes = ifd;
bufs[i].aiocb.aio_offset = off;
off += BSZ;
if (off >= sbuf.st_size)
bufs[i].last = 1;
bufs[i].aiocb.aio_nbytes = BSZ;
if (aio_read(&bufs[i].aiocb) < 0)
err_sys("aio_read failed");
aiolist[i] = &bufs[i].aiocb;
numop++;
}
break;
case READ_PENDING:
if ((err = aio_error(&bufs[i].aiocb)) == EINPROGRESS)
continue;
if (err != 0) {
if (err == -1)
err_sys("aio_error failed");
else
err_exit(err, "read failed");
}
/*
* A read is complete; translate the buffer
* and write it.
*/
if ((n = aio_return(&bufs[i].aiocb)) < 0)
err_sys("aio_return failed");
if (n != BSZ && !bufs[i].last)
err_quit("short read (%d/%d)", n, BSZ);
for (j = 0; j < n; j++)
bufs[i].data[j] = translate(bufs[i].data[j]);
bufs[i].op = WRITE_PENDING;
bufs[i].aiocb.aio_fildes = ofd;
bufs[i].aiocb.aio_nbytes = n;
if (aio_write(&bufs[i].aiocb) < 0)
err_sys("aio_write failed");
/* retain our spot in aiolist */
break;
case WRITE_PENDING:
if ((err = aio_error(&bufs[i].aiocb)) == EINPROGRESS)
continue;
if (err != 0) {
if (err == -1)
err_sys("aio_error failed");
else
err_exit(err, "write failed");
}
/*
* A write is complete; mark the buffer as unused.
*/
if ((n = aio_return(&bufs[i].aiocb)) < 0)
err_sys("aio_return failed");
if (n != bufs[i].aiocb.aio_nbytes)
err_quit("short write (%d/%d)", n, BSZ);
aiolist[i] = NULL;
bufs[i].op = UNUSED;
numop--;
break;
}
}
if (numop == 0) {
if (off >= sbuf.st_size)
break;
} else {
if (aio_suspend(aiolist, NBUF, NULL) < 0)
err_sys("aio_suspend failed");
}
}
bufs[0].aiocb.aio_fildes = ofd;
if (aio_fsync(O_SYNC, &bufs[0].aiocb) < 0)
err_sys("aio_fsync failed");
exit(0);
}
结果如下:
源代码