0

My understanding of mmap is very limited, let me know which of the following are correct. For the following scenario in a piece of program:

1. Process starts, call mmap()    // this is not actually loading anything from disk,
                                  // just allocates memory?

2. access data in the file        // this actually triggers the load from disk so
                                  // it takes longer?

3. at this point, the process is killed and restarted

4. Process starts, call mmap()    // this is not loading but the memory pointer
                                  // allocated is likely to be different?

5. access data in the file        // it takes roughly the same amount of time
                                  // as the first time

Is my understanding correct? I am especially confused about the part after the process is killed and restarted. Thanks!

WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30

1 Answers1

2

mmap "creates a new mapping in the virtual address space of the calling process". Unless you use MAP_POPULATE nothing is read from the file backing the mapping. (man page)

Accessing the file-backed mapping needs to bring in the data obviously. Whether physical I/O happens at this point depends on whether the OS has the page you're trying to access in its cache.

So I'd say your statements 1, 2, and 4 are true, while 5 might be not.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
  • https://stackoverflow.com/questions/45972/mmap-vs-reading-blocks has discussion with much more detail – Nickolay Jan 12 '18 at 18:20