VM-concepts Flashcards
What’s the difference between sequential access and random access?
Sequential access - accessing the elements in an ordered way: 1,2,3,…
Random access- accessing the elements in an unordereed way: 4, 8, 5, 1, 9,7,3,…
What’s the page cache?
The memory area that caches (synchronized) disk data
– Is called a “page cache”
Because DRAM is much faster
– OS caches disk content in DRAM
What happens when we write or read from disk?
If a process reads from disk
– Data brought to DRAM
– Stays there until OS decides
differently
If a process writes to disk
– First written to OS DRAM buffer
– Later sync-ed to disk
What’s the difference between a page and a frame?
– “Page” = chunk of contagious data (in virtual or physical space)
– “Frame” = physical memory exactly big enough to hold one page
– |page| = |frame| = 2^k (bytes)
– Typically, k = 12, namely a page (and frame) size is 4KB
- Pages & frames are always aligned on 2^k boundaries
What is the division of labor between HW and OS in VM implementation?
OS sets the VA=>PA mappings (“control path”)
• HW does on-the-fly translation from VA to PA (“data path”)
What can a PTE point to?
An address in DRAM or on disk or nothing
What happens on every Memory access?
If ( valid == 1 )
– Page is in main memory @ PA stored in table
– Data is readily available & can be used
• Else if ( page resides on disk ) // HW generates page fault (“major”)
– OS suspends process
– Fetches page from disk and adds VA=>PA mapping to page table
– Resumes process, which will re-execute faulting instruction
• Else if ( should be, but still isn’t allocated ) // page fault (“minor”)
– OS allocates pages and adds VA=>PA mapping to page talble
• Else if ( some other legal situations for which OS is responsible )
– Resolve (example: CoW)
• Else // process performed an illegal memory ref
– OS (which handles the page fault interrupts) delivers a signal
(SIGSEGV/SIGBUS) to the offending process
– The matching signal handler terminates process by default
Access Control – R=read-only, R/W=read/write, X=execute – If access type incompatible with specified access rights 1. protection violation fault 2. interrupt 3. resolve if OS’s fault (as in CoW), or deliver signal if process’s fault
What’s a major page fault? and what is the algorithm for handling?
Major => need to retrieve page from disk
- CPU detects the situation (valid bit = 0)
• It cannot remedy the situation on its own
– CPU doesn’t communicate with disks
– Moreover, CPU has no say regarding page table contents - CPU generates interrupt and transfers control to the OS
• Invoking the OS page-fault handler - OS regains control, realizes page is on disk, initiates I/O read ops
• To read missing page from disk to DRAM
• Possibly need to write victim page(s) to disk (if no room & dirty) - OS suspends process & context switches to another process
• It might take a few milliseconds for I/O ops to complete - Upon read completion, OS makes suspended process runnable again
• It’ll soon be chosen for execution - When process is resumed, faulting operation is re-executed
• Now it will succeed because the page is there
What’s a minor page fault? give examples
A page fault where we don’t need to go to disk in order to resolve it.
Examples:
1. CoW (copy-on-write)
2. Demand-based (lazy) memory allocation
3. Reading a file content that is already found in the “page cache”
• OS caches previously read files, possibly by other processes
• All read/write ops go through the page cache
What’s the difference between a page cache and the DRAM?
the page cache is just a portion of the DRAM.
for example a page might have been accessed from an earlier process, for our process it will be invalid page but it’s in the dram and not disk.
hence, in the page cache
What is the purpose of the mmap syscall?
Map a given file in a given virtual address in the process’s memory space.
A file becomes an “array of bytes” (backed by disk), and gets a VA
• With the right mmap() flags, reading/writing from/to the array
(more or less) translates to reading/writing from/to the file
A mmap()ed file is said to be a “memory-mapped file”
What are the pros of mmap?
- Can make programming easier
- Saves overhead of read/write system calls (user/kernel switches)
- Integrated with “page cache”…
• All disk load/store ops are cached in memory by “page cache”
• By default, all I/O goes through page cache, not directly to disk
• Mmap sets process’s PTEs to point to the page cache pages
– …So we get “zero copy” I/O
• Saves copying to/from user buffers
• May reduces memory pressure
What are Anonymous and named pages?
Anonymous pages
– Heap/stack pages; not mmap-ed files
Named pages
– Backed by a file (via mmap-ing)
Can an anonymous page be allocated with mmap?
Yes. by using the flag MAP_ANONYMOUS
What’s shared and private mmaping?
MAP_SHARED = changes affect the actual file (visible to other processes)
MAP_PRIVATE = changes don’t affect the actual file; rather, there will be
a copy-on-write for the process (other processes will not see changes)