Mmap () um arquivo grande inteiro
Estou tentando "mmap" um arquivo binário (~ 8 GB) usando o seguinte código (test.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
const char *memblock;
int fd;
struct stat sb;
fd = open(argv[1], O_RDONLY);
fstat(fd, &sb);
printf("Size: %lu\n", (uint64_t)sb.st_size);
memblock = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (memblock == MAP_FAILED) handle_error("mmap");
for(uint64_t i = 0; i < 10; i++)
{
printf("[%lu]=%X ", i, memblock[i]);
}
printf("\n");
return 0;
}
test.c é compilado usandogcc -std=c99 test.c -o test
efile
de retornos de teste:test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
Embora isso funcione bem para arquivos pequenos, eu recebo uma falha de segmentação quando tento carregar uma grande. O programa realmente retorna:
Size: 8274324021
mmap: Cannot allocate memory
Consegui mapear o arquivo inteiro usando boost :: iostreams :: mapped_file, mas quero fazê-lo usando C e chamadas de sistema. O que está errado com meu código