Linux> 2.6.33:sendfile()是否可用于实现更快的"cat"? [英] Linux >2.6.33: could sendfile() be used to implement a faster 'cat'?

查看:130
本文介绍了Linux> 2.6.33:sendfile()是否可用于实现更快的"cat"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于必须将许多大文件连接成一个更大的文件,因此我们目前使用

Having to concatenate lots of large files into an even larger single one, we currently use

cat file1 file2 ... output_file

但想知道是否可以比和那个老朋友更快地完成这项工作.

but are wondering whether it could be done faster than with that old friend.

阅读sendfile()的手册页,您可以指定* input_file *的偏移量,然后将其其余部分从其中发送到* output_file *.但是:我还可以在* output_file *中指定一个偏移量吗? 还是我可以简单地通过打开输出FD并将sendfile()反复放入其中,从而有效地串联* input_files *来循环遍历所有输入文件? 换句话说:如果我不关闭它,也不在其中寻找(),进入输出FD的文件指针会保留在它的末尾吗?

Reading the man page of sendfile(), one can specify an offset into *input_file*, from where to send the remainder of it to *output_file*. But: can I also specify an offset into *output_file*? Or could I simply loop over all input files, simply by leaving open my output FD and sendfile()'ing repeatedly into it, effectively concatenating the *input_files*? In other words: would the filepointer into my output FD remain at its end, if I do not close it nor seek() in it?

有人知道使用sendfile()的这种cat实现吗?

Does anybody knows of such a cat implementation using sendfile()?

诚然,我是管理员,而不是程序员,所以请忍受我缺乏真正的"编码知识...

Admittedly, I'm an admin, not a programmer, so please bear with my lack of 'real' coding knowledge...

推荐答案

是的,输出fd的文件指针将保留在其末尾(如果文件是新文件或不大于已写入文件的数据)

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).

sendfile()的文档明确提到(强调我的意思):

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

在2.6.33之前的Linux内核中,out_fd必须引用套接字.自从 Linux 2.6.33可以是任何文件. 如果是常规文件,则 sendfile()适当地更改文件偏移量.

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.

我个人从未见过依赖于sendfile()cat的实现,也许是因为2.6.33是最近的,而out_fd以前不是fileno(stdout)的. sendfile()也不是可移植的,因此这样做会导致cat的版本只能在Linux 2.6.33+上运行(尽管我想它仍可以实现为在编译时激活的依赖于平台的优化).

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).

这篇关于Linux> 2.6.33:sendfile()是否可用于实现更快的"cat"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆