How Copy-on-Write Works with Memory-Mapped Files
In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just like demand paging, CoW is the kernel’s internal mechanism with implications for the performance of user-space systems. It enables multiple processes to share data in RAM between them in read-only mode. The interesting bit is that the processes themselves are not aware of this sharing, as far as they are concerned they are executing as if they are the only ones working with that data. Of course, this works as long as the processes are reading the data. When one of them needs to do a write to this shared data, the kernel needs to make a copy before the write can happen, hence the name “copy-on-write”!
This is a very wide and deep topic, so I’m going to split into multiple videos. This first video goes deep inside the kernel to explain what CoW is, how the kernel implements it, and for that we will take the example of mmap to read and write files.
Following are some of the major sections in the video with the timestamps to help you navigate. Also, I recommend watching the video at higher speed to get a better experience.
- 00:00 — Why CoW matters: memory use, page faults, and unpredictable latency in data-intensive applications.
- (06:34) The page-table picture: how different processes can map the same physical frame.
- (11:44) CoW in one diagram: share a page while reading; make a private copy when writing.
- (15:06) Mapping a file with
mmap: the call’s arguments, includingMAP_PRIVATE. - (21:31) What
mmapcreates: a virtual address range and VMA, before the file page is maps into the process. - (25:03) The first read: address translation, a page fault, and how the kernel resolves it.
- (32:25) The page cache: where file data is held in RAM and why another process can reuse it.
- (38:43) A second process maps the file: its own page fault leads to the same cached physical page.
- (43:11) A private write: why writing to that shared file page would violate
MAP_PRIVATE. - (45:51) Write protection and CoW: how a read-only PTE causes a write fault and the kernel gives the writer a private copy.
- (49:07) What happens next: the second process can also get a copy; later writes to an already private page proceed without another CoW fault.
A minor correction note: At about 46 minutes, when I say mappings of page-cache pages are read-only, I mean the MAP_PRIVATE mappings in this example. A writable MAP_SHARED mapping can modify a cached file page, which is later written back to the file.
If you are new to this series, it is based on my ebook called “Virtual Memory from First Principles”. It is available to read for free online and also available to purchase from Gumroad (PDF/Epub) and Amazon (Kindle edition).
And, if you want to watch the previous videos in this series, the following is what has been published so far: