简体   繁体   English

Windows中如何实现共享内存?

[英]How is shared memory implemented in windows?

Where is the memory allocated? 内存分配在哪里? In which address space is the new memory? 新内存在哪个地址空间中?

There are two common types of shared memory in Windows. Windows中有两种常见的共享内存类型。

One is when more than one process maps the same file into memory. 一种是多个进程将同一文件映射到内存中。 This obviously occurs with executables and libraries, but it can also occur with data files. 这显然发生在可执行文件和库中,但也可能发生在数据文件中。 Basically, the fact that processes are sharing this memory has almost no effect on how it works. 基本上,进程共享此内存这一事实几乎对其工作方式没有影响。 It is implemented through the same demand paging as would occur if a single process had it mapped. 它是通过与单个进程进行映射时相同的需求分页来实现的。

The other type is a shared, anonymous mapping. 另一种类型是共享的匿名映射。 These can be created by calling the CreateFileMapping function without specifying a valid file to map. 可以通过调用CreateFileMapping函数来创建这些文件,而无需指定要映射的有效文件。 This acts the same as if the memory was allocated, except that more than one process can access it. 这与分配内存一样,只是有多个进程可以访问它。 Just like normal memory usage, it can be paged to disk if it's not accessed or if RAM is needed for some other purpose. 就像普通的内存使用一样,如果不访问它或出于其他目的需要RAM,则可以将其分页到磁盘。

For shared, anonymous mappings, the kernel gives processes a handle to the memory so that processes have a way to refer to it when they want to map it into their memory space. 对于共享的匿名映射,内核为进程提供了内存的句柄,以便进程在希望将其映射到其内存空间时可以引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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