什么时候应该使用文件访问的mmap? [英] When should I use mmap for file access?

查看:189
本文介绍了什么时候应该使用文件访问的mmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POSIX环境提供访问文件至少有两种方法。有标准的系统调用的open()阅读()的write(),和朋友,但也有使用的mmap()来将文件映射到虚拟内存的选项。

POSIX environments provide at least two ways of accessing files. There's the standard system calls open(), read(), write(), and friends, but there's also the option of using mmap() to map the file into virtual memory.

当它是preferable使用一个比其他?他们算什么个人优势优点包括两个接口?

When is it preferable to use one over the other? What're their individual advantages that merit including two interfaces?

推荐答案

MMAP是伟大的,如果你有多个进程相同的文件,这是常见的那种服务器系统我写在只读方式访问数据。 MMAP允许所有这些进程共享相同的物理内存页,节省了大量的内存。

mmap is great if you have multiple processes accessing data in a read only fashion from the same file, which is common in the kind of server systems I write. mmap allows all those processes to share the same physical memory pages, saving a lot of memory.

MMAP还允许操作系统优化寻呼操作。例如,考虑两个程序;程序的它读取一个1MB的文件到缓冲区中使用malloc创建和计划B上mmaps 1MB的文件到内存中。如果操作系统有交换的内存部分出来,就必须写缓冲区的内容交换,才可以重新使用的内存。在B的情况下,因为操作系统知道如何从他们从被mmap'd现有文件还原他们的任何未修改mmap'd页面可以立即再次使用。 (操作系统可以检测到哪些页面是由只读标志最初写mmap'd页面和追赶赛格故障类似于写时复制策略不变)。

mmap also allows the operating system to optimize paging operations. For example, consider two programs; program A which reads in a 1MB file into a buffer creating with malloc, and program B which mmaps the 1MB file into memory. If the operating system has to swap part of A's memory out, it must write the contents of the buffer to swap before it can reuse the memory. In B's case any unmodified mmap'd pages can be reused immediately because the OS knows how to restore them from the existing file they were mmap'd from. (The OS can detect which pages are unmodified by initially marking writable mmap'd pages as read only and catching seg faults, similar to Copy on Write strategy).

MMAP也是进程间通信有用。您可以mmap的一个文件的读/在需要沟通,然后在mmap'd地区使用同步控制器元(这是MAP_HASSEMAPHORE标志是什么)写入处理。

mmap is also useful for inter process communication. You can mmap a file as read / write in the processes that need to communicate and then use sychronization primitives in the mmap'd region (this is what the MAP_HASSEMAPHORE flag is for).

一个地方mmap的可能是尴尬的是,如果你需要一个32位的机器上非常大的文件工作。这是因为MMAP已找到你的进程的地址空间地址的连续块足够大,以适应该文件的整个范围映射。如果您的地址空间变得支离破碎,在那里你可能有2 GB的地址空间自由这会成为一个问题,但它没有个人的范围可以容纳1 GB文件映射。在这种情况下,你可能必须映射小块文件比你想使它适合。

One place mmap can be awkward is if you need to work with very large files on a 32 bit machine. This is because mmap has to find a contiguous block of addresses in your process's address space that is large enough to fit the entire range of the file being mapped. This can become a problem if your address space becomes fragmented, where you might have 2 GB of address space free, but no individual range of it can fit a 1 GB file mapping. In this case you may have to map the file in smaller chunks than you would like to make it fit.

与MMAP为读/写一个替代另一个潜在的尴尬是,你必须开始对页面大小的偏移量的映射。如果你只是想获得的值X的一些数据,您将需要修正内容的偏移量,它与MMAP兼容。

Another potential awkwardness with mmap as a replacement for read / write is that you have to start your mapping on offsets of the page size. If you just want to get some data at offset X you will need to fixup that offset so it's compatible with mmap.

最后,读/写你的只有这样,才能与某些类型的文件的工作。 MMAP不能之类的东西的管道和ttys中被使用。

And finally, read / write are the only way you can work with some types of files. mmap can't be used on things like pipes and ttys.

这篇关于什么时候应该使用文件访问的mmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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