0

I am working on a watchdog for a multiprocess system on a linux-based OS that will simply check if a segment of shared memory has been written within a configurable timeout. Ideally, the watchdog will not have to know the layout or contents of the shared memory segment, only the segment id or virtual filepath.

I have tried regularly executing stat() on the virtual file, but it appears that st_mtime is not updated when the memory-mapped segment is written to, only when the file is created, see here for more information.

The best I have come up with involves executing a hash function on the virtual memory file of the segment. The hash changes regularly with each write to the segment. However, this is somewhat cumbersome and expensive. While I could use some internal signaling from the main process, I'd prefer to keep the watchdog focused on whether or not new data has been added to the shared memory segment. Is there a better way to determine the last modified time of a shared memory segment? Or, that it has been modified within the last N seconds?

I primarily use python but C++ would also be accepted if some required syscalls cannot be made from python.

Young_Maker
  • 293
  • 4
  • 10
  • 1
    sounds like you simply need more granular timekeeping than the filesystem keeps track of by default. best to just signal from the main process imao. could be as simple as a separate shared memory segment holding a single value (the timestamp of last modification) – Aaron Apr 11 '23 at 15:12
  • 2
    You could just write the current time to some location in shared memory on a periodic basis. If that time ever differs by more than N seconds from the current time, your process may be stuck/crashed. – tadman Apr 11 '23 at 15:16
  • You could use the (Linux-specific) inotify interface to get updates on file access and changes. – Something Something Apr 11 '23 at 15:23
  • @Aaron the OS doesn't keep track of st_mtime at all it, it seems to not update after the shmem shegment is opened. I need ~2 minutes of granularity, which I don't feel is _that_ fine – Young_Maker Apr 11 '23 at 15:42
  • I mean more in terms of granularity like what actions cause st_mtime to be updated. on mormal files, it's a little more clear that mtime should be updated any time msync is called, but that never happens with tempfs in memory. – Aaron Apr 11 '23 at 18:24
  • If you have control of the writing process, you could just have the writing process increment a counter in the shared memory region along with any other write it does; then all the watchdog has to do is keep track of how long it has been since it has last seen that counter's value change. – Jeremy Friesner Apr 11 '23 at 21:10
  • @JeremyFriesner lets just say the writing process is done by another team, and they're not exactly forthcoming about the structure. – Young_Maker Apr 12 '23 at 14:42

0 Answers0