mmap地址提示的合理有效起始地址,以确保一切正常 [英] Reasonable valid start address for mmap address hint so as to be gauranteed that things work

查看:209
本文介绍了mmap地址提示的合理有效起始地址,以确保一切正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的一项任务中,我们需要在两台机器之间建立一个分布式共享内存,我使用的是基于分页的技术,因此两台机器上的基址是不同的.但是有一个链表测试用例几乎要求两个地址范围都相同.使用MAP_FIXED的mmap的固定地址会导致从属机器崩溃(因为答复服务器线程的堆栈已被覆盖),我认为在两个进程的初始化期间创建一个保证在开始时都是免费的地址是合理的.但是,经过数小时的网上搜索之后,我仍然很遗憾没有找到该默认地址.我们的要求是10000页.如果有人可以指出堆地址的基数,超过该范围不保证sbrk不增长,我可以使用它.

In one of our assignments we are required to build a distributed shared memory between 2 machines, I was using a paging based technique such that the base addresses are different on both the machines. But there is this linked list test case which almost mandates that both address ranges are same. mmap's fixed address using MAP_FIXED causes the slave machine to crash( because the stack of the reply server thread was getting overwritten ), i figured that creating an address that is gauranteed to be free at the very start during initialization on both processes would be reasonable. However, after hours of scouring the net i am still unlucky in finding that default address. Our requirement is 10000 pages. If someone could point the base of heap address beyond which sbrk is gauranteed not to grow i could use that.

最好, Subramanian

Best, Subramanian

推荐答案

我认为任何人都不能保证任何可以一直使用的地址.虚拟地址的选择始终取决于内核,而MAP_FIXED总是要加紧脚步.

I don't think that anyone is going to be able to guarantee any address that will always work. The choice of virtual addresses is always up to the kernel and MAP_FIXED is always going to step on its toes.

我建议不要使用-m32来编译您的应用程序吗?如果您选择了一个随机地址,那么有了那么多可用的地址,发生冲突的可能性就会降低4294967296倍.

Might I suggest not using -m32 to compile your application? With so many more addresses available you'll be 4294967296 times less likely to hit a conflict if you choose a random address.

我建议使用以下其中一种黑客手段:

I would suggest one of the following hacks:

  • 由于您使用的是Linux,因此请阅读并解析/proc/self/maps.找到任意两个映射之间的最大间隙,然后在该间隙的中点选择一个地址.如果先前的映射在以后的运行中向上爬行或下一个映射的情况向下爬行,则应该相当安全.将此地址与其他进程进行通信,以便它可以尝试在同一地址进行映射.希望另一个进程的内存映射足够相似,并且在同一位置或多或少会有这个大缺口.
  • 在没有MAP_FIXED的情况下临时分配大量匿名内存.对于您的固定映射,请尝试紧跟此映射之后的地址.将此地址与其他进程进行通信,以便它可以尝试在同一地址进行映射,然后摆脱临时映射.希望另一个进程的内存映射足够相似,并且您在选择的地址之前已经留有大量的备用地址,这样,如果第二个进程占用了其中一些地址,您仍然可以.
  • Since you are on Linux, read and parse /proc/self/maps. Find the largest gap between any two mappings, and choose an address right at the midpoint of this gap. You should be reasonably safe in case the previous mapping creeps upward or the next mapping creeps downward in subsequent runs. Communicate this address to the other process so that it can try mapping at the same address. Hopefully the other process's memory map will be similar enough and it will have this big gap more or less at the same place.
  • Temporarily allocate a huge amount of anonymous memory without MAP_FIXED. For your fixed mapping, try the address immediately following this mapping. Communicate this address to the other process so that it can try mapping at the same address, and then get rid of the temporary mapping. Hopefully the other process's memory map will be similar enough and you've left yourself a huge amount of spare addresses before your chosen address so that if the second process is occupying some of those you will still be OK.

请记住,这些都是骇客.

And keep in mind that these are hacks.

这篇关于mmap地址提示的合理有效起始地址,以确保一切正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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