开发者

When would one use Un-named shared memory?

When would you choose to use un-named shared memory in windows?

it seems to me that message passing between threads is not very useful. One can instead pass a pointer to a struct/variable to the worker threads, and use that as shared 开发者_如何学Cmemory instead, rather than calling the CreateFileMapping system call.


One reason to use unnamed shared memory is to restrict access to the file mapping to only those processes who are given a handle to it by the creating process. This avoid two problems:

  • any process that knows the name and has sufficient access to create a mapped file can squat on your named object, preventing or interfering with its legitimate use - this allows a denial of service attack.
  • accidental rather than malicious name clashes.

When you don't use a name, you can be sure that only processes that you want to have access, get it. From the MSDN docs for CreateFileMapping:

A single file mapping object can be shared by multiple processes through inheriting the handle at process creation, duplicating the handle, or opening the file mapping object by name.


Section objects (aka "file mapping objects") are not just used to share memory between processes. The most obvious use of section objects is to map in a file to do I/O, and giving the objects names wouldn't be very useful in most cases. For unnamed pagefile-backed sections ("shared memory") you can still make child processes inherit the handle so they can use the sections.


You can pass handles to unnamed objects across process boundaries. That is to say, you can actually create an unnamed memory map in your application, and access it from another without using a name!

Look at the DuplicateHandle call, which can be used to pass handles to unnamed objects across process boundaries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜