1

How does Linux's Virtual Memory system handle the following case:

  1. Process 1 maps one of its virtual page to physical frame 1.
  2. Context switches to process 2.
  3. Process 2 swaps out frame 1 and replace with its own content.
  4. Context switches back to process 1.

How does process 1 knows that it does not own the content of frame 1 and needs to do a swap in?

migrated from stackoverflow.com Apr 16 '13 at 21:57

This question came from our site for professional and enthusiast programmers.

3

Technically, "process1" doesn't know this. There is a set of page-tables for each process. The kernel knows which process it will switch to, and switch the page-tables around. A part of these page-tables is the kernel, which is shared between ALL processes (so, a certain part of the address range, typically in the range of 0xc0000000 to 0xffffffff - although not ALL of those pages will be present). Typically, this shared section is achieved by simply making all the page-table sets point at the same shared section of "kernel page-table bits".

Having this shared section allows the kernel calls, traps and interrupts to go into the kernel, no matter which process is currently running, or what that process is doing.

Exactly how this works depends on the architecture the kernel is built for, but for example in x86(32 and 64 bit), CR3 is the register that points to the current page-table. So for each process, there is a pointer to the page-table, and it is loaded into CR3, as part of the switch to the new process, just like the register values (at least the stack-pointer) is restored for the process 1 when it is being switched in.

  • Thank you Mats! I still don't understand: If Process1 continues to use the corrupted frame (because no one updated Process1's page table when Process2 changes the frame content), it will crash but it doesn't. So I guess the OS has to maintain some information about the current owner of a frame, doesn't it? – cody Apr 14 '13 at 20:05
  • The page-table contains frames, and each frame belongs to one (or more) processes. So process 1 has it's unique set of pages. Frame 1 in process 1 is different from frame 1 in process 2 (unless we're sharing memory between the processes, in which case the page table points at the same physical piece of memory in both process 1's and process 2's pagetable) [Technically, virtual frame 1 is probably not accessible, as it's part of the "null pointer access protection"] – Mats Petersson Apr 14 '13 at 20:11
  • You are avoiding or can't understand what he is asking Mat. The question is if some page of Process 1 is evicted and if the corresponding frame is replaced with the content of some other page of Process 2, then how Process 1 will know that the frame that its using for evicted page is not valid? I mean how OS keep track of these evicted pages? – Muhammad Razib Mar 17 '14 at 20:23
  • Each process has it's own UNIQUE set of frames (page-table entries). There is no knowledge within process 1 of process 2's pages, and process 2 has no knowledge of process 1's pages. The virtual address X in process 1 lives on a different physical address than process 2's version of address X. In most processors, the difference is made by updating a register that holds the base of that process's pagetables. If that is not clear enough, then please phrase the question differently. (note that I don't frequent the Linux/Unix part of Stackexchage, so I probably won't see any new question). – Mats Petersson Mar 17 '14 at 21:32
1

The question is about the page frame conflict while context switching of two processes, not about virtual memory address space (virtual memory address space is unique to each process, that's not news.). This stack overflow link has the exact same question and precise answer. https://stackoverflow.com/questions/16581490/how-does-kernel-know-which-pages-in-the-virtual-address-space-correspond-to-a-s?lq=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.