简体   繁体   中英

How to use shared memory correctly under windows

I'm kind of new to shared memory and i was searching for a working example, i managed to find only over MSDN

On the first process i've declared my shared memory as following:

hFileMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, dwDataSize, strSharedMemoryName.c_str());
pBuffer = ::MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, dwDataSize);
::CopyMemory(pBuffer, pData, dwDataSize);

and on the second process:

HANDLE hFileMap = ::OpenFileMapping(FILE_MAP_READ, FALSE, strContentsSizeFileMap.c_str());
LPVOID pData = ::MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);

I know that once I'm done working on the mapView, i need to release it using ' UnmapViewOfFile() '. My question is where exactly?

  1. Parent Process?
  2. Child Process
  3. Both?

If it's on both, does the OS keep some reference count on the address before it's fully released?

From MSDN:

It also decrements the share count of the corresponding physical page

which got me a little bit confused regarding what im actually supposed to do.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx

CreateFileMapping function
Creates or opens a named or unnamed file mapping object for a specified file.
...
Creating a file mapping object does not actually map the view into a process address space. The MapViewOfFile and MapViewOfFileEx functions map a view of a file into a process address space.
...
Mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released. Therefore, to fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile and close the file mapping object handle by calling CloseHandle . These functions can be called in any order.

So Process #1 creates a map object in the operating system . Then process #1 maps some part of that into it's own process memory.
Process #2 gets a handle to the same operating system mapping object, and maps the same part or a different part of that into it's own memory.
When either process is finished, they call UnmapViewOfFile which removes the mapping from it's own process memory, and they call CloseHandle on their handle to the operating system map. Windows handles are all effectively reference-counted, so when all processes that have a handle call CloseHandle , then the operating system automatically destroys the map object.

Note that this means if process #1 creates the mapping, uses the mapping, then closes it entirely, and then process #2 tries to open that mapping, process #2 will fail because the operating system deleted it when no processes had any handles. To work around this, create files in the filesystem to back your memory, which allows the memory to stick around between processes, until you delete the file.

Every process that calls MapViewOfFile should call UnmapViewOfFile when it is finished using the shared memory. That would typically be when the program is shutting down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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