为什么 sendfile 不检查从输入描述符读取是否会阻塞?

Why does sendfile not check if reading from the input descriptor would block?

作为服务器,我想从一个非阻塞的客户端套接字中读取,然后写入一个文件。

根据 sendfile 的手册页,如果 errno 设置为 EAGAIN,那么这仅表明,如果 output 文件描述符设置为非阻塞,则调用 sendfile 会阻塞。

也就是说,sendfile 进行写入的底层调用会阻塞。

是否可以使用 sendfile 以便在读取阻塞时 errno 为 EAGAIN?

当然可以。

使用带有读取描述符的 select() 库函数,您可以通过 errno 检查 EWOULDBLOCK。如果已设置,则读取将阻塞。

您无法通过检查 sendfile() 调用中的内容来查看读取是否会阻塞。