Do writes to mmap'd memory ever block? Do writes to mmap'd memory ever block? unix unix

Do writes to mmap'd memory ever block?


Yes the calls would block until the write back action is done.

Any access to a page in memory which is not cached will generate a page fault, so a trap which stops execution and hands control back to the OS. The OS will schedule all IO operations necessary to load the page from memory(or harddisk if it was swapped out). Then it will run other processes until the IO is done. When IO is done your process will be appended back to the queue of processes ready to be executed.

It is not important whether something is swapped out or in memory, as soon as it is not cached execution will be stopped until the data is available in cache, so your call blocks.

It is not actually important what kind of memory you are accessing. When you mmap a file, then it will behave similar to swapped out memory. Access to contents of it will bring it into the cache and when the space in cache is needed, then it will be written back to memory or to disk. The memory really behaves like a cache for your swap space when it is filled.

You can read about page faults here: https://en.wikipedia.org/wiki/Page_fault


You'd swap a lot. mmap() just means you're swapping with a normal file rather than a swap partition or file.

Of course, this only applies if you mmap()ed a file. If you made an anonymous mapping (i.e. MAP_ANONYMOUS), you're subject to the usual rules.

You can also trigger some of the swapping now rather than later using MAP_POPULATE, which is similar to blocking.