共有メモリをファイルでバックアップすることは重要ですか?そうでない場合は、基盤となるUnix共有メモリAPI(shmget、shmat、shmdt、およびshmctl)の使用を検討してください。これらはすべてsys/shm.hで宣言されています。私はそれらが非常に使いやすいことを発見しました。
// 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();
}
別のプロセスでは、次のようなものを使用して共有メモリにアクセスします。
// 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();
}
次に、すべてのプロセスが共有メモリで完了したら、を使用shmctl(id, IPC_RMID, 0)
して共有メモリをシステムに解放します。
コマンドラインでipcsおよびipcrmツールを使用して、共有メモリを管理できます。これらは、共有メモリコードを最初に作成するときの間違いをクリーンアップするのに役立ちます。
そうは言っても、32ビットプログラムと64ビットプログラムの間でメモリを共有することについてはよくわかりません。Unix APIを試すことをお勧めしますが、失敗した場合はおそらく実行できません。結局のところ、これらはBoostがその実装で使用するものです。