Why should the initial file offset in mmap () be a multiple of the page size

In mmap () manpage:

Its prototype:

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'. 

Septic, for the last argument:

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

From what I practiced, offset MUST be a multiple of the page size, for example 4096 on my Linux, otherwise mmap () will return Invalid argument , offset to offset the file, why should it be a multiple of the page size of the virtual memory system?

Thanks,

+6
source share
2 answers

The simple answer: do it fast. A more complicated answer: whenever you access memory at a location in the associated memory, the OS must make sure that this place is filled with the contents of the file. But the OS can only determine access to the memory page - not a single place. What he does, he creates a simple relationship between offsets in files and memory pages - and whenever you access a memory page, this part of the file is loaded. To make these calculations quick, it limits you to the beginning with certain offsets.

+4
source

All process memory is virtual.
"Virtual memory" is the place on the hard disk that is displayed in physical memory.

On the hard drive, data is stored in thin concentric ranges of tracks.

OS requires seeing space in a linear, continuous way.
HDD manufacturers adopted this model throughout the 90s.

The smallest allocated block on the hard drive is a sector that has a size of 512 bytes ... half a kilogram.

NTFS on Windows gives a default value of 4096 bytes as the size for the smallest allocated device on the hard drive in terms of the OS.

It will be a cluster of four sectors ... page size.

Source: http://ntfs.com/hard-disk-basics.htm

The page is often 4096 bytes because the Intel x86 MMU displays memory ...

through a series of tables, or rather. This is a paging directory and swap table. Both tables contain 1024 4 bytes of writing, making them each 4kb.

In the page table, each entry points to a physical address, which then maps to the virtual address found by calculating the offset within the directory and the offset in the table.

Please note that this means pages may be larger than 4096,
but this is the smallest possible block size for MMU!

Source: http://wiki.osdev.org/Paging#MMU

Output:
Size is determined by those who programmed / developed the operating system. It can be any multiple of 4096 bytes.

-2
source

Source: https://habr.com/ru/post/958467/


All Articles