Linux >2.6.33: could sendfile() be used to implement a faster 'cat'?

独自空忆成欢 提交于 2019-12-06 09:45:49

Yes, the file pointer of the output fd will remain at its end (if the file is new or is not bigger than the data you already wrote to it).

The documentation for sendfile() explicitly mentions (emphasis mine):

In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file. If it is a regular file, then sendfile() changes the file offset appropriately.

I personally never saw an implementation of cat that relies on sendfile(), maybe because 2.6.33 is quite recent, and out_fd could not be fileno(stdout) before. sendfile() is also not portable, so doing that would result in a version of cat that only runs on Linux 2.6.33+ (although I guess it can still be implemented as a platform-dependent optimization activated at compile time).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!