简体   繁体   English

mmap()拥有内存块

[英]mmap() owning memory block

I have a call to mmap() which I try to map 64MB using MAP_ANONYMOUS as follows: 我有一个mmap()调用,我尝试使用MAP_ANONYMOUS映射64MB,如下所示:

void *block = mmap(0, 67108864, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (block == MAP_FAILED)
    exit(1);

I understand that to actually own the memory, I need to hit that block of memory. 我知道要真正拥有内存,我需要打入那块内存。 I want to add some sort of 0's or empty strings to actually own the memory. 我想添加某种0或空字符串来实际拥有内存。 How would I do that? 我该怎么做? I tried the following, but that obviously segfaults (I know why it does): 我尝试了以下操作,但是显然存在段错误(我知道为什么会这样):

char *temp = block;
for (int i = 0; i < 67108864; i++) {
    *temp = '0';
    temp++;
}

How would I actually gain ownership of that block by assigning something in that block? 通过在该区块中分配某些内容,我实际上将如何获得该区块的所有权?

Thanks! 谢谢!

Your process already owns the memory, but what I think you want is to make it resident. 您的进程已经拥有该内存,但是我认为您想要的是使其驻留。 That is, you want the kernel to allocate physical memory for the mmap ed region. 也就是说,您希望内核为mmap ed区域分配物理内存。

The kernel allocates a virtual memory area (VMA) for the process, but this just specifies a valid region and doesn't actually allocate physical pages (or frames as they are also sometimes called). 内核为该进程分配了一个虚拟内存区域(VMA),但这仅指定了一个有效区域,实际上并未分配物理页面(或有时称为帧的帧)。 To make the kernel allocate entries in the page table, all you need to do is force a page fault. 要使内核在页面表中分配条目,您要做的就是强制页面错误。

The easiest way to force a page fault is to touch the memory just like you're doing. 强制页面错误的最简单方法是像执行操作一样触摸内存。 Though, because your page size is almost certainly 4096 bytes, you really only need to read one byte every 4096 bytes thereby reducing the amount of work you actually need to do. 但是,由于页面大小几乎可以确定为4096字节,因此您实际上只需要每4096字节读取一个字节,从而减少了实际需要做的工作量。

Finally, because you are setting the pages PROT_READ , you will actually want to read from each page rather than try to write. 最后,由于要设置页面PROT_READ ,因此实际上您将要从每个页面读取而不是尝试写入。

Your question is not very well formulated. 您提出的问题不是很好。 I don't understand why you think the process is not owning its memory obtained thru mmap ? 我不明白为什么您认为进程不拥有通过mmap获得的内存

Your newly mmap -ed memory zone has only PROT_READ (so you can just read the zeros inside) and you need that to be PROT_READ|PROT_WRITE to be able to write inside. 您新的由mmap内存区域只有PROT_READ (因此您只能读取内部的零),并且您需要将其设置为PROT_READ|PROT_WRITE才能写入内部。

But your process already "owns" the memory once the mmap returned. 但是,一旦mmap返回,您的进程已经“拥有”了内存。

If the process has pid 1234, you could sequentially read (perhaps with cat /proc/1234/maps in a different terminal) its memory map thru /proc/1234/maps ; 如果该过程以PID 1234,则可以顺序地读取(或许与cat /proc/1234/maps在不同的终端)其内存映射通/proc/1234/maps ; from inside your process, use /proc/self/maps . 从您的进程内部,使用/proc/self/maps

Maybe you are interested in memory overcommit ; 也许您对内存过量使用感兴趣; there is a way to disable that. 有一种方法可以禁用它。

Perhaps the mincore(2) , msync(2) , mlock(2) syscalls are interesting you. mincore(2)msync(2)mlock(2)系统调用可能会让您感兴趣。

Maybe you want the MAP_POPULATE or MAP_LOCKED flag of mmap(2) 也许您想要mmap(2)MAP_POPULATEMAP_LOCKED标志

I actually don't understand why you say "own the memory" in your question, which I don't understand very well. 我实际上不明白您为什么在问题中说“拥有记忆”,对此我不太了解。 If you just want to disable memory overcommit, please tell. 如果只想禁用内存过量使用,请告知。

And you might also mmap some file segment. 您也可以mmap一些文件段。 I believe there is no possible overcommit in that case. 我认为在这种情况下不会过度使用。 But I would just suggest to disable memory overcommit in your entire system, thru /proc/sys/vm/overcommit_memory . 但是我只建议通过/proc/sys/vm/overcommit_memory禁用整个系统中的内存/proc/sys/vm/overcommit_memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM