异步I/O
异步I/O是POSIX定义的一组标准接口,Linux支持。异步I/O在提交完I/O操作请求后就立即返回,程序不需要等到I/O操作完成后在去做别的事情,具有非阻塞的特性。当底层把I/O操作完成后,可以给提交者发送信号,或调用注册的回调函数,告知请求提交者I/O操作完成。在信号处理函数或回调函数中,可以使用异步I/O接口来获得I/O的完成情况(获取读写操作返回的字节数或错误码、读取的数据等)。
一个简单的针对虚拟串口的异步I/O应用程序部分代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <linux/input.h>
#include <aio.h>
#include "vser.h"
void aiow_completion_handler(sigval_t sigval)
{
int ret;
struct aiocb *req;
req = (struct aiocb *)sigval.sival_ptr;
if (aio_error(req) == 0) {
ret = aio_return(req);
printf("aio write %d bytes\n", ret);
}
return;
}
void aior_completion_handler(sigval_t sigval)
{
int ret;
struct aiocb *req;
req = (struct aiocb *)sigval.sival_ptr;
if (aio_error(req) == 0) {
ret = aio_return(req);
if (ret)
printf("aio read: %s\n", (char *)req->aio_buf);
}
return;
}
int main(int argc, char *argv[])
{
int ret;
int fd;
struct aiocb aiow, aior;
fd = open("/dev/vser0", O_RDWR);
if (fd == -1)
goto fail;
memset(&aiow, 0, sizeof(aiow));
memset(&aior, 0, sizeof(aior));
aiow.aio_fildes = fd;
aiow.aio_buf = malloc(32);
strcpy((char *)aiow.aio_buf, "aio test");
aiow.aio_nbytes = strlen((char *)aiow.aio_buf) + 1;
aiow.aio_offset = 0;
aiow.aio_sigevent.sigev_notify = SIGEV_THREAD;
aiow.aio_sigevent.sigev_notify_function = aiow_completion_handler;
aiow.aio_sigevent.sigev_notify_attributes = NULL;
aiow.aio_sigevent.sigev_value.sival_ptr = &aiow;
aior.aio_fildes = fd;
aior.aio_buf = malloc(32);
aior.aio_nbytes = 32;
aior.aio_offset = 0;
aior.aio_sigevent.sigev_notify = SIGEV_THREAD;
aior.aio_sigevent.sigev_notify_function = aior_completion_handler;
aior.aio_sigevent.sigev_notify_attributes = NULL;
aior.aio_sigevent.sigev_value.sival_ptr = &aior;
while (1) {
if (aio_write(&aiow) == -1)
goto fail;
if (aio_read(&aior) == -1)
goto fail;
sleep(1);
}
fail:
perror("aio test");
exit(EXIT_FAILURE);
}
定义了两个分别用于读和写的异步I/O控制块,初始化这两个控制块,主要是文件描述符,用于读写的缓冲区,读写的字节数和异步I/O完成后的回调函数。
发起一个异步写操作,该函数会立即返回