How many random / sequential access per fread / fwrite?

前端 未结 5 1208
孤街浪徒
孤街浪徒 2021-01-21 03:53

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

At a physical level (harddrive), is it valid to assume that every fread(n_blocks, size, length,FILE fp)

5条回答
  •  执念已碎
    2021-01-21 04:45

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

    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.

提交回复
热议问题