Linux, will zeroed page pagefault on first read or on first write?

牧云@^-^@ 提交于 2019-11-30 04:11:02

问题


My question is Linux specific and needs understanding of kernel, virtual memory, mmap, pagefaults. I have C program with large static arrays, which will go into bss section (memory, initialized to zero). When program starts, this memory is not physically allocated, there is only virtual memory, and every page of virtual memory is mapped to the special zero page (the page of all zeroes). When application access this page, the pagefault will be generated and physical page will be allocated.

The question is: Will such pagefault be generated on first read access or on first write access to the page from bss section?


回答1:


Linux allocates a zero page to this memory (one zero page for the whole region) and then will essentially do COW (copy on write) behavior on the page because you are changing the contents. So you will not get read faults (unless the page was swapped out or its a minor page fault which means the page was in memory but not mapped).

So only write faults will cause a fault that causes allocation of a new page on the zero page.



来源:https://stackoverflow.com/questions/12115434/linux-will-zeroed-page-pagefault-on-first-read-or-on-first-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!