Is it important that shared memory is supported by the file? Otherwise, you can use the basic Unix shared memory APIs: shmget, shmat, shmdt, and shmctl, all declared in sys / shm.h. I found them very easy to use.
// create some shared memory int id = shmget(0x12345678, 1024 * 1024, IPC_CREAT | 0666); if (id >= 0) { void* p = shmat(id, 0, 0); if (p != (void*)-1) { initialize_shared_memory(p); // detach from the shared memory when we are done; // it will still exist, waiting for another process to access it shmdt(p); } else { handle_error(); } } else { handle_error(); }
Another process will use something like this to access shared memory:
// access the shared memory int id = shmget(0x12345678, 0, 0); if (id >= 0) { // find out how big it is struct shmid_ds info = { { 0 } }; if (shmctl(id, IPC_STAT, &info) == 0) printf("%d bytes of shared memory\n", (int)info.shm_segsz); else handle_error(); // get its address void* p = shmat(id, 0, 0); if (p != (void*)-1) { do_something(p); // detach from the shared memory; it still exists, but we can't get to it shmdt(p); } else { handle_error(); } } else { handle_error(); }
Then, when all processes are running with shared memory, use shmctl(id, IPC_RMID, 0)
to release it back to the system.
You can use the ipcs and ipcrm tools on the command line to manage shared memory. They are useful for troubleshooting the first time you write shared memory code.
All that has been said, I'm not sure about the sharing of memory between 32-bit and 64-bit programs. I recommend trying Unix APIs, and if they fail, this is probably not possible. After all, this is what Boost uses in its implementation.