[英]writing to memory mapped file while it is memory-mapped
I have memory mapped a file on disk as follow: 我已将内存映射到磁盘上的文件,如下所示:
const wchar_t fileName[] = L"temp.txt";
HANDLE h = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE fileMap = CreateFileMapping(h, NULL, PAGE_READWRITE, 0x0, 1024, NULL);
char *ptr = (char *)MapViewOfFile(fileMap, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 1024)
Is it possible to write to the same file by another process while it is memory mapped? 在内存映射的情况下,是否可以通过另一个进程写入同一文件?
FILE *fp = fopen("temp.txt", "w+"); if(NULL == fp) printf("Failed to open\\n");
Above code always prints 'Failed to open' if the file is memory mapped. 如果文件是内存映射的,则上面的代码始终显示“无法打开”。
The answer for a coherent shared use is simply :yes ,except for remote files. 一致共享使用的答案很简单:是, 远程文件除外。 (see documentations of
CreateFileMapping
and/or MapViewOfFile
). (请参阅
CreateFileMapping
和/或MapViewOfFile
文档)。
For shared use you must however open the file ( CreateFile
) in a shared-mode (which you didn't specify). 为了共享使用,您必须以共享模式(未指定)打开文件(
CreateFile
)。 I don't know in which share-mode fopen
operates ,but I suspect your fopen
fails because of the missing share-mode with CreateFile
. 我不知道
fopen
在哪个共享模式下运行,但是我怀疑您的fopen
失败是因为缺少CreateFile
共享模式。 If fopen
still fails when using a shared-mode you should use CreateFile
instead, 如果在使用共享模式时
fopen
仍然失败,则应改用CreateFile
,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.