17. Swapping Flashcards
What two things happen when we run out of physical memory?
- Fail and either don’t load the process (exec()), don’t create a new process (fork()), refuse to allocate more heap (sbrk()), or kill the process if it is trying to allocate more stack.
- Create more space, preserving the contents of memory for later use.
What are the requirements for having the kernel remove memory from a process “behind its back”?
- The last time the process used the virtual address, it behaved like memory.
- The next time the process uses the virtual address, it behaves like memory.
- In between, whatever data was stored at that address must be preserved.
What is swapping?
The process of moving data back and forth from memory to disk in order to improve memory usage
What is the goal of swapping?
The system feels like it has memory that is as large as the size of the disk but as fast as actual RAM
When the OS goes to borrow memory from a process, what does it typically do with the data stored in that memory?
The OS moves the memory onto disk for safe keeping until the memory is given back to the process
What are the two kinds of memory-related faults we can expect to deal with?
TLB faults and page faults
What is a TLB fault?
A required virtual to physical address translation is not in the TLB
What is a page fault?
The contents of a virtual page are either not initialized or not in memory
What is the logical relationship between page fault occurrences and TLB fault occurrences?
Every page fault is preceded by a TLB fault, but not every TLB fault generates a page fault.
If the contents of the virtual page are not in memory, a translation CANNOT exist for it. By contrast, if a page is in memory and the translation is the page table, the TLB fault can be handled without generating a page fault.
What three things need to happen to swap a page to disk?
- Remove the translation from the TLB (if it exists).
- Copy the contents of the page to disk.
- Update the page table entry to indicate that the page is on disk.
Break down the three steps of swapping in terms of relative speed.
Removing the translation from the TLB is fast. Copying the contents of the page to disk is super slow. Updating the page table entry to indicate that the page is on disk is fast.
When must we swap in a page?
When the virtual address is used by the process.
How can we set up the swap out function to be faster?
Give each page a dedicated place on disk and then, during idle periods, have the OS write data from active memory pages to their space on disk.
Pages with matching data between active memory and disk are called clean, so they can be overwritten immediately.
Pages that do not match their swap disk content are called dirty and need to be saved to disk before being overwritten in memory.
Why must we remove the TLB entry first before swapping out?
Because if another process tries to access information at that virtual address, the TLB will happily map that process to physical memory, but that location is currently or about to be loaded with new information after the swap finishes. Remove the translation entry so no other process can grab data that is not currently there.
What 7 steps must we take to translate a virtual address used by a process (that points to a page that has been swapped out)?
- Stop the instruction that is trying to translate the address until we can retrieve the contents.
- Allocate a page in memory to hold the new page contents.
- Locate the page on disk using the page table entry
- Copy the contents of the page from disk.
- Update the page table entry to indicate that the page is in memory.
- Load the TLB.
- Restart the instruction that was addressing the virtual address we retrieved.