每个fread/fwrite有多少个随机/顺序访问? [英] How many random / sequential access per fread / fwrite?

查看:107
本文介绍了每个fread/fwrite有多少个随机/顺序访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于C文件I/O,我有以下问题.

I have the following question regarding C file I/O.

在物理级别(硬盘驱动器)上,假设每个fread(n_blocks, size, length,FILE fp)操作都应对该同一个缓冲区的第一页(块)进行一次随机访问,而对下一个块进行n-1次顺序访问,是否有效?

At a physical level (harddrive), is it valid to assume that every fread(n_blocks, size, length,FILE fp) operation should cost one random access for the first page (block) and n-1 sequential accesses for the next blocks of that same buffer ??

我之所以这样假设,是因为操作系统具有如此多的进程,大多数可以确定其中一个进程还在本地程序的每个fread之间写入文件或从中读取文件,并且假设硬盘驱动器位于另一个进程中.扇形/圆柱体.

I assumed this because the OS has so many processes that is mostly sure that one of them is also writing to or reading from a file between each fread of the local program and by that assumption the hard drive is positioned at another sector / cylinder.

这个假设还可以吗?

推荐答案

您可以假设您想要的任何东西,实际上这要复杂得多.

You can assume whatever you want, it's much more complicated in reality.

fread/fwrite通常将在您的进程的内存中读取/写入内部缓冲区.当缓冲区已满/为空时,它们会将读/写转发给具有自己缓存的操作系统.如果您正在读取并且操作系统无法在高速缓存中找到文件的一部分,则您的程序将等到实际从硬盘驱动器中获取数据后,这是一项昂贵的操作.如果您正在写,那么数据将被复制到OS缓存中并驻留在该缓存中,直到刷新到磁盘为止,这可能在程序关闭文件后很长时间才会发生.然后,当今的硬盘驱动器又会拥有操作系统甚至可能不知道的自己缓存.

fread/fwrite will usually read and write from/to an internal buffer in the memory of your process. When the buffer is full/empty, they will forward the read/write to the operating system, which has its own cache. If you are reading and the OS can't find the portion of the file in the cache then your program will wait till the data is actually fetched from the hard-drive, which is an expensive operation. If you're writing then the data will be just copied to the OS cache and reside there till it'll flush to the disk, which may happen long after your program has closed the file. Then, today's hard drives have in turn their own caches which the OS may not even be aware of.

这篇关于每个fread/fwrite有多少个随机/顺序访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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