当内存映射文件修改时通知/信号

时间:2021-09-23 16:59:25

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

我目前正在通过内存映射在多个进程之间共享数据(<1KB)。1“writer”进程和多个“reader”进程处理的都是同一个文件。

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.

目前,阅读器进程需要不断地检查更新。读取器进程一直轮询mmap-ed区域,以查看是否写入了任何新数据。

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:

简而言之,系统试图模仿msgsnd()和msgrcv()的语义,并附加两个注意事项:

  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.

    支持多个“读者”当“作者”发布一个msg,应该发送多个通知,每个活跃的“读者”一个。——>目前已经实现,因为每个“阅读器”不断地轮询“head”字段,并在新数据发生变化时读取它们。

  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.

    如果任何“读取器”/“写入器”进程突然终止,恢复系统应该像重新启动进程一样简单。——>目前实现,因为IPC共享数据是在磁盘上支持的mmap-ed文件中维护的。

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?

什么是-快速(低延迟)和-轻量(低cpu使用率)替代实现某种事件机制以在每次mmap-ed区域被写入进程修改时通知读取器进程?

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

注意:在读取器进程中,当写入器进程更新mmap-ed内存时(甚至在调用msync()之后),在mmap-ed文件上添加inotify监视不会导致任何事件。

2 个解决方案

#1


0  

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

艰难的问题……这些就是我所拥有的……

#2


0  

(Posting the practically used solution for future reference)

(张贴实用的解决方案供日后参考)

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

使用这个将mmap-ed文件支持添加到inotify的补丁,

  • reader processes can use the inotify framework
  • 读者流程可以使用inotify框架。
  • to monitor for changes on an mmap-ed file at significant/important intervals
  • 以重要/重要的间隔监视mmap-ed文件的更改
  • that are triggerred by the writer process calling sync() on the same mmap-ed file.
  • 由写入器进程在相同的mmap-ed文件上调用sync()触发。

#1


0  

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

艰难的问题……这些就是我所拥有的……

#2


0  

(Posting the practically used solution for future reference)

(张贴实用的解决方案供日后参考)

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

使用这个将mmap-ed文件支持添加到inotify的补丁,

  • reader processes can use the inotify framework
  • 读者流程可以使用inotify框架。
  • to monitor for changes on an mmap-ed file at significant/important intervals
  • 以重要/重要的间隔监视mmap-ed文件的更改
  • that are triggerred by the writer process calling sync() on the same mmap-ed file.
  • 由写入器进程在相同的mmap-ed文件上调用sync()触发。