Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

c - Notify/Signal when memory mapped file modified

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()).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...