内存映射文件(Memory-Mapped Files)

来源:互联网 发布:淘宝优惠卷软件挣钱 编辑:程序博客网 时间:2024/06/13 05:48

内存映射文件(Memory-Mapped Files)可以用来:

 

(1)操作系统用来加载进程和动态库

(2)访问硬盘文件

The primary benefit of memory mapping a file is increasing I/O performance, especially when used on large files. memory mapped files is faster than using direct read and write operations for two reasons. Firstly, a system call is orders of magnitude slower than a simple change to a program's local memory. Secondly, in most operating systems the memory region mapped actuallyis the kernel'spage cache (file cache), meaning that no copies need to be created in user space.

(3)进程间共享数据

事实上Windows的进程间通信如RPC,COM,OLE,DDE,windows messages(WM_COPYDATA等),the Clipboard, mailslots,pipes, sockets等其底层机制都是Memory-Mapped Files.

 

其缺点:

The major reason to choose memory mapped file I/O is performance. Nevertheless there can be tradeoffs. The standard I/O approach is costly due to system call overhead and memory copying. The memory mapped approach has its cost inminor page faults - when a block of data is loaded inpage cache, but is not yet mapped into the process's virtual memory space. In some circumstances, memory mapped file I/O can be substantially slower than standard file I/O.

Another drawback of memory mapped files relates to a given architecture's address space: a file larger than the addressable space can have only portions mapped at a time, complicating reading it. For example, a32-bit architecture such as Intel'sIA-32 can only directly address 4GiB or smaller portions of files. This drawback is avoided in the case of devices addressing memory when anIOMMU is present.