views:

67

answers:

4

Hi all, I'm new to 64-bits architecture. Could you tell me what's MAX file size supported by file mapping in 64 bits linux machine. I want to open more than 20GB files by file mapping, is it available? Thanks

Thanks for your replies. Now I write a sample code. But it causes Bus Error when I get the value of the pointer in GBSIZE offset:

unsigned char* pCur = pBegin + GBSIZE;
//pBegin is the pointer returned by mmap
printf("%c",*pCur); 

BTW, printf("%c",*pBegin ); works fine. and my address sizes : 38 bits physical, 48 bits virtual

Sorry, I made a mistake. I opened a 50M file, and mmap 20GB length, it causes that problem, but if I open a 20GB file instead, it works fine. Although my problem is solved, I still have a question about why the Bus Error happens when I open a small file and mmap a large length.

Below is my codes:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

//#define FILEPATH "smallfile"
#define FILEPATH "bigfile"
#define GBSIZE (1024L*1024L*1024L)
#define TBSIZE (1024L*GBSIZE)
#define NUMSIZE  (20L * GBSIZE)
//#define NUMSIZE  (10)
#define FILESIZE (NUMINTS * sizeof(int))

int main(int argc, char *argv[])
{
    int i;
    int fd;
    unsigned char *pBegin;

    fd = open(FILEPATH, O_RDONLY);
        if (fd == -1) {
        perror("Error opening file for reading");
        exit(EXIT_FAILURE);
    }

    pBegin = mmap(0, NUMSIZE, PROT_READ, MAP_SHARED, fd, 0);
    if (pBegin == MAP_FAILED) {
        close(fd);
        perror("Error mmapping the file");
        exit(EXIT_FAILURE);
    }

    /** ERROR happens here!!! **/
    unsigned char* pCur = pBegin + GBSIZE;
    printf("%c",*pCur);

    if (munmap(pBegin, NUMSIZE) == -1) {
        perror("Error un-mmapping the file");
    }
    close(fd);
    return 0;
}
+1  A: 

64-bit addresses allow for many orders of magnitude more than 20 GB.

Max Shawabkeh
+3  A: 

From the mmap(2) man page:

   void *mmap(void *addr, size_t length, int prot, int flags,
              int fd, off_t offset);

length is a size_t, which on 64-bit machines is 64 bits in length. Therefore yes, you can theoretically map a 20GB file.

Ignacio Vazquez-Abrams
A: 

Thought it was a Terabyte....

Hope that helps, Best regards, Tom.

tommieb75
Maybe I am mixing up with something else...it's late, in bed...need sleep....
tommieb75
+3  A: 

Although pointers are 64-bit wide, most processors do not actually support virtual addresses using the full 64 bits. To see what size virtual addresses your processor supports, look in /proc/cpuinfo (48 bits is typical).

grep "address sizes" /proc/cpuinfo

Additionally, half of the virtual address space is used by the kernel and not available to userspace - leaving 47 bits in the current Linux implementation.

However, even taking this into account, you will still have plenty of room for a 20GB file. 47 bits in theory means a virtual address space of 128TB.

caf
I've tried to mmap a 20GB file by file mapping. but it causes Bus Error when I get the value of the pointer in GBSIZE offset:unsigned char* pCur = pBegin + GBSIZE; //pBegin is the pointer returned by mmapprintf("%c",*pszPointer);BTW, "printf("%c",*pBegin );" works fine.
If you add the offset to the beginning of the mapping, you get a pointer to the byte BEYOND the end of the mapping, therefore a bus error would be exactly what I'd expect.
MarkR