sendfile()
Linux 2.6.32でシステムコールをテストして、 2つの通常のファイル間でデータをゼロコピーしようとしています。私が理解している限り、これは機能するはずです。2.6.22以降、sendfile()
を使用して実装されておりsplice()
、入力ファイルと出力ファイルの両方を通常のファイルまたはソケットにすることができます。
以下はその内容ですsendfile_test.c
:
#include <sys/sendfile.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv) {
int result;
int in_file;
int out_file;
in_file = open(argv[1], O_RDONLY);
out_file = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
result = sendfile(out_file, in_file, NULL, 1);
if (result == -1)
perror("sendfile");
close(in_file);
close(out_file);
return 0;
}
そして、私が次のコマンドを実行しているとき:
$ gcc sendfile_test.c
$ ./a.out infile outfile
出力は
sendfile: Invalid argument
そして走っているとき
$ strace ./a.out infile outfile
出力には次のものが含まれます
open("infile", O_RDONLY) = 3
open("outfile", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 1) = -1 EINVAL (Invalid argument)
私は何が間違っているのですか?