0
votes

I have a code that writes to file using MapViewOfFile and FlushViewOfFile before remapping.

And I have a second code, that reads from the same file (on disk, not HANDLE/mapping object) using MapViewOfFile in other thread of the same process.

The flags of views are PAGE_READONLY/FILE_READ and PAGE_READWRITE/FILE_WRITE.

On my PC everything works totally fine. But when the same build runs as service on other PC, it have one strange bug: when I remap reader (this means UnmapViewOfFile + MapViewOfFile) with offset == 0 (to read some file header), it doesn't return any error, but the reading itself returns some old values, that were already changed by other thread.

When I open ProcessMonitor, it shows, that there were no reading from offset = 0, only the writing.

So, the problem is that new call CreateFileMapping/MapViewOfFile returns sucessfully, but maps some old cached page, and doesn't read new data from the file.

Once again, same build fine works on my PC.

Can anybody suggest something? Thanks.

1
looks like the problem is with FlushViewOfFile. I have switched reading to simple ReadFile, and it works fine on local pc, but the same problem happens when I'm trying to read this file from another PC (with simple ReadFile).. - Алр

1 Answers

0
votes

From the documentation for MapViewOfFile:

Although MapViewOfFile works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer only sees its own writes to the page. When the data gets updated on the disk, it is not merged.

In other words, the behaviour you are observing is as expected.

Also:

A mapped view of a file is not guaranteed to be coherent with a file that is being accessed by the ReadFile or WriteFile function.

Since you need coherency over the network, you can't use a file mapping. You'll have to use ReadFile to read the contents and WriteFile to modify them. You should also use LockFile or some other method to coordinate access to the file, to ensure that the data you're reading is consistent.

(You should also note that it is not useful to unmap and then remap the exact same view. That's basically a no-op.)