1

我想打开一个大小为 100GB 的文件,并且每次都使用文件映射从其中逐块读取数据。当偏移量大于 2GB 时,它总是会开始映射任何内容。我想我可能是不支持 64 位寻址的函数。但是在我添加大文件支持之后(包括大文件支持定义、大文件打开选项以及使用命令-D_FILE_OFFSET_BITS=64 -D_LARGE_FILE 编译)。但是,同样的问题仍然存在。这是简化的代码:

#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <math.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<sys/mman.h>
#include<sys/types.h>

#define PERMS 0600

int total_piece, PAGE, buffer, share, offset, count, chunk;

void get_size(char * strFileName)   
{  
    struct stat temp;  
    stat(strFileName, &temp);

    PAGE = getpagesize();             
    total_piece = temp.st_size/PAGE;
    chunk = 1024*1024*1024*0.4/PAGE; 

    if (temp.st_size%PAGE!=0)     
    total_piece++;
}

char *
mmaping (char *source)
{
  int src;
  char *sm;
  struct stat statbuf;

  if ((src = open (source, O_RDONLY)) < 0)  //I thought error comes from this line. So I tried to use large file support as following. But still the same. 
    {
      perror (" open source ");
      exit (EXIT_FAILURE);
    }
/*
  if ((src = open64(source, O_RDONLY|O_LARGEFILE, 0644))<0)  
    {
      perror (" open source ");
      exit (EXIT_FAILURE);
    }
*/
  if (fstat (src, &statbuf) < 0)
    {
      perror (" fstat source ");
      exit (EXIT_FAILURE);
    }

  printf("share->%d PAGES per node\n",share);

  if (share>=chunk)
  buffer = chunk;
  else
  buffer = share;

  printf("total pieces->%d\n",total_piece);
  printf("data left->%d\n",share);
  printf("buffer size->%d\n",buffer);
  printf("PAGE size->%d\n",PAGE);

  sm = mmap (0,buffer*PAGE, PROT_READ, MAP_SHARED | MAP_NORESERVE,src, offset*PAGE); 

  if (MAP_FAILED == sm)
    {
      perror (" mmap source ");
      exit (EXIT_FAILURE);
    }

  return sm;
}

main(int argc, char**argv){

   get_size(argv[1]);

   share = total_piece;

   offset = 0;

   while (share>0)
   {

      char *x = mmaping(argv[1]);

      printf("data->%0.30s\n",x); //bus error will occur when offset reaches 2GiB, which proves my thought: it maps          nothing.

      munmap(x,buffer*PAGE);  

      share-=buffer;

      offset+=buffer;

   }

   return 0;
}

任何人都可以很好地帮助我吗?

4

1 回答 1

5

当然,“int”类型的变量(在 Linux 上为 32 位)不足以容纳 100 GB 文件的大小(以字节为单位)。对于文件大小/偏移量,您需要使用“off_t”类型(当您启用 LFS 支持时,它是 off64_t 的别名,一个有符号的 64 位整数)。

同样,mmap 的“length”参数是 size_t 类型,而不是 int。

要使代码可移植到 32 位和 64 位目标,无论是否使用 LFS,您需要注意应在何处使用哪些整数类型。

于 2012-04-23T09:32:44.920 回答