19

In mmap() manpage:

Its prototype is:

void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

and description:

The mmap() function asks to map 'length' bytes starting at offset 'offset' 
from the file (or other object) specified by the file descriptor fd into 
memory, preferably at address 'start'.

Sepcifically, for the last argument:

'offset' should be a multiple of the page size as returned by getpagesize(2).

From what I practised, offset MUST be a multiple of the page size, for example, 4096 on my Linux, otherwise, mmap() would return Invalid argument, offset is for file offset, why it must be multiple of virtual memory system's page size?

Thanks,

password636
  • 981
  • 1
  • 7
  • 18

1 Answers1

16

The simple answer: to make it fast. The more complex answer: whenever you access the memory at a location within the mapped memory, the OS must make sure that this location is filled with the contents of the file. But the OS can only detect whether you access a memory page - not a single location. What it does is, it creates a simple relation between offsets in the file and memory pages - and whenever you access a memory page, that part of the file is loaded. To make these calculations fast, it restricts you to start at certain offsets.

Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
  • 1
    "the OS must make sure that this location is filled with the contents of the file. But the OS can only detect whether you access a memory page - not a single location." Could you be little more simple and clear here. Unable to absorb. Thanks! – Gaurav Minocha Jun 16 '16 at 17:44
  • @GauravMinocha The OS can only detect that an entire page is being read from (in this example 4096 bytes) not an individual byte address for example (in RAM every single byte of memory can be addressable). So entire pages of the file are loaded at a time. – jwbensley May 06 '17 at 10:30
  • But it a file, that's a different story, right? I mean, what stops the system from mapping that particular page (aligned to page boundary) to an _arbitrary_ offset in the file in its page table? – SasQ Sep 12 '20 at 09:53
  • @SasQ efficiency. This way they don't need to track & apply that additional offset. It makes it a tiny bit faster an that's what memory mapped files are about. It also allows re-use of the code for swap-files. – Tobias Langner Sep 14 '20 at 18:38