How is cache coherency handled when a file is opened for “ordinary” (cached) I/O and also opened memory mapped?
The Windows NT/2000/XP file systems all utilize the virtual memory system for caching the file system data. The task of managing the file system data is the responsibility of the cache manager. The cache manager accomplishes this by memory mapping the files – which in turn is done by the Memory Manager. An application (such as Notepad) that uses memory-mapped files does so by memory mapping the files, again via the Memory Manager. What this means is that if an application program reads data from the file system (using the standard Win32 ReadFile API call) the file system satisfies this request by calling the Cache Manager (via CcCopyRead, for example) and the Cache Manager then, in turn, copies the data from virtual memory. The advantage of this approach is that there is only a single copy of the file – the one in virtual memory. Thus, both read/write and memory mapped access to the file are coherent.