使用sendfile(),是否可以判断in_fd何时处于EOF?

发布于 2024-09-18 15:17:33 字数 729 浏览 15 评论 0原文

通读 Linux 系统调用的 手册页sendfile,我想知道调用程序是否可以知道in_fd何时位于EOF。据推测,这可以通过返回值 0 来表示,但这会导致返回值 0 实际意味着什么的问题。如果 sendfile 就像 write,那么返回值 0 就意味着复制了 0 个字节。但是,如果 sendfile 就像 read< /code>,则返回值 0 表示 EOF。为了使用 sendfile,必须事先知道要从 in_fd 复制到 out_fd 多少字节吗? sendfile 返回 0 意味着什么?

Reading through the man page of the Linux system call sendfile, I am wondering whether it is possible for the calling program to know when in_fd is at EOF. Presumably, this could be signaled by a return value of 0, but this leads to the question of what a return value of 0 actually means. If sendfile is like write, then a return value of 0 would just mean that 0 bytes were copied. But, if sendfile is like read, then a return value of 0 would mean EOF. Must one know in advance how many bytes that are to be copied from in_fd to out_fd in order to use sendfile? What does it mean when sendfile returns 0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-09-25 15:17:33

我认为没有任何直接的方法可以知道这一点,但这并不重要。通常,您可以通过 stat/fstat 找到输入文件大小,并使用它来计算传输量。插座端对你来说并不重要。

唯一有问题的情况是,如果您想要传输正在增长或缩小的文件。考虑到输入文件必须进行 mmap,并且在这些情况下 mmap 可能会发生不好的事情(没有一些聪明的代码),您可能不应该在这些情况下使用 sendfile。

I don't think there is any direct way to know that but it shouldn't really matter. Normally you would find the input file size via stat/fstat and use that to count out your transfer. The socket end isn't going to matter to you.

The only situation this should be problematic is if you want to transfer a file that is growing or shrinking. Given that the input file has to be mmap-ed, and the bad things that can happen (without some clever code) with mmap in those situations you should probably just not employ sendfile for those cases.

戈亓 2024-09-25 15:17:33

您可以使用偏移参数来读取计数。

根据手册页

,如果offset不为NULL,则它指向一个保存文件偏移量的变量,sendfile()将从该变量开始从in_fd读取数据。当 sendfile() 返回时,该变量将被设置为读取的最后一个字节之后的字节的偏移量。如果offset不为NULL,则sendfile()不会修改in_fd的当前文件偏移量;否则,调整当前文件偏移量以反映从 in_fd 读取的字节数。

count 是在文件描述符之间复制的字节数。

返回值
如果传输成功,则返回写入 out_fd 的字节数。出错时,返回 -1,并适当设置 errno。

是的,这意味着返回值 0 意味着没有数据复制到写入套接字。

you can use offset parameter for read count.

According to Man page

If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start reading data from in_fd. When sendfile() returns, this variable will be set to the offset of the byte following the last byte that was read. If offset is not NULL, then sendfile() does not modify the current file offset of in_fd; otherwise the current file offset is adjusted to reflect the number of bytes read from in_fd.

count is the number of bytes to copy between the file descriptors.

RETURN VALUE
If the transfer was successful, the number of bytes written to out_fd is returned. On error, -1 is returned, and errno is set appropriately.

and yes that means return value 0 means no data copied to write socket.

诗酒趁年少 2024-09-25 15:17:33

当发送的字节数为 0 时,您可以假设已达到 EOF:

sent = sendfile(out_fd, in_fd, &offset, nbytes);
if (sent == 0) {
    // EOF
    ...
}

此假设也适用于非阻塞套接字的情况。

You can assume EOF has been reached when then number of bytes sent is 0:

sent = sendfile(out_fd, in_fd, &offset, nbytes);
if (sent == 0) {
    // EOF
    ...
}

This assumption works also in case of non-blocking sockets.

七度光 2024-09-25 15:17:33

就我而言,遇到文件被rsync截断,应用程序同时使用sendfile传输文件。我发现应用程序在这种情况下吃CPU 100%,我参考下面的文章修复了我的代码,问题消失了。
http://www.linuxjournal.com/article/6345

重点是使用 F_SETLEASE 获取文件您的应用程序的租赁。

in my case, encounter the file be truncate by rsync, app use sendfile to transmit file in the same time. I find the app eat cpu 100% in the condition, I fix my code refer the follow article , the question disappear.
http://www.linuxjournal.com/article/6345

the point is use F_SETLEASE get the file leases for your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文