简体   繁体   English

文件系统I / O缓冲区

[英]File system I/O buffer

Consider the following pseudo-code snippet to read a file from it's end 考虑以下伪代码片段以从文件末尾读取文件

while (1) {
  seek(fd, offset, SEEK_END)
  read(fd, buf, n)

  // process the buffer, break on EOF...

  offset -= n
}

Now n can vary between 1 byte and let's say 1kB . 现在n可以在1 byte之间变化,比如说1kB

How big would be the impact on the file system for very small n s? 有多大将是非常小的文件系统的影响n S' Is this compensated by file system buffering for the most part, or should I always read larger chunks at once? 这是否大部分由文件系统缓冲所补偿,还是我应该始终一次读取较大的块?

The answer depends on your operating system. 答案取决于您的操作系统。 Most modern OS's use a multiple of the system page size for file buffers. 大多数现代操作系统都使用系统页面大小的倍数作为文件缓冲区。 As such, 4KB (the most common page size on most systems) is likely to be the minimum unit the disk cache holds. 这样,4KB(在大多数系统上最常见的页面大小)可能是磁盘缓存所容纳的最小单位。 The bigger problem is that your code is making a lot of redundant system calls, which are expensive. 更大的问题是您的代码正在进行大量的冗余系统调用,这很昂贵。 If you are concerned about performance, consider either buffering the data you think you need in big chunks and then referencing that data directly from your buffer, or calling mmap() if your system supports it and accessing the mapped file directly. 如果您担心性能,可以考虑将需要的数据mmap()缓冲,然后直接从缓冲区中引用该数据;如果系统支持,则调用mmap()并直接访问映射的文件。

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

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