面试题答案
一键面试基本步骤和主要函数
- 初始化异步I/O请求结构体
struct aiocb
:- 这个结构体定义在
<aio.h>
头文件中,用于描述异步I/O操作。 - 主要成员包括:
aio_fildes
:文件描述符,指定要进行I/O操作的文件。aio_buf
:指向数据缓冲区的指针,用于存放读取或写入的数据。aio_nbytes
:要传输的字节数。aio_offset
:文件偏移量,指定I/O操作在文件中的起始位置。aio_sigevent
:指定操作完成时要产生的信号或通知。
- 这个结构体定义在
- 使用
aio_read
或aio_write
发起异步I/O操作:aio_read
函数:- 原型:
int aio_read(struct aiocb *aiocbp);
- 功能:发起一个异步读操作。
aiocbp
是指向已初始化的struct aiocb
结构体的指针。成功时返回0,失败返回 -1,并设置errno
来指示错误原因。
- 原型:
aio_write
函数:- 原型:
int aio_write(struct aiocb *aiocbp);
- 功能:发起一个异步写操作。同样,
aiocbp
是指向已初始化的struct aiocb
结构体的指针。成功返回0,失败返回 -1 并设置errno
。
- 原型:
- 检查异步I/O操作的状态:
aio_error
函数:- 原型:
int aio_error(const struct aiocb *aiocbp);
- 功能:检查指定异步I/O操作的状态。返回值:如果操作尚未完成,返回
EINPROGRESS
;如果操作成功完成,返回0;如果操作失败,返回对应的负的错误码。
- 原型:
aio_return
函数:- 原型:
ssize_t aio_return(struct aiocb *aiocbp);
- 功能:获取异步I/O操作的返回值(如读取或写入的字节数)。仅当
aio_error
返回0(操作成功完成)时调用此函数才有意义。
- 原型:
简单的异步文件读取场景示例
#include <stdio.h>
#include <stdlib.h>
#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 1024
int main() {
int fd;
struct aiocb aiocb;
char buffer[BUFFER_SIZE];
// 打开文件
fd = open("test.txt", O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
// 初始化aiocb结构体
memset(&aiocb, 0, sizeof(aiocb));
aiocb.aio_fildes = fd;
aiocb.aio_buf = buffer;
aiocb.aio_nbytes = BUFFER_SIZE;
aiocb.aio_offset = 0;
// 发起异步读操作
if (aio_read(&aiocb) == -1) {
perror("aio_read");
close(fd);
exit(EXIT_FAILURE);
}
// 检查操作状态
int status;
while ((status = aio_error(&aiocb)) == EINPROGRESS) {
// 操作尚未完成,可执行其他任务
usleep(1000);
}
if (status != 0) {
perror("aio_error");
} else {
ssize_t bytes_read = aio_return(&aiocb);
printf("Read %zd bytes: %.*s\n", bytes_read, (int)bytes_read, buffer);
}
// 关闭文件
close(fd);
return 0;
}
在这个示例中:
- 首先打开一个文件
test.txt
。 - 初始化
struct aiocb
结构体,设置文件描述符、缓冲区、要读取的字节数和文件偏移量。 - 使用
aio_read
发起异步读操作。 - 通过
aio_error
循环检查操作状态,当操作完成后,使用aio_return
获取读取的字节数并输出读取的内容。 - 最后关闭文件。