views:

267

answers:

2

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.

+1  A: 

The shared memory mapping would not persist across reboots and it will disappear when all of its handles are closed. A memory mapping object is a kernel object - they always get deleted when the last reference to them goes away, either explicitly via a CloseHandle or when the process containing the reference exits.

Try creating a registry key with RegCreateKeyEx with REG_OPTION_VOLATILE - the data will not preserved when the corresponding hive is unloaded. This will be at system shutdown for HKLM or user logoff for HKCU.

Michael
Thanks for confirming that my current approach is flawed; I was coming to that conclusion but put it to test yet. `REG_OPTION_VOLATILE` in `HKLM` looks like a good fit, I just have to work around some in-house registry wrappers.
ephemient
A: 

sounds like maybe you want serialization instead of shared memory? If that is indeed appropriate for your application, the way you serialize will depend on your language. If you're using c++, check out boost::serialize. C# undoubtedly has lots of serializations options (like java), if that's what you're using.

Mike Ellery
Serialization is not the problem; the problem is getting the lifetime of the serialized data exactly right.
ephemient