3

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)

私は何が間違っているのですか?

4

1 に答える 1

6

argcに等しいことを確認するのを忘れました3。つまり、名前で出力ファイルを開いていますがargv[2]、プログラムに引数を1つだけ指定しています(その後、エラーを確認していませんopen(2))。

strace(1)どのシステムコールが失敗したかを見つけるために使用できます。

編集:

これは私には古いカーネルのように見えます。同じソース(モジュロエラーチェック)は、ここでは正常に機能します 2.6.33.4 #3 SMP。また、1バイトだけをコピーしている特別な理由はありますか?

#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$
于 2010-05-31T18:53:51.240 に答える