Reasonable valid start address for mmap address hint so as to be gauranteed that things work

99封情书 提交于 2019-12-11 10:42:16

问题


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.

Best, Subramanian


回答1:


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.

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:

  • 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.



来源:https://stackoverflow.com/questions/10112877/reasonable-valid-start-address-for-mmap-address-hint-so-as-to-be-gauranteed-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!