[英]Reserving Shared Memory with No File Backing (Linux/Windows) (boost::interprocess)
How can I reserve and allocate shared memory without the backing of a file?如何在没有文件支持的情况下保留和分配共享内存? I'm trying to reserve a large (many tens of GiBs) chunk of shared memory and use it in multiple processes as a form of IPC.我正在尝试保留一大块(数十 GiB)共享内存,并将其作为 IPC 的一种形式在多个进程中使用。 However, most of this chunk won't be touched at all (the access will be really sparse; maybe a few hundred megabytes throughout the life of the processes) and I don't care about the data when the applications end.然而,这个块的大部分根本不会被触及(访问将非常稀疏;在进程的整个生命周期中可能只有几百兆字节)并且我不关心应用程序结束时的数据。
So preferably, the method to do this should have the following properties:所以最好,这样做的方法应该具有以下属性:
I've been experimenting with boost::interprocess::shared_memory_object, but that causes a large file to be created on the filesystem (with the same size as my mapped memory region.) It does remove the file afterwards, but that hardly helps.我一直在尝试使用 boost::interprocess::shared_memory_object,但这会导致在文件系统上创建一个大文件(与我映射的内存区域大小相同)。之后它确实删除了文件,但这几乎没有帮助。
Any help/advice/pointers/reference is appreciated.任何帮助/建议/指针/参考表示赞赏。
PS I do know how to do this on Windows using the native API. PS 我知道如何使用本机 API 在 Windows 上执行此操作。 And POSIX seems to have the same functionality (only with a cleaner interface!) I'm looking for a cross-platform way here. POSIX 似乎具有相同的功能(只有更简洁的界面!)我在这里寻找跨平台的方式。
UPDATE: I did a bit of digging, and it turns out that the support that I thought existed in Windows was only a special case of memory-mapped files, using the system page file as a backing.更新:我做了一些挖掘,结果证明我认为在 Windows 中存在的支持只是内存映射文件的一种特殊情况,使用系统页面文件作为支持。 (I'd never noticed it before because I had used at most a few megabytes of shared memory in the past projects.) (我以前从未注意到它,因为我在过去的项目中最多使用了几兆字节的共享内存。)
Also, I have a new requirement now (the number 6 above.)另外,我现在有一个新要求(上面的数字 6。)
On Windows, all memory has to be backed by the disk one way or another.在 Windows 上,所有内存都必须以某种方式由磁盘支持。
The closest I think you can accomplish on windows would be to memory map a sparse file.我认为您可以在 Windows 上完成的最接近的是内存映射稀疏文件。 I think this will work in your case for two reasons:我认为这对您的情况有效,原因有两个:
I realize that this technically doesn't meet your 2nd requirement, but, unfortunately, I don't think that is possible on Windows.我意识到这在技术上不符合您的第二个要求,但不幸的是,我认为这在 Windows 上是不可能的。 At least with this approach, only the regions you actually use will take up space.至少使用这种方法,只有您实际使用的区域才会占用空间。 (And only when Windows decides to commit the memory to disk, which it may or may not do at its discretion.) (并且仅当 Windows 决定将内存提交到磁盘时,它可能会或可能不会自行决定这样做。)
As for turning this into a cross-platform solution, one option would be to modify boost::interprocess so that it creates sparse files on Windows.至于将其转变为跨平台解决方案,一种选择是修改 boost::interprocess 以便它在 Windows 上创建稀疏文件。 I believe boost::interprocess already meets your requirements on Linux, where POSIX shared memory is available.我相信 boost::interprocess 已经满足您在 Linux 上的要求,其中 POSIX 共享内存可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.