linux write 和 sendfile 系统调用之间的区别

发布于 2024-08-31 01:19:27 字数 285 浏览 1 评论 0原文

我正在编程网络服务器(C),它应该发送大文件。我的问题是: 两个系统调用的主要区别是什么:writesendfilesendfile 是否取决于套接字系统缓冲区的大小?我注意到 write 写的内容常常比我要求的要少。

例如,如果对一个文件有很多请求:我应该打开它,复制到内存中并使用write,还是可以为每个客户端执行sendfile

预先感谢所有答案。

Im programming webserver (C), which should send big files. My question is:
What are the main differences in two syscalls: write and sendfile. Does sendfile depends on size of socket system buffer? I noticed that write often writes less then I requested.

For example, if got many requests for one file: should i open it, copy into memory and use write, or maybe I can do sendfile for each client?

Thanks in advance for all answers.

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

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

发布评论

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

评论(1

べ繥欢鉨o。 2024-09-07 01:19:27

请阅读sendfile(2)。

sendfile() 在一个文件描述符和另一个文件描述符之间复制数据。由于此复制是在内核内完成的,因此 sendfile()read(2) 和 write(2) 的组合更高效,这需要将数据传输到用户空间或从用户空间传输数据。

关于返回值,任何 write/read/senfile 调用都不能保证整个数据块被写入/读取/发送

Please read sendfile(2).

sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more efficient than the combination of read(2) and write(2), which would require transferring data to and from user space.

Regarding return value any of write/read/senfile calls do not guarantee that entire block of data is written/read/sent

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