假設,假設我想對可能非常大的文件執行順序寫入。madvise(___,___,MADV_DONTNEED)是否指示操作系統懶惰地寫入磁盤?
如果我在整個區域上繪製了一個巨大的區域和madvise(MADV_SEQUENTIAL),那麼我可以以相對高效的方式寫入內存。這我已經工作得很好。
現在,爲了釋放各種操作系統資源,我偶爾會在已經寫入的小塊內存上執行munmap()。我擔心的是,munmap()和msync()會阻塞我的線程,等待數據被物理提交到磁盤。我不能放慢我的作家,所以我需要找到另一種方式。
在小的,已經寫好的內存塊上使用madvise(MADV_DONTNEED)會更好嗎?我想告訴操作系統將該內存寫入磁盤,而不是阻止我的調用線程。
上的madvise的手冊頁()是這樣說的,這是相當含糊:
MADV_DONTNEED
Do not expect access in the near future. (For the time being, the
application is finished with the given range, so the kernel can free
resources associated with it.) Subsequent accesses of pages in this
range will succeed, but will result either in re-loading of the memory
contents from the underlying mapped file (see mmap(2)) or
zero-fill-on-demand pages for mappings without an underlying file.
我不會試試這個;文件映射上的「MADV_DONTNEED」可能會被解釋爲意味着您希望操作系統對文件進行更改。 – zwol 2013-02-20 02:33:45
@Zack,你有沒有參考MADV_DONTNEED丟棄對文件的更改? – Anton 2013-03-14 19:54:55
@antonm http://man7.org/tlpi/code/online/dist/vmem/madvise_dontneed.c.html有一個程序來演示它(不是很自包含,但很容易修改)。另請參閱https://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html(「'MADV_DONTNEED」:該區域不再需要,內核可釋放這些頁面,*導致任何更改頁面丟失*「(重點是我的))以及2005年的這個LKML線程:https://lkml.org/lkml/2005/6/28/188。 – zwol 2013-03-14 20:27:15