Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
833 views
in Technique[技术] by (71.8m points)

linux - C streams: Copy data from one stream to another directly, without using a buffer

I want to copy data from one stream to another. Now normally, I would do it this way:

n = fread(buffer, 1, bufsize, fin);
fwrite(buffer, 1, n, fout);

Is there a way to write the data directly from fin to fout, without going through a buffer, i.e. instead of fin->buffer->fout, I want to directly do fin->fout (no buffer).

Is it possible to do so in ANSI C? If not, is it possible to do it with POSIX functions? Or a Linux-specific solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

2 possible Linux-only solutions are splice() and sendfile(). What they do is copy data without it ever leaving kernel space, thus making a potentially significant performance optimization.

Note that both have limitations:

  • sendfile() requires a socket for its output for Linux kernels before 2.6.33, after that, any file can be the output, and also it requires the input to support mmap() operations, meaning the input can't be stdin or a pipe.

  • splice() requires one of the input or output streams to be a pipe (not sure about both), and also for kernel versions 2.6.30.10 and older, it requires the file system for the stream that is not a pipe to support splicing.

Edit: Note that some filesystems might not support splicing for Linux 2.6.30.10 and below.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...