Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would one use Un-named shared memory?

Tags:

c++

windows

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 memory instead, rather than calling the CreateFileMapping system call.

like image 430
aCuria Avatar asked Jan 26 '26 05:01

aCuria


2 Answers

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.

like image 137
Steve Townsend Avatar answered Jan 27 '26 19:01

Steve Townsend


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.

like image 42
wj32 Avatar answered Jan 27 '26 18:01

wj32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!