Active6 years, 8 months ago
Using a named mutex could probably work for me, however, if I need to accept the penalty of a system call for every locking operation, then I would probably resort to simply use file locking (LockFileEx) since in my case I already have the shared memory mapped file open. – Kristian Spangsege Jan 18 '13 at 16:42. Memory-Mapped Files. Memory-mapped files are not new. For over 20 years, the Windows operating system allowed applications to map virtual addresses directly to a file on disk thereby allowing multiple processes to share it. File-based data looked and, more importantly, performed like system virtual memory. There was another benefit. Apr 03, 2015 Memory mapping a file on disk doesn’t make the disk I/O any faster, and disk sectors can be cached in memory as easily as pages can be stored in memory. However, most operating systems do not allow a program to trap its own page faults, so when a page has to be read in from. Memory mapping a file on disk doesn’t make the disk I/O any faster, and disk sectors can be cached in memory as easily as pages can be stored in memory. However, most operating systems do not allow a program to trap its own page faults, so when a page has to be read in from.
On Memory-Mapped Files. When you map a file to memory, you instruct the Windows memory manager to create a data structure that maps a region of virtual memory pages in your process’ address space to a region of a file on disk. More specifically, the memory manager marks the virtual memory region as invalid from the processor’s perspective.
Does Windows offer any kind of mutex that can be placed in a memory mapped file and used across multiple processes?
Ideally it must be completely self contained such that it can survive by itself in the file, even across a reboot.Also, no resources should be leaked if I simply remove the file manually while no processes are running.
If possible the solution should also offer the accompanying 'condition' concept which should also be an object that can sit in a shared memory mapped file.
In short, I need something similar to a PTHREADS mutex with the SHARED attribute.
As far as I understand, simply using a PTHREADS mutex is not possible because the SHARED attribute is unsupported in the Windows port of PTHREADS.
Kristian Spangsege
Kristian SpangsegeKristian Spangsege
3 Answers
To share a synchronization object, give it a name and use the same name in each process when you Create the object.
The following synchronization objects can be shared between process that way :
Critical sections cannot be shared, but are faster.
Testing or waiting on those objects is done with the wait family of functions, often WaitForMultipleObjects.
ixe013ixe013
7,5152 gold badges35 silver badges63 bronze badges
Use the file as its own mutex: Use the
Raymond ChenRaymond ChenLockFileEx
function and have everybody agree to lock byte 0 of the file when they want to claim the mutex.40.6k7 gold badges69 silver badges113 bronze badges
That's not possible. The mutex object itself lives in kernel space to protect it from user code messing with its state. The handle you acquired to it is only valid for the process that acquired it. Technically you could use DuplicateHandle() and put the returned handle in the mmf, but only if you have a handle to the other process that accesses the memory section. That's fairly brittle.
This is why you can specify a name for the mutex in the CreateMutex() function. The other process gets to it by using the same name in the OpenMutex call.
Hans PassantHans Passant815k113 gold badges1399 silver badges2187 bronze badges
Memory Mapped File Python
Not the answer you're looking for? Browse other questions tagged c++windowsmutexmemory-mapped-files or ask your own question.
-->A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory. Starting with the .NET Framework 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files.
There are two types of memory-mapped files:
- Persisted memory-mapped filesPersisted files are memory-mapped files that are associated with a source file on a disk. When the last process has finished working with the file, the data is saved to the source file on the disk. These memory-mapped files are suitable for working with extremely large source files.
- Non-persisted memory-mapped filesNon-persisted files are memory-mapped files that are not associated with a file on a disk. When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. These files are suitable for creating shared memory for inter-process communications (IPC).
Processes, Views, and Managing Memory
Memory-mapped files can be shared across multiple processes. Processes can map to the same memory-mapped file by using a common name that is assigned by the process that created the file.
To work with a memory-mapped file, you must create a view of the entire memory-mapped file or a part of it. You can also create multiple views to the same part of the memory-mapped file, thereby creating concurrent memory. For two views to remain concurrent, they have to be created from the same memory-mapped file.
Multiple views may also be necessary if the file is greater than the size of the application’s logical memory space available for memory mapping (2 GB on a 32-bit computer).
There are two types of views: stream access view and random access view. Use stream access views for sequential access to a file; this is recommended for non-persisted files and IPC. Random access views are preferred for working with persisted files.
Memory-mapped files are accessed through the operating system’s memory manager, so the file is automatically partitioned into a number of pages and accessed as needed. You do not have to handle the memory management yourself.
The following illustration shows how multiple processes can have multiple and overlapping views to the same memory-mapped file at the same time.
The following image shows multiple and overlapped views to a memory-mapped file:
Programming with Memory-Mapped Files
The following table provides a guide for using memory-mapped file objects and their members.
Task | Methods or properties to use |
---|---|
To obtain a MemoryMappedFile object that represents a persisted memory-mapped file from a file on disk. | MemoryMappedFile.CreateFromFile method. |
To obtain a MemoryMappedFile object that represents a non-persisted memory-mapped file (not associated with a file on disk). | MemoryMappedFile.CreateNew method. - or - MemoryMappedFile.CreateOrOpen method. |
To obtain a MemoryMappedFile object of an existing memory-mapped file (either persisted or non-persisted). | MemoryMappedFile.OpenExisting method. |
To obtain a UnmanagedMemoryStream object for a sequentially accessed view to the memory-mapped file. | MemoryMappedFile.CreateViewStream method. |
To obtain a UnmanagedMemoryAccessor object for a random access view to a memory-mapped fie. | MemoryMappedFile.CreateViewAccessor method. |
To obtain a SafeMemoryMappedViewHandle object to use with unmanaged code. | MemoryMappedFile.SafeMemoryMappedFileHandle property. - or - MemoryMappedViewAccessor.SafeMemoryMappedViewHandle property. - or - MemoryMappedViewStream.SafeMemoryMappedViewHandle property. |
To delay allocating memory until a view is created (non-persisted files only). (To determine the current system page size, use the Environment.SystemPageSize property.) | CreateNew method with the MemoryMappedFileOptions.DelayAllocatePages value. - or - CreateOrOpen methods that have a MemoryMappedFileOptions enumeration as a parameter. |
Security
Windows Memory Mapped File Lock
You can apply access rights when you create a memory-mapped file, by using the following methods that take a MemoryMappedFileAccess enumeration as a parameter:
You can specify access rights for opening an existing memory-mapped file by using the OpenExisting methods that take an MemoryMappedFileRights as a parameter.
In addition, you can include a MemoryMappedFileSecurity object that contains predefined access rules.
To apply new or changed access rules to a memory-mapped file, use the SetAccessControl method. To retrieve access or audit rules from an existing file, use the GetAccessControl method.
Examples
Persisted Memory-Mapped Files
The CreateFromFile methods create a memory-mapped file from an existing file on disk.
The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.
The following example opens the same memory-mapped file for another process.
Non-Persisted Memory-Mapped Files
The CreateNew and CreateOrOpen methods create a memory-mapped file that is not mapped to an existing file on disk.
The following example consists of three separate processes (console applications) that write Boolean values to a memory-mapped file. The following sequence of actions occur:
Process A
creates the memory-mapped file and writes a value to it.Process B
opens the memory-mapped file and writes a value to it.Process C
opens the memory-mapped file and writes a value to it.Process A
reads and displays the values from the memory-mapped file.- After
Process A
is finished with the memory-mapped file, the file is immediately reclaimed by garbage collection.
To run this example, do the following:
- Compile the applications and open three Command Prompt windows.
- In the first Command Prompt window, run
Process A
. - In the second Command Prompt window, run
Process B
. - Return to
Process A
and press ENTER. - In the third Command Prompt window, run
Process C
. - Return to
Process A
and press ENTER.
The output of
Process A
is as follows:Process A
Process B
Process C