| 怪事内存映射文件,没有FlushViewOfFile,也可以保存到文件? |
| [ 来源:ITWENKU 时间:2007-4-18 6:29:38 | 浏览:357人次
] |
| |
|
// 将数据写入文件 BOOL CData::Write(PVOID pvBuf, DWORD dwSize) { return Write(pvBuf, 0, dwSize); } BOOL CData::Write(PVOID pvBuf, DWORD dwOffsetAddr, DWORD dwSize) { if (!m_pvMapAddr) { return FALSE; } assert(dwOffsetAddr + dwSize < DAT_SIZE); ::memcpy((BYTE*)m_pvMapAddr + dwOffsetAddr, pvBuf, dwSize);
return TRUE; }
如上代码,我并没有使用FlushViewOfFile或UnmapViewOfFile,在定时器里,我每隔0.01秒就写入一次,然后在 任 务 管 理 器 里 强 行 结 束 相应进程,但文件依然能写入文件中。
哪位帮忙解释一下。
注:MSDN上说为了提高速度,内存映射文件并不会立刻写入内存的,只有调用 FlushViewOfFile 强行结束或调用 UnmapViewOfFile 。
没人?帮你UP下.
进程结束,操作系统会把映射文件写回磁盘的。
进程结束了,会做一些回收工作,进程所锁的文件标识被释放,文件被关闭,就会FLUSH一遍缓冲区的
问题是,进程是被强制关闭的,操作系统也会将文件写回磁盘吗?
另,我特意边写边用其他软件打开被内存映射的文件,发现:相应文件对应的地址里都写入了相应的内容。
这就说明:并不是在进程结束时才写入文件,而是memcpy就直接写入文件了。
缓冲满了,就自动写进去了,缓冲不满,就不会写,只有到进程结束,强制关闭,WINDOWS也知道的,也会对其进行进程结束的操作;否则强制关闭的进程所锁住的文件就永远不能用了。
CreateFile时我使用了FILE_SHARE_READ|FILE_SHARE_WRITE,再查看被内存映射的文件,发现,就算是写入8个字节,且只写一次,其实也已经被保存到了相应文件里。
比如,CData::Write(L"abcd", 10, 8);,虽然进程没结束,相信缓冲区也不会满,但abcd确实已经写入了文件了。
问题是,进程是被强制关闭的,操作系统也会将文件写回磁盘吗? ================================================================= 有证据说“强制关闭,系统不会帮你回收资源么“??? 一个见状的系统,会帮用户做很多事情的
就算强制关闭了,系统可以保证将相应信息写入文件。 但:为何没有调用FlushViewOfFile,比如:
比如,CData::Write(L"abcd", 10, 8);
虽然 进 程 没 结 束,相信 缓冲区 也 不 会 满,也没调用FlushViewOfFile,但abcd已经被写进文件了。
虽然 进 程 没 结 束,相信 缓冲区 也 不 会 满,也没调用FlushViewOfFile,但abcd已经被写进文件了。
实在想不通,请各位老大帮个忙解释解释到底是为什么。
那反过来说,请问,为什么一定要调用FlushViewOfFile系统才把信息写入硬盘捏?
..........................................................
to WingForce:“一定要调用FlushViewOfFile系统才把信息写入硬盘”是MSDN上说的。 核心编程里也这么说。
我的msdn: The FlushViewOfFile function writes to the disk a byte range within a mapped view of a file.
BOOL FlushViewOfFile( LPCVOID lpBaseAddress, SIZE_T dwNumberOfBytesToFlush );
Parameters lpBaseAddress [in] Pointer to the base address of the byte range to be flushed to the disk representation of the mapped file. dwNumberOfBytesToFlush [in] 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.
没这句话。。。
核心编程也是书,既然你目前测试的结果不是这样,为什么还要执着的相信书呢?
多谢各位!!
|
|
 |
推荐文章 |
|