POSIX provides a memory-mapped equivalent of the fsync() system call that we discussed in Chapter 2: #include <sys/mman.h> int msync (void *addr, size_t len, int flags); A call to msync() flushes back to disk any changes made to a file mapped via mmap(), synchronizing the mapped file with the mapping. Specifically, the file or subset of a file associated with the mapping starting at memory address addr and continuing forlenbytes is synchronized to disk. Theaddrargument must be page-aligned; it is generally the return value from a previousmmap()invocation. Without invocation ofmsync(), there is no guarantee that a dirty mapping will be written back to disk until the file is unmapped. This is different from the behavior ofwrite(), where a buffer is dirtied as part of the writing process, and queued for writeback to disk. When writing into a memory mapping, the process directly modifies the file’s pages in the kernel’s page cache, without kernel involvement. The kernel may not synchronize the page cache and the disk anytime soon. Theflagsparameter controls the behavior of the synchronizing operation. It is a bitwise OR of the following values: MS_ASYNC
MS_INVALIDATE
MS_SYNC
EitherMS_ASYNCorMS_SYNCmust be specified, but not both. Usage is simple: if (msync (addr, len, MS_ASYNC) == -1) This example asynchronously synchronizes (say that 10 times fast) to disk the file mapped in the region[addr,addr+len). On success, msync() returns 0. On failure, the call returns -1, and setserrnoappropriately. The following are validerrnovalues: EINVAL
ENOMEM
Before version 2.4.19 of the Linux kernel,msync()returnedEFAULTin place ofENOMEM.
blog comments powered by Disqus |