How do I pass virtual address to shmat() function in a guaranteed way

99封情书 提交于 2020-01-14 03:09:10

问题


I am using shmat() by using a virtual address.

  • the first process calls a shmat with shmaddr as null and when it gets the virtual address of the data block it stores in another shared memeory place.

  • the second process calls the shmat() with the virtaul address that was stored in shared memeory by the first process.

  • The second process usually can attach to the same virtual address in most of the cases, but in one case I couldn't and shmat returned -1 and when I used gdb I saw that the address is a Bad address.

    (gdb) x 0x800852000
    0x800852000:    Error accessing memory address 0x800852000: Bad address.
    

So my question is How do I guarantee that the first time I get virtual address that both processes can see?


回答1:


In cases where you are setting the virtual address, its better if you force it to a value that is unlikely to be used normally. You most likely got the bad address because a library or something else took over before you had a chance to attach. We have a similar, situation and we force our address to 0x0000005000000000 (for 64bit systems):

void *stuff = shmat(shmid, 0x0000005000000000, SHM_RDONLY);


来源:https://stackoverflow.com/questions/5188091/how-do-i-pass-virtual-address-to-shmat-function-in-a-guaranteed-way

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