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:
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.
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()).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…