functions order when do memory mapping - Linux

This is a discussion on functions order when do memory mapping - Linux ; A: open mmap munmap close or B: open mmap close munmap Both works well, I wonder which is better?...

+ Reply to Thread
Results 1 to 5 of 5

Thread: functions order when do memory mapping

  1. functions order when do memory mapping

    A:
    open
    mmap
    munmap
    close

    or

    B:
    open
    mmap
    close
    munmap

    Both works well, I wonder which is better?

  2. [fixed] Re: functions order when do memory mapping

    Ronald wrote:
    > A:
    > open
    > mmap

    do io here
    > munmap
    > close
    >
    > or
    >
    > B:
    > open
    > mmap
    > close

    do io here
    > munmap
    >
    > Both works well, I wonder which is better?


  3. Re: functions order when do memory mapping

    Ronald writes:

    > A:
    > open
    > mmap
    > munmap
    > close
    >
    > or
    >
    > B:
    > open
    > mmap
    > close
    > munmap
    >
    > Both works well, I wonder which is better?


    It doesn't matter in general. The only time it could possibly matter
    is if the file is deleted while open and/or mapped. In both cases,
    the OS keeps reference counts on the inode so everything works.

    --
    Måns Rullgård
    mru@inprovide.com

  4. Re: functions order when do memory mapping


    "Måns Rullgård"
    ??????:yw1xzm9dl1du.fsf@agrajag.inprovide.com...
    > Ronald writes:
    >
    >> A:
    >> open
    >> mmap

    ....
    >> munmap
    >> close
    >>
    >> or
    >>
    >> B:
    >> open
    >> mmap
    >> close

    ....
    >> munmap
    >>
    >> Both works well, I wonder which is better?

    >
    > It doesn't matter in general. The only time it could possibly matter
    > is if the file is deleted while open and/or mapped. In both cases,
    > the OS keeps reference counts on the inode so everything works.


    If so, I think the 2nd is better.



  5. Re: functions order when do memory mapping

    "Ronald" writes:

    > "Måns Rullgård"
    > ??????:yw1xzm9dl1du.fsf@agrajag.inprovide.com...
    >> Ronald writes:
    >>
    >>> A:
    >>> open
    >>> mmap

    > ...
    >>> munmap
    >>> close
    >>>
    >>> or
    >>>
    >>> B:
    >>> open
    >>> mmap
    >>> close

    > ...
    >>> munmap
    >>>
    >>> Both works well, I wonder which is better?

    >>
    >> It doesn't matter in general. The only time it could possibly matter
    >> is if the file is deleted while open and/or mapped. In both cases,
    >> the OS keeps reference counts on the inode so everything works.

    >
    > If so, I think the 2nd is better.


    It only makes a difference if you want the ability to change the
    mapping later on, and the file may have been deleted in the meantime.
    Keeping the file descriptor open allows you to make new mappings at
    any time, even if all links to the inode have been deleted from the
    filesystem. Keeping the file open is of course always more efficient
    than reopening it each time a new mapping is needed. On the other
    hand, if only a single mapping will ever be needed, closing the file
    once the mapping is set up will free up a file descriptor, and
    associated OS resources, which is generally a good thing. As always,
    which way is best depends entirely on the circumstances.

    --
    Måns Rullgård
    mru@inprovide.com

+ Reply to Thread