简体   繁体   中英

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

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?

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.

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