lkml.org 
[lkml]   [2012]   [Sep]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: Advice on mmap() "feature"
From
On Sun, Sep 16, 2012 at 9:21 PM, Mark Jackson <mpfj-list@mimc.co.uk> wrote:
> Apologies if this is the wrong place to post this query. Please feel free
> to redirect me to the correct place.
>
> I have come across a weird (but documented [1]) "feature" of mmap(), which
> is:-
>
> "The mmap()function adds an extra reference to the file associated with the
> file descriptor fildeswhich is not removed by a subsequent close() on that
> file descriptor. This reference is removed when there are no more mappings
> to the file."
>
> In my embedded application, this is resulting in the consumption of
> available file descriptors.
>
> As an example I wrote the following test running on Ubuntu 11.04 64bit.
>
> #include <fcntl.h>
> #include <errno.h>
> #include <stdio.h>
> #include <sys/mman.h>
>
> #define FRAM_SIZE (64 * 1024)
> #define FRAM_BASE_ADDRESS 0x00000000
>
> int main(int argc, char **argv)
> {
> int i;
> for (i = 0; i < 5000; i++)
> {
> // open the fram device
> int m_fdFram = open("/dev/mem", O_RDWR | O_SYNC);
> if (m_fdFram < 0)
> {
> printf("could not open /dev/mem, exiting with errno %d\n",
> errno);
> return 0;
> }
>
> // map the device to memory
> char *m_pFram = (char *)mmap( 0,
> FRAM_SIZE * sizeof(unsigned short),
> PROT_READ | PROT_WRITE,
> MAP_SHARED,
> m_fdFram,
> FRAM_BASE_ADDRESS);
> if ((int)m_pFram == -1)
> {
> close(m_fdFram);
> m_fdFram = -1;
> printf("could not mmap, exiting with errno %d\n", errno);
> return 0;
> }
>
> // now remove the mapping
> if (m_pFram)
> munmap(m_pFram, FRAM_SIZE);

You unmap less memory than you map...

--
Thanks,
//richard


\
 
 \ /
  Last update: 2012-09-16 23:21    [W:0.040 / U:0.204 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site