使用内存映射文件,同时允许其他进程完全访问
我正在尝试在Windows下使用内存映射文件(使用CreateFile/CreateFileMapping/MapViewOfFile函数),并且当前在调用CREATE_FILE时指定FILE_SHARE_READ和FILE_SHARE_WRITE。但是,这会锁定文件以防止其他进程使用。
我想要的是在调用 CreateFileMapping 或 MapViewOfFile 时对文件进行内存映射快照,这样我就看不到其他进程对文件所做的任何更改(写入或删除) 。有点像写时复制,但其他进程正在执行写入操作。我可以在 Windows 上使用内存映射文件来执行此操作吗?
I'm trying to use a memory-mapped file under windows (using CreateFile/CreateFileMapping/MapViewOfFile functions), and I'm currently specifying FILE_SHARE_READ and FILE_SHARE_WRITE when calling CREATE_FILE. However, this is locking the file from being using by other processes.
What I want is to memory-map a snapshot of the file at the time I call CreateFileMapping or MapViewOfFile, so that I don't see any changes (writes or deletes) made to the file by other processes. Sort of like copy-on-write, but other processes are doing the write. Can I do this using memory-mapped files on windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是内存映射文件的工作方式。 Windows 对文件设置了硬锁,这样任何人都无法更改其内容并使其与映射到 RAM 的页面不同。 RAM 中的这些页面在创建文件视图的所有进程之间共享。没有“撕下”选项。
您可以简单地映射文件并在视图中创建字节的副本。通常需要与其他进程进行一些同步。
That's just not how memory mapped files work. Windows puts a hard lock on the file so that nobody can change its content and make it different from the pages mapped into RAM. Those pages in RAM are shared between all processes that created a view on the file. There is no 'tear off' option.
You could simply map the file and make a copy of the bytes in the view. Some synchronization with the other processes would typically be required.