I am not familiar with mapped files, but I have found that there is a MappedFile object in the GLib C library (not glibc!):
The v1.2.0 version of the fmmap library is available.
It has been successfully used in production on Linux (works on Windows too, but it is has never been used in production).
Interesting. Long ago on UNICOS systems we used to get somewhat related functionality by using the Cray facilities to map a direct access file to memory; and that has persisted to work with slight variations. Locking has occasionally needed resolved when sharing the data between processes but basically /dev/shm is now used for the file residency and it still works well enough that even though we keep saying “we should modernize and rewrite this” we never do. I will give this a try. Thanks for the effort!
Sharing memory between processes was not the purpose of this code, and I’m not sure if it can be used for that, and if yes, how (and how efficiently…)
Edit: opening the same physical with FMMAP_NEW is a process and FMMAP_OLD in a forked process does the job (i.e. what is written by one of the processes can be read by the other process), but 1) I’m not sure the memory is actually shared (edit: loc() returns the same address in both processes, but does it prove something?) and 2) I wonder about the performances since the data have to be written on disk at some point…
OK, after thinking a bit about this…
If two independent processes A and B map the same physical file, they share the same consistent cache in RAM for this file (the OS guarantees that), so if process A writes a value in the array that points to the file, process B can read it from the cache right after completion of the assign statement in A (*), even if it has not been yet written to disk. If the file has a small or moderate size it can live entirely in cache, thus without a significant performance hit. This is a basic form of IPC.
I have also read that on posix systems anonymous mapping can achieve the same IPC without any physical file (thus without any performance hit for sure), but this is restricted to processes that are forked (process B forked from process A). I don’t know on Windows.
For the general case, I guess that the /shm features are more suited to IPC…
(*) that’s maybe not entirely true, as the compiler can decide to keep a variable in a register instead of re-fetching it from memory, thus potentially missing a new value written by another process. It is a case where the volatile attribute can help ?