1

I was reading:

While I totally understand what COW is, I don't get anything regarding Demand paging, how it's different thatn COW at all? I am seeing it as exact copy and implementation of COW with different name. Am I wrong?

1 Answers1

4

They're not the same.

Demand paging is a paging mechanism where you (the OS) only load in the required page into memory(RAM) at the exact time it's needed (during a page fault). This is opposite to doing a lot of prefetching, where you load several pages of memory before they're needed in expectation that they will be used in the future.

COW is copy-on-write, a method for saving memory writes. For instance, when a process forks, why do a complete copy of the parent memory? It takes up extra time and space. Why not instead do copy on write, meaning you let the two processes share memory, mark the memory as read only, then only copy a page when one of the processes wants to write to that page.

Demand paging is a virtual memory strategy. COW is an optimization for sharing memory.

wxz
  • 2,254
  • 1
  • 10
  • 31
  • But from what you explained COW uses Demand paging, I'm totally confused. –  Jul 21 '21 at 06:02
  • COW has nothing to do with demand paging. In what way does COW use demand paging? COW is about sharing memory that is already present in memory, like during a fork. Demand paging is about when to bring in memory from hard drive to RAM. – wxz Jul 21 '21 at 06:05
  • Can you give example of when demand paging is used? –  Jul 21 '21 at 07:11
  • I don't have a concrete example because it's a strategy, but here's the hypothetical. Imagine a system that starts out with no memory in RAM. With demand paging, upon the first memory access, we realize it's not in disk (a page fault), and we load in only the page that contains the data that we want. If the next memory access is in a page adjacent to the page we brought in, it doesn't matter, we still generate a page fault and load in only this new adjacent page. So demand paging is exactly that, only loading in memory when it's demanded, never before. – wxz Jul 21 '21 at 14:24
  • With prefetching, we could have saved ourselves the second page fault by loading in the adjacent page during the first page fault just by guessing that it was likely to be used in the future (which it was in my example). One of the few downsides of prefetching is that its a guess; sometimes you'll spend time loading extra memory you won't need. Demand paging guarantees you'll only ever load in memory that you explicitly access. – wxz Jul 21 '21 at 14:26