2014-09-29 111 views
2

如果我在內存映射區域調用msync和MS_ASYNC,同步過程將被異步處理。mmap,msync(MS_ASYNC)和munmap

但是,如果我立即在該區域調用munmap,我可以假設msync將安全執行嗎?或者我必須在munmap之前調用msync?

回答

0

簡短答案是肯定的 - 對內容的更改將最終(並且安全地)進入文件,即使您從未請致電msync。從man 2 mmap

MAP_SHARED 
      Share this mapping. Updates to the mapping are visible to other 
      processes that map this file, and are carried through to the 
      underlying file. (To precisely control when updates are carried 
      through to the underlying file requires the use of msync(2).) 

也許更重要的是,man 2 msync有這樣一個字條:

由於Linux 2.6.19,MS_ASYNC事實上是一個空操作,因爲內核正確跟蹤髒頁面並根據需要將其刷新到存儲空間。

記住:mmap直接暴露在文件系統緩存的頁面,用戶空間和,就像任何其他的緩存系統,變化將在未來的某一時刻被傳播到後備存儲。即使您的程序崩潰,您對頁面所做的更改也會最終傳播。 msync的使用是爲了保護你免受電力損失之類的事情。