Re: functions order when do memory mapping



"Ronald" <followait@xxxxxxx> writes:

"Måns Rullgård" <mru@xxxxxxxxxxxxx>
??????:yw1xzm9dl1du.fsf@xxxxxxxxxxxxxxxxxxxxxxxx
Ronald <followait@xxxxxxx> 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@xxxxxxxxxxxxx
.



Relevant Pages

  • Re: File Map
    ... multiple processes can map the same file into their address ... space using the mmap() function. ... and pass the file descriptor to mmap. ... is this named mapping mapping? ...
    (comp.sys.mac.programmer.help)
  • Re: File Map
    ... multiple processes can map the same file into their address ... space using the mmap() function. ... and pass the file descriptor to mmap. ... is this named mapping mapping? ...
    (comp.sys.mac.programmer.help)
  • [PATCH, RFD] Unbreaking nommu mmap, v2
    ... A while ago I sent out a patch to change mmap behaviour on nommu targets. ... Currently, the kernel requires a munmap call for every mmap, with the exact same address range; this is inconsistent with behaviour on mmu systems, and complicates the malloc implementation in uClibc. ... Every VMA can be in two states: either it manages a power-of-2 sized compound ... mapping we're capable of supporting ...
    (Linux-Kernel)
  • Re: How wrong is this analogy for the curvature of Spacetime?
    ... >> a mapping method, but I can see why that may be so wrt curvature. ... > I seriously doubt either Dark matter or Dark Energy exists. ... >> majority who will not speak up to help the Hobbles straighten up their ...
    (sci.physics.relativity)
  • Re: sharing memory map between processes (same parent)
    ... | 'warm the cache' by getting the inode into memory. ... It might make a difference if the memory remained mapped to some process ... How about we take a look at the approximate steps that mmap needs to ... So, whether the mapping is transferred to a second process, or the second ...
    (comp.unix.programmer)