我正在尝试在 Linux 2.6.32 下测试 sendfile()
系统调用以在两个常规文件之间进行零拷贝数据。
据我了解,它应该可以工作:从 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)
我做错了什么?
最佳答案
您忘记检查 argc
是否等于 3
,即您打开名称为 argv[2]
的输出文件,但是只给你的程序一个参数(并且你不会在 open(2)
之后检查错误。)
您可以使用 strace(1)
找出哪个系统调用失败。
编辑:
这对我来说看起来像是旧内核。相同的源代码(模数错误检查)在 2.6.33.4 #3 SMP
下工作正常。另外,您只复制一个字节有什么特别的原因吗?
#include <sys/sendfile.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( int argc, char *argv[] )
{
int in_file, out_file;
if ( argc != 3 )
{
fprintf( stderr, "usage: %s <in-file> <out-file>\n", argv[0] );
exit( 1 );
}
if (( in_file = open( argv[1], O_RDONLY )) == -1 ) err( 1, "open" );
if (( out_file = open( argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644 )) == -1 )
err( 1, "open(2)" );
if ( sendfile( out_file, in_file, NULL, 4096 ) == -1 ) err( 1, "sendfile" );
exit( 0 );
}
跟踪:
nickf@slack:~/csource/linux/splice$ cc -Wall -pedantic -ggdb -g3 -o sf sndf.c
nickf@slack:~/csource/linux/splice$ strace ./sf Makefile mm
...
open("Makefile", O_RDONLY) = 3
open("mm", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 4096) = 239
exit_group(0) = ?
nickf@slack:~/csource/linux/splice$ diff Makefile mm
nickf@slack:~/csource/linux/splice$
关于c - 带有两个常规文件的 sendfile() 中的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2945372/