简体   繁体   中英

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

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM