主な問題は、プラットフォームに依存しない方法ではないことです。したがって、あなたが何らかのUNIXシステムを使用していると仮定すると、任意のプロセス間で共有するために、おそらくファイルで使用mmap
します。これの良いところは、任意の数のプロセスで共有できること、共有ポイント (ファイル) を簡単に特定できること、そして実際の永続ストレージを無料で利用できることです (デバッグが容易になります)。データ構造がどれほど複雑であっても、それは単なる記憶の一部です。したがって、解決しなければならない唯一の問題は、プロセス間で書き込みアクセスを同期する方法です。これは実際にはアプリ固有のものです (ただし、複数のプロセスからの書き込みを許可する場合は簡単ではありません)。
サンプルコード:
#include <sys/mman.h>
#include <fcntl.h>
...
struct my_structure *buf; /* just an example - can be arbitrary complex */
...
int fd = open("foo.bin", O_RDWR);
if (fd == -1) { /* if the file doesn't exist create your initial structure */
fd = open("foo.bin", O_RDWR | O_CREAT, 0700);
/* ... allocate enough space in the file or pre-fill with the structure ... */
/* (for safety you may do that in a separate process or using move-in atomically) */
}
buf = (struct my_structure*) mmap(0, sizeof(*buf), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
...
/* memory in buf is now shared across all processes ... */
/* if you want to synchronize that shared memory with the file use msync, but it's not needed for the sharing */
msync(buf, sizeof(*buf), MS_ASYNC);
/* when you're done, unmap */
munmap(buf, sizeof(*buf));