mmap 与/dev/zero [英] mmap with /dev/zero

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

问题描述

假设我使用/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.

顺便说一句,在这种情况下,您可以使用标志 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/zero的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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