简体   繁体   中英

Shared memory on Windows that can be accessed (read and written) by separate processes

Can I use shared memory on Windows in order to have a common memory region that can be used by multiple separate processes?

Context:

Porting a unix application to Windows which has: - a 'setup' process that creates a number of shared memory regions. - a set of other processes (that run at times when the setup process has already finished) that read (and occasionally write) the memory regions that have been prepared by the setup process


I have already experimented with CreateFileMapping(INVALID_HANDLE_VALUE, ...)/OpenFileMapping but the shared memory seems to be freed as soon as the creating process exits.

Should I create real (ie on file system) files and open these instead of using INVALID_HANDLE_VALUE ?

Windows doesn't support creating a global file mapping object that exists without any processes holding handles to it, which explains why your use of CreateFileMapping with anonymous sections didn't work.

However, as you suggest you can create an actual file on disk, and map that using CreateFileMapping as needed, and processes can share memory that is backed by the disk file. Your processes will just need to know where to look to find the backing file.

Boost有一个可用于共享内存的库: Boost.Interprocess

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