dev shm equivalence in Windows – What to Use and How
In this article
On Windows the closest counterpart to Linux’s /dev/shm is the named file‑mapping (shared memory) API – CreateFileMapping, OpenFileMapping and MapViewOfFile – which provides RAM‑backed, cross‑process memory just like the tmpfs‑based /dev/shm directory.
Background: what /dev/shm actually is
/dev/shm is a special mount point created by the Linux kernel (usually a tmpfs filesystem) that lives under /dev. It is used by the POSIX shared‑memory functions shm_open and shm_unlink as well as by applications that simply create regular files in that directory for fast, RAM‑backed I/O. Because it is a RAM‑backed filesystem, data stored there disappears on reboot and does not touch persistent storage.
The name comes from the original Unix convention of placing device nodes under /dev. When Linux added POSIX shared memory in the 2.4 kernel, the developers chose /dev/shm as a convenient, file‑system‑like namespace.
How Windows implements shared memory
Windows does not expose a filesystem path for RAM‑backed shared memory. Instead it uses kernel objects called *section objects* that are created with the CreateFileMapping function. When the first parameter is INVALID_HANDLE_VALUE, the mapping is backed by the system paging file (i.e., pure RAM) and behaves like a /dev/shm object.
Typical usage pattern:
- Call
CreateFileMapping(INVALID_HANDLE_VALUE, …, "MySharedMemory", size)to create a named mapping. - In the same or another process, call
OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "MySharedMemory")to obtain a handle. - Map the memory into the process address space with
MapViewOfFile(orMapViewOfFileExfor a specific address). - When finished, unmap with
UnmapViewOfFileand close the handle withCloseHandle.
The name you give (e.g., "MySharedMemory") is a string in the Windows Object Manager namespace. By default the name is placed in the Local\ namespace, which is visible only to processes in the same session. To share across sessions (e.g., services and user apps) prefix the name with Global\.
Choosing the right API for a given scenario
- Pure RAM, no file backing: Use
CreateFileMapping(INVALID_HANDLE_VALUE, …). This is the direct analogue of a/dev/shmentry. - Backed by a real file on disk: Open the file with
CreateFilethen pass the handle toCreateFileMapping. This mirrors using a regular file inside/dev/shmfor persistence across reboots. - Cross‑session communication: Prefix the mapping name with
Global\and ensure the security descriptor grants access to the intended users. - Large data that may exceed RAM: Use a file‑backed mapping so the system can page data to disk automatically.
Common problems and how to solve them
Even though the API is straightforward, developers often run into a handful of pitfalls:
- Name collisions: Two unrelated programs may choose the same mapping name. To avoid this, include a GUID or the executable name, e.g.,
Global\MyApp_9F2A3C. - Insufficient permissions: When using
Global\mappings, the default security descriptor may deny access to normal users. Create aSECURITY_ATTRIBUTESstructure with a permissive DACL or useConvertStringSecurityDescriptorToSecurityDescriptorto set explicit rights. - Size mismatches: The size passed to
CreateFileMappingmust be a multiple of the system allocation granularity (usually 64 KB). UseGetSystemInfoto retrievedwAllocationGranularityand round up. - Forgotten cleanup: If a process crashes without calling
CloseHandle, the mapping persists until the last handle is closed. Use a named mutex or a watchdog process to detect stale mappings. - 32‑bit vs 64‑bit address space: A 32‑bit process cannot map a region larger than its virtual address space. Split large data into multiple mappings or switch to a 64‑bit build.
Comparison: Linux /dev/shm vs Windows named file mapping
| Aspect | Linux /dev/shm | Windows named mapping |
|---|---|---|
| Namespace | Filesystem path under /dev/shm | Object Manager namespace (Local\ or Global\) |
| Creation call | shm_open() or regular open() | CreateFileMapping() |
| Backing store | tmpfs (RAM, swapped to swap) | Paging file (RAM, swapped automatically) |
| Automatic cleanup | Unlinked files disappear on reboot; shm_unlink() removes entry. | Mappings disappear when last handle is closed; no automatic unlink. |
| Security model | POSIX permissions on the directory and file. | Security descriptor attached to the object. |
Functionally the two mechanisms provide the same capability: fast, shared memory that does not touch permanent storage. The main differences lie in how names are expressed and how cleanup is handled.
Alternative approaches on Windows
- Memory‑mapped files on disk: Use
CreateFile+CreateFileMappingwith a real file path. This works like creating a file in/dev/shmbut persists across reboots. - Named pipes: Useful for stream‑oriented communication; less efficient for random access but simpler for message passing.
- Windows Sockets (AF_INET): Works across machines and provides robust error handling, but incurs network stack overhead.
- Boost.Interprocess library: Provides a cross‑platform abstraction that hides the platform‑specific details of POSIX shm and Windows file mappings.
Frequently Asked Questions
Is there a /dev/shm directory on Windows?
No. Windows does not expose a RAM‑backed filesystem under /dev. The functional equivalent is a named file‑mapping object created with CreateFileMapping.
Can I use the same code that opens /dev/shm on Linux to run on Windows?
Not directly. Linux code that calls shm_open will not compile on Windows. You must replace those calls with the Windows API equivalents (CreateFileMapping, OpenFileMapping, MapViewOfFile) or use a portable library such as Boost.Interprocess.
Do I need administrator rights to create a global named mapping?
Creating a mapping in the Global\ namespace does not require admin rights, but the default security descriptor may restrict access. If non‑admin processes need to open the mapping, you must supply a custom security descriptor that grants the appropriate permissions.
How do I clean up a named mapping that was left behind after a crash?
When the last handle to the mapping is closed, the kernel automatically destroys the object. If a process crashes without closing its handle, the mapping persists until the process terminates. You can detect and remove stale mappings by attempting to open them with OpenFileMapping and, if they exist but are not in use, calling CloseHandle from a cleanup utility.
Is the size of a Windows shared memory segment limited?
The size is limited by the available virtual address space and the system allocation granularity. On a 64‑bit system the limit is effectively the amount of free RAM plus swap, while on a 32‑bit process the practical limit is around 2 GB (often less due to address‑space fragmentation).
Can I use the Windows shared‑memory API from .NET?
Yes. .NET provides MemoryMappedFile.CreateOrOpen and related classes that wrap the native API. The underlying mechanism is still the same named file‑mapping object.