简体   繁体   中英

Calling msync Necessary?

After reading few articles and posts: https://lwn.net/Articles/502612/ mmap, msync(MS_ASYNC) and munmap

I am kind of confused:
1. whether msync really does anything (2nd link above includes a section of a manpage to indicate it doesn't in newer kernels; tho, the first link says it does do something)
2. whether calling msync is necessary if a process might get killed or crashed (but not the OS).
3. how frequent does the kernel flush the mmapped memory back to the "disk"?

Let's say I use kernel 3.10 and 2.6.32.

Thanks.

We need to be careful - your question title says "Calling msync Necessary? " but msync() takes parameters complicating an answer:

  1. On Linux msync(MS_ASYNC) is a no-op. On other OSes it may do something and of course you can call msync() with other parameters.
  2. If you need to know whether syncing completed and you're willing to block until it has, you can use msync(MS_SYNC) (notice the lack the A). If just your program crashes the Linux kernel will continue to track dirty filesystem pages .
  3. This is going to depend on your filesystem and VM settings. See the dirty_* settings in https://www.kernel.org/doc/Documentation/sysctl/vm.txt .

This question is similar to the having linux persist memory changes to disk - if you need to know WHEN your data is on stable media you will have to use a blocking call (which will trigger immediate writeback). There's no "please start syncing in the background" call on Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM