mmap clarification: what happens after process restarts

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!

1 answer

  • answered 2018-01-12 18:19 Nickolay

    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.