Virtual Memory
What are three algorithms that can be used to find an allocation space? Why would anyone ever want to use the worst fit algorithm?
*Best-fit* - Traverse entire physical address space for the smallest unallocated space that can fit. *Worst-fit* - Find the largest unallocated space in the physical address space. *First-fit* - Find the first block of unallocated physical space that can fit. Guess: Worst-fit may be used store store something that may be expanded?
In practice, frequency based page replacement policies don't work very well. Why is this?
- Not all memory accesses can be accounted for. A TLB hit on a hardware-controlled TLB cannot be seen by the kernel, so it's reference count cannot be incremented by the kernel. - Also there is may not be hardware support to record references. - Old references are not forgotten - Freshly loaded pages may be victims of replacement as they start with low reference counts
A range addresses starting at 0x00000000 is intentionally left empty. Why is this?
0x00000000 is the null pointer
How large are the pages in OS/161?
4KB = 2^12 bytes = 2^10 + 2^2 bytes
What is the difference between a hardware-controlled TLB and a software-controlled TLB? Which is the MIPS TLB?
A hardware-controlled TLB is done by the MMU and a software-controlled TLB is done by the kernel. The MIPS TLB is a software controlled TLB with 64 entries
How do you translate a segmented and paged virtual address into a physical address? In reverse?
A segmented and paged virtual address has 4 bits for the segment number, some bits for the page number, and some bits for the offset. To get the page number we divide the virtual address by the page size. From the segment number we lookup the segment in the segment table and check if access is permitted. For example, writing to a read-only segment is disallowed. From the segment table, we lookup the associated page table, and from the page table we lookup the page number and from that we get frame number. Then the physical address is frame number + offset.
What is a simple method of address translation? How does this work? What are its disadvantages?
A simple method of address translation is the use of dynamic relocation. Dynamic relocation works by having a relocation register for each process' address space and adding the value of it's relocation register to the address space location at runtime. Each process has it's own relocation register and is reset on each address space. The disadvantage of address translation is that it can cause external fragmentation - wasted unallocated space. Note: The relocation register is included in the memory management unit (MMU).
Give 3 advantages and 2 disadvantages of large page sizes.
Advantages - Smaller page tables - Larger TLB footprint - More efficient I/O Disadvantages - More internal fragmentation - Higher chances of paging unnecessary data
Describe the different parts of an ELF file. Why doesn't the ELF file describe the stack?
An ELF file contains descriptions of each address space segment and the virtual address of the program's first instruction. ELF files do not describe the stack because the stack is created at runtime.
What functions/system calls can be used to share virtual memory?
Create: shmid = shmget(key, size) Attach: vaddr = shmat(shmid, vaddr) Detatch: shmdt(vaddr) Delete: shmctl(shmid, IPC_RMID)
Given a virtual address and a page table, how do you calculate its physical address? In reverse?
Divide the virtual address by the page size to get the page number. Get the frame number in the page table by looking up the page number. Add the offset and frame number to get the physical address. Calculating the virtual address from the physical address is the opposite process: Subtract the offset from the physical address to get the frame number. Lookup the frame number in the page table to get the page number. Multiply the page number by the page size to get the virtual address.
What are the different sections in an ELF file? What are the different segments?
ELF file sections: .text: program code .rodata: read-only global data .data: initialized global data .bss: uninitialized global data .sbss: small uninitialized global data ELF Segments: Text segment: .text and .rodata sections Data segment: .data, .bss, and .sbss sections
Describe how the OS uses the valid bit to determine which PTEs (Page table entries) contain a valid page mapping.
Each page table entry (PTE) contains a valid bit which indicates if the entry contains a valid page mapping, which is used during address translation to ensure that the process only uses a valid virtual address.
How does the kernel use a page table to help translate virtual addresses to physical addresses?
Each process has a page table which specifies the frame in physical memory where the process's page is located.
What are the 6 different page replacement policies discussed in lecture? Describe and compare them.
FIFO - Replace the page that has been in memory the longest (Practical) OPTIMAL - Replace the page that will not be referenced for the longest time - requires knowledge of the future (Impossible) LFU - Replace the page that is the least frequently used by having each page have a reference counter (Impractical) LRU - Replace the page that has not been used for the longest time using a list to keep track of pages in memory, with the most recently used page at the front of the list (Impractical) CLOCK - Like FIFO but skips pages that are used. Replace the next page with the use bit set as false, pointed to by a victim pointer that cycles through the page frames. (Practical)
What are the key differences between normal and inverted page tables?
Inverted Page Table - Maps physical frames to virtual pages - Only one table for kernel Entries also include PID (Process Identifier) - Only shows locations of virtual addresses in memory, another mechanism is needed to find pages not in memory Normal Page Table - Maps virtual pages to physical frames One table per process - Can be used to find pages not in memory
Describe the mapping of different types of variables (local variables, initialized global variables, uninitialized global primitive variables, uninitialized large variables) to their section in an ELF file.
Local variables are allocated on the stack at runtime. Initialized global variables belong to the .data section. Uninitialized global primitive variables belong to the .sbss section. Uninitialized global large variables such as arrays, belong to the .bss section.
Why are modified pages more expensive to replace than a clean page?
Modified pages must be written to secondary storage when being replaced, whereas a clean page does not since it can just be loaded from the ELF file.
Page tables for large address spaces may be very large. One problem is that they must be in memory, and must be physically contiguous. What are two solutions to this problem?
Multi-level page tables (paging the page tables) or inverted page tables could be used for large address spaces where page tables need to be in memory and physically contiguous.
LRU page replacement is considered impractical in virtual memory systems. Why is this?
Not very practical to make the MMU keep track of accesses. Another problem is that if an item in memory is accessed repeatedly within a short period of time but then not accessed for a long time afterwards, other pages that are accessed more frequently over the long-term would more likely be replaced.
Define page cleaning, and explain the difference between synchronous and asynchronous page cleaning.
Page cleaning means to copy a modified page to secondary storage. Synchronous Cleaning - Clean the page when it is being replaced. Asynchronous Cleaning - Clean the page before it is replaced (done by page sweeper threads that clean modified pages)
What advantages does paging give over dynamic relocation?
Paging allows virtual address spaces to be split into fixed-size chunks so they do not have to be physically contiguous, which means that there is no external fragmentation. On the other hand, this allows there to be internal fragmentation of physical memory.
How does segmentation improve address translation? What is the motivation behind segmentation?
Segmentation improves address translation since each segment is treated as a separate virtual address space and can grow (or shrink) independently of the other segments, each with their own attributes. The motivation behind segmentation is to group memory addresses for code, data, stack, etc as these types of memory addresses have different attributes.
Why is sharing virtual memory useful? How can this be accomplished? Give an example of when two processes should share a page.
Shared virtual memory is useful as it allows physical memory to be used more efficiently. For example, a copy of a program's virtual memory can be shared by multiple processes. If a process is forked, then the read-only segment can be shared instead of having two copies of it in physical memory on a multi-processor architecture. It is also a method of interprocess communication.
What are the two types of locality to consider when designing a page replacement policy?
Temporal Locality - Recently accessed pages will be accessed again. Spatial Locality - Pages "close" to the page accessed are likely to be accessed next.
Does the MIPS TLB include a use bit? A modified bit?
The MIPS TLB does not have a use bit or a modified bit.
What steps must the OS take to handle a page fault exception?
The OS must determine which page table entry caused the fault, load the page into memory, set the valid bit, and then retry the instruction.
What is the TLB? What does it stand for?
The TLB is a fast, associative cache which contains a (page #, frame #) pair. The TLB stands for Translation Lookaside Buffer.
Describe the Working Set Model, working set, and resident set.
The Working Set Model says that at any given time some part of a program's address space will be heavily used (called the Working Set) while the rest of it will not. The Resident Set is the set of pages in memory. If a process' Residence Set includes its Working Set, there will be fewer page faults.
What is the difference between external fragmentation and internal fragmentation?
The difference between external fragmentation and internal fragmentation is that external fragmentation is wasted unallocated space between contiguous chunks of physical memory. Internal fragmentation is wasted unallocated space between non-contiguous physical chunks of memory.
In address translation, what (4) roles does the OS serve? What (2) roles does the MMU serve?
The kernel manages the MMU state when address spaces are switched (e.g. during a context switch from one process' thread to another process' thread). It also creates and manages the page tables, allocates and deallocates physical memory, and handles exceptions raised by the MMU. The MMU translates virtual addresses to physical addresses and checks for invalid entries and raises exceptions when necessary.
In dynamic relocation, when does the value of the relocation register in the MMU change?
The relocation register value changes for each process' address space.
Why is it not a good idea to save/restore the TLB on context switches?
The virtual address might not point to the same physical address. Also, on a context-switch, the thread that was switched to may belong to a different process than the previous thread, which would mean a switch of address spaces. Since address spaces have different page table entries, the TLB entries will differ as well. Therefore, TLB entries cannot be saved during a context switch.
A system uses physical address and virtual addresses. How many physical address spaces are there in a computer? How many virtual address spaces?
There is only one physical address per computer, and one virtual address per process.
What is thrashing and what are two solutions to it?
Thrashing is when the system is spending too much time paging, which can occur when too many processes are competing for memory. To solve thrashing, the kernel could kill processes (probably not such a great idea) or suspend and swap out processes (better way).
What are three approaches for locating the kernel's address space? How do OS/161 and Linux do this?
Three possible approaches are: - Having the kernel address space in physical space where we disable address translation in privileged system execution mode and re-enable it in privileged mode. - Having the kernel address space in a separate virtual address space. We would need a way to change address translation when switching from privileged mode to unprivileged mode. - Having the kernel mapped into a protected portion of every process' address space to isolate the kernel from applications. This is used in OS161 and Linux. This makes it easy for the kernel to have access to application virtual addresses, for example when copying system call parameters from a user program's address space to the kernel's address space.
Describe how you would simulate a modified bit in software
To simulate a modified bit, a page is marked as READ-ONLY with the modified bit set as false when the page is first loaded into memory. When the page is accessed again and if the page is supposed to be writable (determined by which segment the page is in) then set the modified bit to true and mark the page as WRITABLE. Requires the OS to maintain 2 extra bits: modified bit and writable bit.
Why is simulating a use bit expensive?
To simulate a use bit, a page is marked as INVALID with the use bit set as false when first loaded into memory. Then when the page is accessed again, another exception will occur, and the use bit must be set to true and the page must be marked as VALID. Simulating a use bit requires each loaded page to cause another exception and the OS to maintain a "use bit" and "in-memory" bit for every page, which is very expensive.
Why might the MMU need to invalidate the TLB? When would this happen?
When a TLB miss occurs, and the TLB is full, the TLB must be invalidated so that a new TLB entry can be written to the TLB.
What happens when a process violates a protection rule checked by the MMU?
When a process violates a MMU protection rule, it raises an exception which is handled by the kernel.