Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Is it important that the shared memory be backed by a file? If not, you might consider using the underlying Unix shared memory APIs: shmget, shmat, shmdt, and shmctl, all declared in sys/shm.h. I have found them to be very easy to use.</p> <pre><code>// create some shared memory int id = shmget(0x12345678, 1024 * 1024, IPC_CREAT | 0666); if (id &gt;= 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(); } </code></pre> <p>Another process would use something like this to access the shared memory:</p> <pre><code>// access the shared memory int id = shmget(0x12345678, 0, 0); if (id &gt;= 0) { // find out how big it is struct shmid_ds info = { { 0 } }; if (shmctl(id, IPC_STAT, &amp;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(); } </code></pre> <p>Then, when all processes are done with the shared memory, use <code>shmctl(id, IPC_RMID, 0)</code> to release it back to the system.</p> <p>You can use the ipcs and ipcrm tools on the command line to manage shared memory. They are useful for cleaning up mistakes when first writing shared memory code.</p> <p>All that being said, I am not sure about sharing memory between 32-bit and 64-bit programs. I recommend trying the Unix APIs and if they fail, it probably cannot be done. They are, after all, what Boost uses in its implementation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload