5

I'm learning file operation calls under Linux. The read() and write() and many other functions use cache to increase performance, and I know fsync() can transfer data from cache to disk device.

However, is there any commands or system calls that can determine whether the data is cached or written to disk?

jasonwryan
  • 71,734
  • 34
  • 193
  • 226
Krist Pan
  • 105
  • 4

2 Answers2

5

Read data is (directly) read from the cache only if it is already there. That implies that cached data was previously accessed by a process and kept in cache. There is no system call or any method for a process to know if some piece of data to be read is already in cache or not.

On the other hand, a process can select if it wants written data to be immediately stored on the disk or only after a variable delay which is the general case. This is done by using the O_SYNC flag when opening the file.

There is also the O_DIRECT flag which when supported force all I/Os to bypass the read and write cache and go directly to the disk.

Finally, the hard-disk itself is free to implement its own cache so even after a synchronous write call has returned, there is no guarantee data is already on the disk platters.

jlliagre
  • 60,319
  • 10
  • 115
  • 157
1

fsync already does this for the file in question (write thru to disk), but it does not guarantee this for the directory. This requires an extra fsync-call.

ott--
  • 846
  • 1
  • 6
  • 12