在 MAP_PRIVATE 上查询 [英] Query on MAP_PRIVATE

查看:46
本文介绍了在 MAP_PRIVATE 上查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 MAP_PRIVATE 的查询.一旦我使用 MAP_PRIVATE 映射了一个区域,我就知道我所做的更改对其他进程是不可见的.但我只想确保如果其他一些进程也在同一实例中运行,如果它对 mmap 文件进行了一些修改,这些更改是否会在我的进程中可见(使用 MAP_PRIVATE 映射它).

基本上我的要求是我想在我的应用程序中某个时刻映射一个文件,条件是其他进程对此文件所做的更新在我的任务完成之前不应该在我的进程中可见.那么我可以安全地使用 MAP_PRIVATE 映射它吗?

谢谢,拉姆

解决方案

仔细阅读多遍 mmap(2) 手册页.

<块引用>

 MAP_PRIVATE创建私有的写时复制映射.更新映射对其他映射的进程不可见相同的文件,并且不会传递到底层文件.未指定是否对文件进行了更改在映射区域中可以看到 mmap() 调用之后.

所以你处于未指定"的情况

您需要在其他进程(写入文件)和您的进程之间进行一些同步.参见 sem_overview(7)mlock(2)msync(2)

如果其他进程是任意的(并且你不能改变它们的行为),你就会被卡住.这是 Unix 文件哲学:任意进程可以同时写入同一个文件,但是并不总是指定(并且可能取决于文件系统).如果您希望多个进程写入同一个文件,它们应该以某种方式同步和协作(例如使用 flock(2)lockf(3)) 等...).

也许你应该使用一些数据库系统而不是普通文件!例如.sqlite 会为你做一些同步(当然,假设所有进程都在写入公共文件使用 sqlite ...)

I have a query on MAP_PRIVATE. Once I map a region with MAP_PRIVATE I know that the changes I made are invisible to other process. But I just want to make sure that if some other process which is also running at the same instance, if it makes some modifications to the mmap file, will those changes be visible inside my process (which mapped it using MAP_PRIVATE).

Basically my requirement is that I want to mmap a file at one moment in my application and the condition is that the updates made to this file by other processes should not be visible inside my process until my task is done. So can I mmap it using MAP_PRIVATE safely?

Thanks, Ram

解决方案

Read carefully and several times mmap(2) man page.

  MAP_PRIVATE
              Create a private copy-on-write mapping.  Updates to the
              mapping are not visible to other processes mapping the
              same file, and are not carried through to the underlying
              file.  It is unspecified whether changes made to the file
              after the mmap() call are visible in the mapped region.

So you are in the "unspecified" case

You need to have some synchronization between the other processes (writing to the file) and your process. See perhaps sem_overview(7) and mlock(2) and msync(2)

If the other processes are arbitrary (and you cannot change their behavior), you are stuck. This is the Unix file philosophy: arbitrary processes can write simultaneously to the same file, but what happens then is not always specified (and might depend upon the filesystem). If you want several processes to write to the same file, they should synchronize and cooperate somehow (e.g. lock with flock(2) or lockf(3), etc...).

Maybe you should use some database system instead of plain files! E.g. sqlite would do some synchronization for you (assuming of course all the processes are writing to the common file using sqlite ...)

这篇关于在 MAP_PRIVATE 上查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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