mmap() problem - Unix

This is a discussion on mmap() problem - Unix ; Hi. I was using mmap() in Solaris 10 Unix and I've been having some trouble. When I go and mmap() a file of a given length, then pass that to a routine in the program with that exact same data ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: mmap() problem

  1. mmap() problem

    Hi.

    I was using mmap() in Solaris 10 Unix and I've been having some
    trouble. When I go and
    mmap() a file of a given length, then pass that to a routine in the
    program with that
    exact same data length (i.e. the length of the file and mmaped region
    are equal), it fails
    with a "Bus error", "object specific error". Yet if I add as little as
    1 byte to the end of the
    file, it works OK, no more bus error.

    What gives with this thing, anyway?

    P.S. Data length is 16,777,216 bytes.

  2. Re: mmap() problem

    mike3 wrote:
    > Hi.
    >
    > I was using mmap() in Solaris 10 Unix and I've been having some
    > trouble. When I go and
    > mmap() a file of a given length, then pass that to a routine in the
    > program with that
    > exact same data length (i.e. the length of the file and mmaped region
    > are equal), it fails
    > with a "Bus error", "object specific error". Yet if I add as little as
    > 1 byte to the end of the
    > file, it works OK, no more bus error.
    >

    Posting the code would help.

    --
    Ian Collins.

  3. Re: mmap() problem

    mike3 writes:

    > I was using mmap() in Solaris 10 Unix and I've been having some
    > trouble. When I go and mmap() a file of a given length, then pass
    > that to a routine in the program with that exact same data length
    > (i.e. the length of the file and mmaped region are equal), it fails
    > with a "Bus error", "object specific error".


    Generally that means that you've mmap()ed more pages then there
    are in the file. From "man mmap":

    The mmap() function allows [pa, pa + len) to extend beyond
    the end of the object both at the time of the mmap() and
    while the mapping persists, such as when the file is created
    prior to the mmap() call and has no contents, or when the
    file is truncated. Any reference to addresses beyond the end
    of the object, however, will result in the delivery of a
    SIGBUS or SIGSEGV signal. The mmap() function cannot be used
    to implicitly extend the length of files.


    > Yet if I add as little as 1 byte to the end of the
    > file, it works OK, no more bus error.


    Sounds like you have a file with size == N*getpagesize(), and you are
    miscalculating its size by 1 page.

    Wrong code to round size up to whole page:

    int pagesize = getpagesize();
    off_t sz = pagesize * (st.st_size / pagesize + 1);
    // sz is correct, except when st.st_size % pagesize == 0

    Correct:

    off_t sz = (st.st_size + pagesize - 1) & ~(pagesize - 1)

    > P.S. Data length is 16,777,216 bytes.


    Confirms my theory: 16777216 == 0x1000000

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

+ Reply to Thread