My service needs to store a few bits of information (at minimum, at least 20 bits or so, but I can easily make use of more) such that
- it persists across service restarts, even if the service crashed or was otherwise terminated abnormally
- it does not persist across a reboot
- can be read and updated with very little overhead
If I store this information in the registry or in a file, it will not get automatically emptied when the system reboots.
Now, if I were on a modern POSIX system, I would use shm_open
, which would create a shared memory segment which persists across process restarts but not system reboots, and I could use shm_unlink
to clean it up if the persistent data somehow got corrupted.
I found MSDN : Creating Named Shared Memory and started reimplementing pieces of it within my service; this basically uses CreateFileMapping(INVALID_HANDLE_NAME, ..., PAGE_READWRITE, ..., "Global\\my_service")
instead of shm_open("/my_service", O_RDWR, O_CREAT)
.
However, I have a few concerns, especially centered around the lifetime of this pagefile-backed mapping. I haven't found answers to these questions in the MSDN documentation:
- Does the mapping persist across reboots?
- If not, does the mapping disappear when all open handles to it are closed?
- If not, is there a way to remove or clear the mapping? Doesn't need to be while it's in use.
If it does persist across reboots, or does disappear when unreferenced, or is not able to be reset manually, this method is useless to me.
Can you verify or find faults in these points, and/or recommend a different approach?
If there were a directory that were guaranteed to be cleaned out upon reboot, I could save data in a temporary file there, but it still wouldn't be ideal: under certain system loads, we are encountering file open/write failures (rare, under 0.01% of the time, but still happening), and this functionality is to be used in the logging path. I would like not to introduce any more file operations here.