When is a file loaded into memory - for fread, fopen and fwrite calls?

When do I do fopen and then fread when the file is actually / partially loaded into memory during fopen or fread?

Or is it partially loaded into fopen based on file size and then completely loaded during fread?

Similarly, what happens inside the system at the OS level when fwrite is called? Is the file loaded into memory at this time, or is there an exchange of links to the page with this particular part of the file in memory?

What happens at the OS level with each of these calls regarding loading files into memory?

+7
source share
4 answers
  • fopen() only creates a file descriptor.
  • fread() actually reads the file into the memory buffer (OS level buffering can be transparent to the client).
  • fwrite() writes data to a file, although its commit to storage may be delayed (for example, with a log file system.)
+6
source

Typically, a file does not load into memory when it is opened. Instead, parts are loaded for each reading; due to all kinds of buffering, large chunks that you request in each fread can be loaded.

When you fwrite some data, it is eventually copied to the kernel, which then writes it to disk (or anywhere) after buffering. In general, you don’t need to download part of the file for recording.

+4
source

This usually depends on the file system and OS. the windows have a caching mechanism that deals with a file in 256 KB blocks and loads each piece when a read request is included in this fragment. The fopen call should not cause the contents of the file to be read from the media. And fread will lead to partial reading (or full reading for small files) from the environment. Partial reads are usually equal to the size of the cache line in the cache manager (256 KB).

fwrite may also / may not cause the actual recording to the media. Usually this leads to the fact that the client data is transferred to the area of ​​cached files in RAM, but there is no guarantee that the data is actually written to the media. on Windows, the cache manager decides when to clear the cached area of ​​the file to media. If you want all dirty data to be fflush to the media after fwrite , you need to call fflush after that.

+2
source

While it depends on the OS, in a modern operating system, all disk activity is transparently cached, so when you open a file in reality, it is mapped to part of the virtual memory space.

This means that there is no disk activity before the actual read / write.

This is true even if you open the file without memory mapping (for example: fopen), while if you open it with a memory map (for example: mmap), you simply lose the "transparency".

+1
source

All Articles