I am trying to test a system call sendfile()on Linux 2.6.32 to zero copy data between two regular files. As far as I understand, it should work: since 2.6.22, it sendfile()was implemented using splice(), and both the input file and the output file can be either ordinary files or sockets.
The following is the content 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;
}
And when I run the following commands:
$ gcc sendfile_test.c
$ ./a.out infile outfile
Output signal
sendfile: Invalid argument
And at startup
$ strace ./a.out infile outfile
The conclusion contains
open("infile", O_RDONLY) = 3
open("outfile", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 1) = -1 EINVAL (Invalid argument)
What am I doing wrong?
source
share