[英]How do I mmap a _particular_ region in memory?
I have a program. 我有一个程序。 I want it to be able to mmap a particular region of memory over different runs.
我希望它能够通过不同的运行来映射内存的特定区域。
I just want to have this particular region of memory, say 0xabcdabcd to 0xdeadbeef that I mmap to a particular file. 我只想拥有这个特定的内存区域,比如说我映射到特定文件的0xabcdabcd到0xdeadbeef。 Is there anyway to guarantee this?
反正有保证吗? (I have to somehow make sure that other things aren't loaded into this particular region).
(我必须以某种方式确保没有将其他内容加载到该特定区域中)。
EDIT: 编辑:
How do I make sure that nothing else takes this particular region in memory? 如何确保没有其他需要该特定区域的内存?
You need to do two things: 您需要做两件事:
For the starting address, you need to make sure it's a multiple of the pagesize. 对于起始地址,您需要确保它是页面大小的倍数。 To get the pagesize, use the call
sysconf(_SC_PAGESIZE)
(that's the appropriate call on Linux, other platforms may be different). 要获取页面大小,请使用调用
sysconf(_SC_PAGESIZE)
(在Linux上是适当的调用,其他平台可能有所不同)。
Pass the address to map in addr
. 将地址传递给
addr
map。 Try to get it on a 8KB boundary. 尝试在8KB的边界上获取它。 You could try
mlock()
instead though. 您可以尝试使用
mlock()
代替。
You cannot make sure that nothing else takes that area of memory - first come, first served. 您无法确保没有其他东西占用该内存区域-先到先得。 However, as you need a particular part of the memory, I'm guessing that you have a pretty specialized environment, so you simply need to make sure that you are first (using start scripts)
但是,由于您需要内存的特定部分,所以我猜您有一个非常专业的环境,因此您只需要确保自己是第一名(使用开始脚本)即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.