关于FlushViewOfFile的问题

大狗狗 2008-05-20 10:22:17
这两天学习<<windows核心编程>>内存映射文件这部分,书上说:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
为了提高速度,系统将文件的数据页面进行高速缓存,并且在对文件的映射视图进行操作时不立即更新文件的磁盘映像。如果需要确保你的更新被写入磁盘,可以强制系统将修改过的数据的一部分或全部重新写入磁盘映像中,方法是调用F l u s h Vi e w O f F i l e函数:


BOOL FlushViewOfFile(
PVOID pvAddress,
SIZE_T dwNumberOfBytesToFlush);
第一个参数是包含在内存映射文件中的视图的一个字节的地址。该函数将你在这里传递的地址圆整为一个页面边界值。第二个参数用于指明你想要刷新的字节数。系统将把这个数字向上圆整,使得字节总数是页面的整数。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
请问一下:
1 "对文件的映射视图进行操作时不立即更新文件的磁盘映像" 那么何时更新?我程序正常退出前一定会更新吧?如果程序意外结束(比如电脑死机)那是不是就可能无法将更改写入磁盘?

2 是不是只要程序不意外结束,我们就没使用FlushViewOfFile的必要?否则请问在什么情况下有必要使用它?
...全文
827 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
scq2099yt 2008-05-20
  • 打赏
  • 举报
回复
The FlushViewOfFile function writes to the disk a byte range within a mapped view of a file.

BOOL FlushViewOfFile(
LPCVOID lpBaseAddress, // start address of byte range to flush
DWORD dwNumberOfBytesToFlush // number of bytes in range
);

Parameters
lpBaseAddress
Pointer to the base address of the byte range to be flushed to the disk representation of the mapped file.
dwNumberOfBytesToFlush
Specifies the number of bytes to flush.
If dwNumberOfBytesToFlush is zero, the file is flushed from the base address to the end of the mapping.

Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
Flushing a range of a mapped view causes any dirty pages within that range to be written to the disk. Dirty pages are those whose contents have changed since the file view was mapped.

When flushing a memory-mapped file over a network, FlushViewOfFile guarantees that the data has been written from the local computer, but not that the data resides on the remote computer. The server can cache the data on the remote side. Therefore, FlushViewOfFile can return before the data has been physically written to disk. However, you can cause FlushViewOfFile to return only when the physical write is complete by specifying the FILE_FLAG_WRITE_THROUGH flag when you open the file with the CreateFile function.
xiantongyuan 2008-05-20
  • 打赏
  • 举报
回复
1.不保证意外结束。
2.需不需要调用FlushViewOfFile就看你个人的要求了,如果你认为系统默认的就ok了,就没必要了,如果不是那最好还是调用,比如你要确保在你需要从文件里获取数据时,文件的数据已经被更新了。
zhoujianhei 2008-05-20
  • 打赏
  • 举报
回复
如何保证程序不意外结束?
大狗狗 2008-05-20
  • 打赏
  • 举报
回复
up
cnzdgs 2008-05-20
  • 打赏
  • 举报
回复
1、在UnmapViewOfFile、CloseHandle和系统回收物理内存的时候写入磁盘。当进程结束时(包括正常和异常),系统会自动关闭该进程打开的所有Handle,所以会写入磁盘。除非是内核代码异常,导致死机,这时才可能没有写入。
2、FlushViewOfFile是为了实现程序自己控制写入磁盘而提供的,当你真正遇到这种需求的时候才能体会到它的价值。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧