-1

if mmap() was used to read a file, how can I find the number of data mapped by mmap().

float *map = (float *)mmap(NULL, FILESIZE, PROT_READ, MAP_SHARED, fd, 0);
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
tsch
  • 15
  • 5

1 Answers1

4

The mmap system call does not read data. It just maps the data in your virtual address space (by indirectly configuring your MMU), and that virtual address space is changed by a successful mmap. Later, your program will read that data (or not). In your example, your program might later read map[356] if mmap has succeeded (and you should test against its failure).

Read carefully the documentation of mmap(2). The second argument (in your code, FILESIZE) defines the size of the mapping (in bytes). You might check that it is a multiple of sizeof(float) and divide it by sizeof(float) to get the number of elements in map that are meaningful and obtained from the file. The size of the mapping is rounded up to a multiple of pages. The man page of mmap(2) says:

A file is mapped in multiples of the page size. For a file that is not a multiple of the page size, the remaining memory is zeroed when mapped, and writes to that region are not written out to the file.

Data is mapped in pages. A page is usually 4096 bytes. Read more about paging.

The page size is returned by getpagesize(2) or by sysconf(3) with _SC_PAGESIZE (which usually gives 4096).

Consider reading some book like Operating Systems: Three Easy Pieces (freely downloadable) to understand how virtual memory works and what is a memory mapped file.

On Linux, the /proc/ filesystem (see proc(5)) is very useful to understand the virtual address space of some process: try cat /proc/$$/maps in your terminal, and read more to understand its output. For a process of pid 1234, try also cat /proc/1234/maps

From inside your process, you could even read sequentially the /proc/self/maps pseudo-file to understand its virtual address space, like here.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • so how can i find the number of data being mapped by mmap. Thank you! – tsch Jan 30 '18 at 05:54
  • 1
    It is the second argument to `mmap` rounded up to the page size. You might divide that by `sizeof(float)` in your case to get the number of elements – Basile Starynkevitch Jan 30 '18 at 06:06
  • Thanks for your answer! I understand data is mapped in pages. For example, if the file I'm trying to map is only of 3000 bytes, after data is mapped, it will round up to one page. The rest part of page is not used.So how do I know the size of those "valid" data from the original file. – tsch Jan 30 '18 at 06:18
  • My answer and comment explained that. Please take time to read them more carefully. Read also the *Operating Systems: Three Eeasy Pieces* book... It seems you don't understand how virtual memory works and what is a virtual address space. I gave references, but an entire book is needed to explain that. You should spend a few days reading it. – Basile Starynkevitch Jan 30 '18 at 06:20
  • ... your main issue is to understand what is virtual memory and what is the virtual address space of some process. You need to read books about these. That takes days. We don't have enough space on StackOverflow to write books. – Basile Starynkevitch Jan 30 '18 at 06:21
  • 1
    Perfect answer, very detailed with a lot of additional info! +1 – Markus Weninger Jan 30 '18 at 06:43