mmap'ing a large file

From: Mike Black (mblack@csihq.com)
Date: Wed Aug 14 2002 - 10:42:16 EST


Is there a logical reason why a process can't mmap more than a 2G file?

I seem to get stuck at 2142208000 with
mmap: Cannot allocate memory

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

#define FILESIZE 2500000000

int
main ()
{
    unsigned long long offset = 0;
    unsigned long maplength = getpagesize () * 1000;
    int i;
    unsigned char *p;
    char mynull = 0;
    int fd = open ("test.map", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd < 0) {
        perror ("test.map");
        exit (-1);
    }
    lseek (fd, FILESIZE - 1, SEEK_SET);
    write (fd, &mynull, 1);
    for (offset = 0; offset < FILESIZE - maplength; offset += maplength) {
        p = mmap (p, maplength, PROT_READ | PROT_WRITE, MAP_SHARED, fd,offset);
        printf ("%lld %p\n", offset, p);
        fflush (stdout);
        if (p == (unsigned char *) -1) {
            perror ("mmap");
            exit (-1);
        }
        memset (p, 1, maplength);
#if 0
        munmap (p, maplength); /* this of course let's things go on */
#endif
    }
    return 0;
}

Michael D. Black mblack@csi-inc.com
http://www.csi-inc.com/
http://www.csi-inc.com/~mike
321-676-2923, x203
Melbourne FL

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Thu Aug 15 2002 - 22:00:36 EST