Notify/Signal when memory mapped file modified

徘徊边缘 提交于 2020-02-02 00:33:58

问题


I am currently sharing data (<1KB) between multiple processes by memory-mapping. 1 "writer" process and multiple "reader" processes all mmap the same file.

Currently the reader processes need to constantly keep checking for updates. The reader processes keep polling the mmap-ed region to see if any new data is written.

Typical Usage (and existing implementation):
The "Writer" process is a logger which keeps appending new data (each on a new line) at irregular intervals. At any given point of time there can be one or more "reader" processes that are interested in any new data that the "writer" process generates. Also rather than having an indefinitely extending file, its is a circular buffer i.e. after a fixed number of lines the writer loops-back and start overwriting the file from the beginning with the new data. A header field in this file keeps track of position of the latest data i.e. the current "head".

In short the systems attempts to mimic the semantics of msgsnd() & msgrcv() with two additional caveats:

  1. Support multiple "readers"
    When "writer" posts a single msg, multiple notifications should be sent, 1 for each active "reader".
    --> Currently achieved as each "reader" constantly polls the "head" field and reads the new data when it changes.

  2. Persistence(file backed)
    If any "reader"/"writer" process is abruptly terminated, recovering the system should be as simple as restarting the process.
    --> Currently achieved as the IPC shared data is maintained in an mmap-ed file backed on the disk.

What would be a
- Fast (low latency) and
- Light-weight (low cpu-usage) alternative to implement some sort of event mechanism to notify the reader processes every time the mmap-ed region is modified by the writer process?

NOTE: In the reader process, adding an inotify watch on the mmap-ed file did NOT result in any events when the mmap-ed memory was updated by the writer process (even after calling msync()).


回答1:


(Posting the practically used solution for future reference)

Using this patch that adds mmap-ed file support to inotify,

  • reader processes can use the inotify framework
  • to monitor for changes on an mmap-ed file at significant/important intervals
  • that are triggerred by the writer process calling sync() on the same mmap-ed file.



回答2:


In all of the blocking communications, the reader grabs the data and no other reader can read it.

  • You can create a chain of readers. This is bad, because in your case, processes might end abruptly
  • Use multiple pipes. Each time a reader is created, it can ask the writer to open a pipe, and then the reader could subscribe to that pipe and read it. Also, the writer can send the reader a copy of the current state of data, unless readers can access it on its own.
  • Use signals. You could send a signal from the writer to all the readers after each write.

Tough question though... These are all I got...



来源:https://stackoverflow.com/questions/17123108/notify-signal-when-memory-mapped-file-modified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!