当两个进程写入mmaped文件的相同部分时会发生什么? [英] What happens when two processes write to same portion of a mmaped file?

查看:388
本文介绍了当两个进程写入mmaped文件的相同部分时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用mmap系统调用的C程序,该程序在64位Linux 3.12上运行.

I am writing a C program that makes use of mmap system call, running on Linux 3.12 64-bit.

如果我有两个进程通过读/写访问映射磁盘文件的同一区域,然后同时修改两个进程的区域内容...

If I have two processes mmaping the same region of a disk file with read/write access, and then modify the region content from both processes at the same time...

  • 在msync之前或之后,一个进程可以查看(读取)另一进程更改的数据吗?
  • 如果其他进程可以看到更新-Linux mmap实现是否可以保证?

推荐答案

是的,这就是内存映射的目的之一:作为即时IPC"的一种形式.

Yes, that's one of the purposes of memory mapping: as a form of "instantaneous IPC".

您必须设置MAP_SHARED标志:

You must set the MAP_SHARED flag:

如果您希望为此目的使用共享内存,则可以考虑使用shminit()/shmat()API:

If you wish to use shared memory for this purpose, I would consider the shminit()/shmat() APIs instead:

建议:查阅Beej的* nix进程间通信指南:

Suggestion: check out Beej's Guide to *nix Interprocess Communication:

不,如果您使用原始的mmap()API,则没有之前/之后保证",并且如果您希望同时读取/写入数据,则必须使用某种锁定方式(例如信号量).

And no, if you use the raw mmap() APIs, there's no "before/after guarantee", and you must use some kind of locking (e.g. semaphores) if you wish to read/write data concurrently.

此外,来自

mmap和shmat服务都提供了多种功能 映射对象的相同区域以便它们共享的过程 该对象的可寻址性.但是,mmap子例程会扩展 该功能超出了shmat子例程提供的功能, 允许相对无限数量的此类映射被 已确立的.尽管此功能增加了映射数量 每个文件对象或内存段均受支持,这可能会导致效率低下 适用于许多进程将相同文件数据映射到其中的应用程序 他们的地址空间.

Both the mmap and shmat services provide the capability for multiple processes to map the same region of an object such that they share addressability to that object. However, the mmap subroutine extends this capability beyond that provided by the shmat subroutine by allowing a relatively unlimited number of such mappings to be established. While this capability increases the number of mappings supported per file object or memory segment, it can prove inefficient for applications in which many processes map the same file data into their address space.

mmap子例程为每个进程提供唯一的对象地址 映射到一个对象.该软件通过提供 每个进程都有一个唯一的虚拟地址,称为别名.这 shmat子例程允许进程共享映射的地址 对象.

The mmap subroutine provides a unique object address for each process that maps to an object. The software accomplishes this by providing each process with a unique virtual address, known as an alias. The shmat subroutine allows processes to share the addresses of the mapped objects.

以上适用于所有* nix变体,包括Linux和AIX.

The above is true of any *nix variant, including both Linux and AIX.

这篇关于当两个进程写入mmaped文件的相同部分时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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