文件何时加载到内存中 - 用于 fread、fopen 和 fwrite 调用?

2022-01-11 00:00:00 operating-system c c++

当我执行 fopen 然后 fread 时,文件实际/部分加载到内存中的时间在 fopen 或 fread 期间?

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

或者是根据文件大小在 fopen 部分加载,然后在时间完全加载害怕吗?

Or is it partially loaded at fopen based on size of file and then fully loaded at time of fread?

同样,当调用 fwrite 时,在操作系统级别内部会发生什么?文件当时是否加载到内存中,或者页面交换发生在仅检索该部分内存中的文件?

Similarly what happens internally at the OS level when fwrite is called? Is the file loaded into memory at that time, or a page swap happens retriving just that part of file in memory?

在每个调用中,在内存中加载文件时,操作系统级别会发生什么情况?

What happens at the OS level at each of these calls with respect to file loading in memory?

推荐答案

  • fopen() 只创建文件句柄.
  • fread() 实际上将文件读入内存缓冲区(操作系统级别的缓冲可能对客户端透明.)
  • fwrite() 将数据写入文件,但提交到存储可能会延迟(例如,使用日志文件系统.)
    • fopen() only creates a handle to the file.
    • fread() actually reads the file into a memory buffer (OS-level buffering may occur transparently to the client.)
    • fwrite() writes data into the file, though its committing to the storage may get delayed (e.g. with journalled filesystem.)

相关文章