简体   繁体   English

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

[英]When is the file loaded into memory - for fread, fopen and fwrite calls?

When I do a fopen and then a fread, when is the file actually/partially loaded in the memory during fopen or fread? 当我执行fopen然后fread时,fopen或fread期间文件实际/部分加载到内存中的时间是什么时候?

Or is it partially loaded at fopen based on size of file and then fully loaded at time of fread? 或者是根据文件大小在fopen部分加载,然后在fread时满载?

Similarly what happens internally at the OS level when fwrite is called? 类似地,当调用fwrite时,操作系统级别内部会发生什么? 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() only creates a handle to the file. fopen()只创建文件的句柄。
  • fread() actually reads the file into a memory buffer (OS-level buffering may occur transparently to the client.) fread()实际上将文件读入内存缓冲区(操作系统级缓冲可能对客户端透明)。
  • fwrite() writes data into the file, though its committing to the storage may get delayed (eg with journalled filesystem.) fwrite()将数据写入文件,尽管它提交到存储可能会延迟(例如,使用日志文件系统。)

Typically, the file is not loaded into memory upon opening it. 通常,打开文件时不会将文件加载到内存中。 Instead, parts are loaded in for each read; 相反,每次读取都会加载部件; due to all kinds of buffering, greater chunks may be loaded then you ask for in each fread . 由于各种缓冲,可能会加载更大的块然后你在每个fread

When you fwrite some data, it is eventually copied into the kernel which will then write it to disk (or wherever) after buffering. 当您fwrite一些数据,它最终被复制到内核,然后将其写入缓冲后的磁盘(或地方)。 In general, no part of a file needs to be loaded in order to write. 通常,为了写入,不需要加载文件的任何部分。

Generally it depends on the file system and OS. 通常它取决于文件系统和操作系统。 in windows there is a caching mechanism which deals with a file in 256KB chunks and loads each chunk upon read request falling in that chunk. 在Windows中有一个缓存机制,它处理256KB块中的文件,并在读取请求时加载每个块,落在该块中。 A call to fopen should not cause reading the file content from media. 调用fopen不应该导致从媒体中读取文件内容。 And fread will cause partial read (or complete read for small files) from the media. 并且fread将导致媒体部分读取(或完整读取小文件)。 Partial read usually is equal to cache line size in cache manager (256KB). 部分读取通常等于缓存管理器中的缓存行大小(256KB)。

fwrite also may/may not cause a actual write to the media. fwrite也可能/可能不会导致对媒体的实际写入。 It usually causes client data to be transferred to the cached file area in RAM, but there is no guaranty that data actually is written to media. 它通常会将客户端数据传输到RAM中的缓存文件区域,但不保证数据实际写入介质。 in Windows, cache manager decides when to flush a cached area of a file to media. 在Windows中,缓存管理器决定何时将文件的缓存区域刷新到介质。 If you want to make sure all dirty data is flushed to media after fwrite , you need to call fflush afterwards. 如果要确保在fwrite之后将所有脏数据刷新到介质,则需要在之后调用fflush

While this is OS-dependent, in modern operating system all disk activity is transparenly cached, so that when you open a file in reality it is mapped to a portion of the virtual memory space. 虽然这是依赖于操作系统的,但在现代操作系统中,所有磁盘活动都是透明缓存的,因此当您实际打开文件时,它会映射到虚拟内存空间的一部分。

This mean that no disk activity occur before the actual reading/writing. 这意味着在实际读/写之前没有磁盘活动。

This is true even if you open the file without memory-mapping (eg: fopen), while if you open it with memory-mapping (eg: mmap) you just lose the "transparency". 即使您打开没有内存映射的文件(例如:fopen)也是如此,而如果您使用内存映射打开它(例如:mmap),您就会失去“透明度”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM