MMAP为/ dev /零 [英] mmap with /dev/zero

查看:166
本文介绍了MMAP为/ dev /零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我使用的/ dev / zero的如下分配与MMAP大的内存(40MB)。

Say I allocate a big memory (40MB) with mmap using /dev/zero as follows.

fd = open("/dev/zero", O_RDWR); 
a = mmap (0, 4096e4, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);

我不解的是,内核将初始化回忆到零页面被带入物理内存(我假设现代Linux内核使用的按需分页的)。因此,例如,当第一页被触摸,因此带入物理内存,内核将其所有的4096字节初始化为零,然后,当第二页被触摸时,它同样的等等。

What I understand is that the kernel will initialize memories to zero as the pages are brought into the physical memory (I suppose the modern Linux kernels use Demand paging). So for example, when the first page is touched and therefore brought into the physical memory, kernel will initialize all of its 4096 bytes to zero, then when the second page is touched, it does the same and so on.

我的理解是否正确?

推荐答案

是的,甚至更多的页面只有当你触摸它们首次带入存在。

Yes, even more the pages are only brought into existence if you touch them for the first time.

BTW,有标志 MAP_ANONYMOUS ,你可以在这种情况下使用,无需打开文件描述符的/ dev / null的

BTW, there is the flag MAP_ANONYMOUS that you can use in this situation, no need to open a file descriptor on /dev/null.

这篇关于MMAP为/ dev /零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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