简体   繁体   English

mmap的用例

[英]Use cases of mmap

I'm currently studying for my OS finals. 我目前正在研究OS决赛。 The teacher in some papers is briefly mentioning the mmap function (memory map). 在某些论文中,老师简短地提到了mmap函数(内存图)。

As I understand it ( correct me if i'm wrong ), mmap is used to load some files from the physical memory to the RAM (after a page default). 据我了解( 如果我错了 ,请纠正我 ),mmap用于将某些文件从物理内存加载到RAM(在默认页面之后)。 The problem is that I don't see any practical reason for this other then to make the access time to that file faster. 问题在于,除了使访问该文件的时间更快之外,我看不到任何实际的原因。

Am I correct? 我对么? Is mmap only used for this? mmap仅用于此吗?

"mmap" has lots of purposes: “ mmap”有很多用途:

  1. Mapping a file for faster read/write access is certainly one use 映射文件以实现更快的读/写访问肯定是一种用途

  2. Shared memory (eg for interprocess communications) is another 共享内存(例如,用于进程间通信)是另一个

  3. mmap is also used to map I/O port addresses for low-level device communications mmap还用于映射I / O端口地址以进行低级设备通信

mmap is used to load some files from the physical memory to the RAM (after a page default) mmap用于将某些文件从物理内存加载到RAM(在默认页面后)

to load the missing pages . 加载缺少的页面 also modifications can be written to the disk the same way! 也可以用相同的方式将修改内容写入磁盘!

  • Performance (you don't have to load the whole file), works really well if you have random access. 性能(不必加载整个文件),如果您具有随机访问权限,则效果很好。
  • It can considerably make your code more compact, you don't have to worry about file I/O. 它可以使您的代码更紧凑,而您不必担心文件I / O。
  • The OS can handle memory management, decide which pages to keep in memory and which to discard. 操作系统可以处理内存管理,确定要保留在内存中的页面以及要丢弃的页面。

In addition to @paulsm4's answer: 除了@ paulsm4的答案:

  1. ... ...
  2. ... ...
  3. ... ...
  4. Most modern malloc(3) implementations use mmap(2) to manage private process memory. 大多数现代的malloc(3)实现都使用mmap(2)管理私有进程内存。
  5. Dynamic link-loader ld.so(8) uses it for mapping shared libraries. 动态链接加载器ld.so(8)使用它来映射共享库。

mmap takes memory management out of the hands of the programmer to a large extent, and puts it in the hands of the OS. mmap在很大程度上将内存管理从程序员的手中解放了出来,并将其交给操作系统。

It's about demand paging using the virtual memory subsystem from disk to physical memory. 这是关于使用虚拟内存子系统从磁盘到物理内存的需求分页。

So to look at the 11111th byte of a file, instead of seeking and reading, you can mmap and use an array index. 因此,要查看文件的第11111个字节,可以查找并使用数组索引,而不是查找和读取。 The OS will keep surroundiung data in its "buffer cache" (page cache really). 操作系统会将周围的数据保留在其“缓冲区高速缓存”(实际上是页面高速缓存)中。

Here's an example: http://stromberg.dnsalias.org/~strombrg/pbmonherc.html 这是一个示例: http : //stromberg.dnsalias.org/~strombrg/pbmonherc.html

The example's a little messy because it was written at a time when Linux had mmap support in its kernel, but the C library didn't yet have a stub for calling it. 该示例有点混乱,因为它是在Linux内核中支持mmap的时候编写的,但是C库尚未有调用它的存根。 But you can pretty much ignore mmap.c. 但是您几乎可以忽略mmap.c。 The example uses mmap to set pixels on and off using a monochromatic display adapter. 该示例使用mmap通过单色显示适配器设置像素的开和关。

Another reasonable use is for a bloom filter: http://stromberg.dnsalias.org/~strombrg/drs-bloom-filter/ 另一个合理的用途是使用布隆过滤器: http//stromberg.dnsalias.org/~strombrg/drs-bloom-filter/

...but on 32 bit OS's, the maximum size of an mmap'd memory region kinda hurts. ...但是在32位OS上,mmap的内存区域的最大大小有点麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM