Heptic
Heptic

Reputation: 3106

Do writes to mmap'd memory ever block?

For example, on a machine with 2 GB of physical memory: if I mmap'd a 100GB file and started spraying writes as fast as I can, what is the expected behaviour? Would writing to memory block while pages are being flushed to disk, or would the system buffers get out of control eventually die with some sort of out of memory errors?

Upvotes: 4

Views: 1371

Answers (2)

Kevin
Kevin

Reputation: 30151

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.

Upvotes: 1

Leonidaz0r
Leonidaz0r

Reputation: 340

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

Upvotes: 1

Related Questions