How the OS knows a page is dirty in mapped memory?

1.5k views Asked by At

I mean when data is updated directly in memory, without using write().

In linux I thought all the data specified in a msync call was flushed.

But in Windows the doc of FlushViewOfFile says "writing of dirty pages", so somehow the OS knows what pages have been updated.

How does that work ? Do we have to use WriteFile to update mapped memory ? If we use write() in linux does msync only syncs dirty pages ?

2

There are 2 answers

0
Harry Johnston On BEST ANSWER

On most (perhaps all) modern-day computers running either Linux or Windows, the CPU keeps track of dirty pages on the operating system's behalf. This information is stored in the page table.

(See, for example, section 4.8 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A and section 5.4.2 in the AMD64 Architecture Programmer's Manual, Volume 2.)

If that functionality isn't available on a particular CPU, an operating system could instead use page faults to detect the first write to a page, as described in datenwolf's answer.

4
datenwolf On

When flushing pages (i.e. cleaning them up) the OS internally removes the "writeable" flag. After that, when a program attempts to write to a memory location in such a page, the kernel's page fault handler is invoked. The page fault handler then sets the page access permissions to allow the actual write and marks the page dirty, then returns control to the program to let it perform the actual write.