修改内存映射文件时通知/信号 [英] Notify/Signal when memory mapped file modified

查看:91
本文介绍了修改内存映射文件时通知/信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过内存映射在多个进程之间共享数据(< 1KB). 1个作家"进程和多个读者"进程全部 mmap 相同的文件.

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

简而言之,系统尝试模仿 msgsnd()&的语义. msgrcv(),还有两个警告:

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

  1. 支持多个读者"
    当作者"发布单个消息时,应发送多个通知,每个通知1条 活动的阅读器".
    ->当前每个阅读器"不断轮询头"字段并在更改时读取新数据的目的.

  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.

持久性(文件支持)
如果任何读取器"/写入器"进程突然终止,则恢复系统应该与重新启动该进程一样简单.
->目前通过将IPC共享数据保存在磁盘上的mmap-ed文件中来实现.

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.

会是什么
-快速(低延迟)和
-轻量级(低CPU使用率)的替代方案,用于实现某种事件机制,以便在每次 mmap-ed区域是通过writer过程修改的?

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?

注意:在阅读器过程中,添加 inotify 对mmap-ed文件的监视在编写器进程更新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版本的文件支持,以使该功能生效

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

  • 阅读器 进程可以使用 inotify 框架
  • 以重要/重要的时间间隔监视mmap-ed文件中的更改
  • writer 进程在同一mmap-ed文件上调用sync()触发.
  • 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.

这篇关于修改内存映射文件时通知/信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆