mmap()的VS阅读() [英] mmap( ) vs read( )

查看:110
本文介绍了mmap()的VS阅读()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C ID3标签批量ID3标签编辑器通常是在一个MP3连接codeD文件的开头,虽然旧的(第1版)标签是在年底。该应用程序的目的是接受来自命令行的目录和帧ID列表,然后递归更新所有它找到的ID3标签的目录结构。用户还可以选择删除所有旧(第1版)的标签。另一种选择是简单地显示当前标记,而不进行更新。该目录可能包含2个文件或200万美元。如果用户是指以更新文件,我正打算将整个文件加载到内存,进行更新,然后将其保存(该文件可能被重命名为好)。然而,如果用户只装置以打印当前ID3标签,然后加载整个文件似乎过高。所有的文件后可能是200MB。

I'm writing a bulk ID3 tag editor in C. ID3 tags are usually at the beginning of an mp3 encoded file, although older (version 1) tags are at the end. The app is designed to accept a directory and frame ID list from the command line, then recurse the directory structure updating all the ID3 tags it finds. The user may additionally choose to remove all older (version 1) tags. Another option is to simply display the current tags, without performing an update. The directory might contain 2 files or 2 million. If the user means to update the files, I was planning to load the entire file into memory, perform the updates, then save it (the file may be renamed as well). However, if the user only means to print the current ID3 tags, then loading the entire file seems excessive. After all the file could be 200mb.

我已经通过这个线程,这是有见地阅读一>

I've read through this thread, which was insightful - mmap() vs. reading blocks

所以我的问题是,走什么是最有效的方式这一点 - 阅读(),mmap()的,或某种组合?设计思路的欢迎。

So my question is, what the most efficient way to go about this -- read(), mmap() or some combination? Design ideas welcome.

TIA,

安德鲁

编辑:这是我的理解是本质上的mmap代表文件加载到内存,虚拟内存子系统。在我看来,在VMM将高度在大多数系统上进行了优化,因为它是系统性能的关键。

It's my understanding that mmap essentially delegates loading a file into memory, to the virtual memory subsystem. It seems to me, the VMM would be highly optimized on most systems as it's critical for system performance.

推荐答案

这真的取决于你想要做什么。如果你需要做的是跳跃到一个已知的偏移量和读出一个小标签,阅读()可能会更快(的mmap()已做了一些相当复杂的内部会计)。如果您在复制出来的MP3的所有200MB,但是,还是扫描它可能出现在一个不知名的抵消一些标签计划,那么的mmap()很可能更快的方法。

It really depends on what you're trying to do. If all you need to do is hop to a known offset and read out a small tag, read() may be faster (mmap() has to do some rather complex internal accounting). If you are planning on copying out all 200mb of the MP3, however, or scanning it for some tag that may appear at an unknown offset, then mmap() is likely a faster approach.

例如,如果你需要将整个文件,以便插入ID3标签下移几百个字节,一个简单的办法是扩大与 ftruncate文件(),mmap的文件,那么 memmove与()的内容了一点。然而,如果将你的程序,而它的运行崩溃破坏文件。您也可以在文件的内容复制到一个新的文件 - 这是另外一个地方mmap()的真正的亮点;你可以简单地的mmap()旧文件,那么它的所有数据复制到一个单一的write()新文件

For example, if you need to shift the entire file down a few hundred bytes in order to insert an ID3 tag, one simple approach would be to expand the file with ftruncate(), mmap the file, then memmove() the contents down a bit. This, however, will destroy the file if your program crashes while it's running. You could also copy the contents of the file into a new file - this is another place where mmap() really shines; you can simply mmap() the old file, then copy all of its data into the new file with a single write().

在短,的mmap()是伟大的,如果你在传输的字节总数方面做大量的IO;这是因为它减少了所需要的份数,并且可以显著减少所需用于读缓存数据的内核中的条目数。然而的mmap()至少需要两次旅行到内核(三级如果你清理的时候,你就大功告成了!映射),并做一些复杂的内部内核核算,所以固定的开销可能会很高。

In short, mmap() is great if you're doing a large amount of IO in terms of total bytes transferred; this is because it reduces the number of copies needed, and can significantly reduce the number of kernel entries needed for reading cached data. However mmap() requires a minimum of two trips into the kernel (three if you clean up the mapping when you're done!) and does some complex internal kernel accounting, and so the fixed overhead can be high.

阅读(),另一方面需要额外的内存到内存复制,因此可以是低效的大I / O操作,但简单,这样固定的开销相对较低。总之,使用的mmap()为大批量I / O和阅读() $ p $垫()为一次性的,小的I / O。

read() on the other hand involves an extra memory-to-memory copy, and can thus be inefficient for large I/O operations, but is simple, and so the fixed overhead is relatively low. In short, use mmap() for large bulk I/O, and read() or pread() for one-off, small I/Os.

这篇关于mmap()的VS阅读()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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