简体   繁体   English

保留没有文件支持的共享内存 (Linux/Windows) (boost::interprocess)

[英]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:所以最好,这样做的方法应该具有以下属性:

  1. Doesn't commit the whole range.不提交整个范围。 I will choose which parts to commit (actually use.) (But the pattern is quite unpredictable.)我会选择提交哪些部分(实际使用。)(但模式非常难以预测。)
  2. Doesn't need a memory-mapped file or anything like that.不需要内存映射文件或类似的东西。 I don't need to preserve the data.我不需要保存数据。
  3. Lets me access the memory area from multiple processes (I'll handle the locking explicitly.)让我从多个进程访问内存区域(我将明确处理锁定。)
  4. Works in both Linux and Windows (obviously a 64-bit OS is needed.)适用于 Linux 和 Windows(显然需要 64 位操作系统。)
  5. Actually uses shared memory.实际上使用共享内存。 I need the performance.我需要表演。
  6. ( NEW ) The OS or the library doesn't try to initialize the reserved region (to zero or whatever.) This is obviously impractical and unnecessary. )操作系统或库不会尝试初始化保留区域(为零或其他)。这显然是不切实际和不必要的。

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:我认为这对您的情况有效,原因有两个:

  1. On Windows, only the shared memory that you actually touch will become resident.在 Windows 上,只有您实际接触的共享内存才会成为常驻内存。 This meets the first requirement.这满足第一个要求。
  2. Because the file is sparse, only the parts that have been modified will actually be stored on disk.因为文件是稀疏的,只有修改过的部分才会真正存储在磁盘上。 If you looked at the properties of the file, it would say something like, "Size: 500 MB, Size on disk: 32 KB".如果您查看文件的属性,它会说“大小:500 MB,磁盘大小:32 KB”。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM