Linux,在首次读取或首次写入时将页面pagefault置零吗? [英] Linux, will zeroed page pagefault on first read or on first write?

查看:173
本文介绍了Linux,在首次读取或首次写入时将页面pagefault置零吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是Linux特定的,需要了解内核,虚拟内存,mmap,页面错误.我有带有大型静态数组的C程序,它将进入bss部分(内存,初始化为零).程序启动时,不会物理分配此内存,只有虚拟内存,并且虚拟内存的每一页都映射到特殊的零页(所有零的页).当应用程序访问此页面时,将生成页面错误并分配物理页面.

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.

问题是:这种页面错误会在从bss部分对该页面的首次读取访问或首次写入访问时生成吗?

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

推荐答案

Linux会为此内存分配一个零页(整个区域为一个零页),然后实质上将对该页执行COW(写时复制)行为,因为您正在更改内容.这样您就不会出现读取错误(除非页面被换出,或者它是次要的页面错误,这意味着该页面已在内存中但未映射).

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.

这篇关于Linux,在首次读取或首次写入时将页面pagefault置零吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆