问题
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