Introduction
π‘ Virtual Memory
- Programmer's view of memory
- Each process has its own virtual memory
- A linear array of bytes addressed by the virtual address
βοΈ κ°μ λ©λͺ¨λ¦¬λ₯Ό μ μ¬μ©νλκ°?
- νν°μ
ν¬κΈ°μ λ°λΌ overlay μμ μ ν νμμμ΄ OSκ° μ±
μμ Έμ€μ νλ‘κ·Έλλ°μ νκΈ° μ½λ€.
- λ€ μ¬λΌμ¬ νμμμ΄ νλ‘μΈμ€ μ΄λ―Έμ§μ μΌλΆλ§ μ¬λΌμμ μ€ννλ€.
- κ°μ λ©λͺ¨λ¦¬λ μΆ©λΆν ν¬κ² μ‘νμ Έμμ΄μ νλ‘κ·Έλλ¨Έλ λ©λͺ¨λ¦¬κ° (κ±°μ) 무ννλ€κ³ μκ°ν μ μλ€.
π‘ Physical Memory
- Machine's physical memory (DRAM)
- Caches are parts of physical memory
- Also called main memory
βοΈ Virtual address : The address of a program λͺ
λ Ήμ΄λ λ°μ΄ν°μ μ£Όμ
βοΈ Physical address : The address of a DRAM
βοΈ Page table : OS κ° κ΄λ¦¬νλ λ°μ΄ν° ꡬ쑰
βοΈ TLB (MMU) : Virtual page number λ₯Ό Physical Page Number λ‘ λ°κΎΈλ κ²μ΄ Translate μ΄λΌκ³ νλ€. Page table μ entry λ€μ Caching ν νλμ¨μ΄. νΉμν entry λ₯Ό μ μΈνκ³ λ λκ° μ»¨ν
μ€νΈ μ€μμΉλ λλ§λ€ flush λμ΄μΌ νλ€. μ΄κ²μ΄ μλ€λ©΄ λ§€λ² CPU κ° exception μ΄ κ±Έλ €μ OS κ° μ°ΎμμμΌνλ€.. λ무 λλ €μ§λ€!
Virtual Memory
Functions
- Large address space
- Easy to program
- Provide the illusion of infinite amount of memory
- Program (including both code and data) can exceed the main memory capacity
- Processes partially resident in memory
- Protection
- Access rights: read modify/ execute permission
- Each segment/page has its own access rights
- Privilege level
- Sharing
- Portability
- Increased CPU utilization
- More programs can run at the same time
Require the following functions
- Memory allocation (Placement)
- Any virtual page can be placed in any page frame
- Memory deallocation (Replacement)
- Memory mapping (Translation)
- Virtual address must be translated to physical address to access main memory and caches
memory management
- Automatic movement of data between main memory and secondary storage
- Done by OS with the help of processor HW (TLB)
- Main memory contains only the most frequently used portions of a process's address space
- Illusion of infinite memory(size of secondary storage) but access time is equal to main memory
- Usually use demand paging (cache λ λμΉλΉ λ₯΄κ² PreFetching)
Paging
-
Divide address space into fixed size pages
- VA consists of VPN, offset
- PA consists of PPN, offset
-
Map a virtual page to a physical page frame at runtime
-
Each process has its own page table
- The page table contains mapping between VPN and PPN
- VPN is used as an index into the page table
-
Page Table Entry(PTN) contains
- PPN (Physical Page Number or Frame Number)
- Presence bit : 1 if this page is in main memory
- Modified bit : 1 if this page has been modified
- Reference bits : reference statistics into used for page replacement
- Access control : read/write/execute permissions
- Privilege level : user-level page versus system-level page
- Disk Address
Page table organization
-
Linear : one PTE per virtual page
-
Hierarchical : tree structured page table
- Page Directory π Page Table π Virtual Memory
-
Inverted : PTEs for only pages in main memory νλ‘μΈμ€ μμ μκ΄μμ΄ λ©λͺ¨λ¦¬μ μ¬λΌμμλ νμ΄μ§μ λν΄μλ§ κ΄λ¦¬νκ² λ€. frame μ λ°λΌ κ΄λ¦¬νκ² λ€.
- Hashing μΌλ‘ νμ΄μ§μ νλ μμ μ§μ§μ΄μ€λ€. Hash collision μ΄ λλ©΄ chain μΌλ‘ κ·Έ λ€μ κ²μ μ°κ²°νλ€
-
Page table entries are dynamically allocated as needed
TLB (MMU; Memory Management Unit)
TLB λ μΊμμ΄λ―λ‘ TLB miss, hit μ΄ μλ€
μ΄κ²μ΄ CPU λ΄μ μμ΄μΌλ§ κ°μλ©λͺ¨λ¦¬λ₯Ό μ§μν μ μλ€
βοΈ Translation Lookaside Buffer
- Cache of page table entries (PTEs)
- On TLB hit, can do virtual to physical translation without accessing the page table
- On TLB miss (Exception), must search the page table for the missing entry
Page fault
π‘ But what happens if the process tries to access a page that was not brought into memory? Access to a page marked invalid causes a page fault.
The paging hardware, in translating the address through the page table, will notice that the invalid bit is set, causing a trap to the operating system. This trap is the result of the operating systemβs failure to bring the desired page into memory. The procedure for handling this page fault is straightforward (Figure 10.5):
- We check an internal table (usually kept with the process control block) for this process to determine whether the reference was a valid or an invalid memory access.
- If the reference was invalid, we terminate the process. If it was valid but we have not yet brought in that page, we now page it in.
- We find a free frame (by taking one from the free-frame list, for example)
- We schedule a secondary storage operation to read the desired page into
the newly allocated frame.
- When the storage read is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory.
- We restart the instruction that was interrupted by the trap. The process can now access the page as though it had always been in memory.
Page Size
컀μ§λ μΆμΈλ€. κ°μ Έμ€λ μ€λ²ν€λλ₯Ό μ€μ΄κΈ° μν΄μ.
- The smaller the page size, the lesser the amount of internal fragmnentation
- However, more pages are required per process
- More pages per process means larger page tables
- For large programs, portion of the page tables of active processes must be in secondary storage instead of main memory
Segmentation
βοΈ Segmentation allows a programmer to view a virtual memory as a collection of sgments
βοΈ Advantages
- Simplify the handling of growing data structures
- Facilitate sharing among processes
βοΈ Segment Table Entry Contains
- Base address of the corresponding segment in main memory (DRAM μμ μμνλ μ£Όμ)
- Length of the segment (λ²μ)
- Presence bit - 1 if this segment is in main memory
- Modified bit - 1 if this segment has been modified
Virtual Memory Policies
π‘ Key issues: Performance
Fetch Policy
βοΈ Demand paging
- Bring a page into main memory only on a page miss
- Generate many page faults when process is first started
- Principle of locality suggests that as more and more pages are brought in, most future references will be to pages that have recently been brought in. and page fault should drop to a very low level
βοΈ Prepaging
- Pages other than the one demanded by a page fault are brought in
- If pages of a process are stored contiquously in secondary memory it is more efficient to bring in a number of pages at one time
- Ineffective if extra pages are not referenced
Frame Locking
βοΈ Frame Locking
- When a frame is locked, the page currently stored in that frame should not be replaced
- OS kernel and key control structures are locked
- I/O buffers and time-critical areas may be locked
- Locking is achieved by associating a lock bit with each frame
Replacement Algorithms
βοΈ Optimal
- Select the page for which the time to the next reference is the longest
βοΈ LRU
- Select the page that has not been referenced for the longest time
βοΈ FIFO
- Page that has been in memory the longest is replaced
βοΈ Clock
- Associate a use bit with each frames
- When a page is first loaded or referenced, the use bit is set to
- Any frame with a use bit of 1 is passed over by the algorithm
- Page frames visualized as laid out in a circle
Page Buffering
βοΈ When a page is replaced, the page is not physically moved
- Instead, the PTE for this page is removed and placed in either the free or modified page list
- Free page list is a list of page frames available for reading in pages
- When a page is to be replaced, it remains in memory and its page frame is added to the tail of the free page list
- Thus, if the process reference the page, it is returned to the resident set of that process at little cost
- When a page is to be read in, the page frame at the head of the list is used. destroying the page that was there
- Modified page list is a list of page frames that have been modified
- When a modified is replaced, the page frame is added to the tail of the modified page list
- Modified pages are written out in clusters rather than one at a timer significantly reducing the number of I/O operation
Page Fault Frequency (PFF)
PF κ° λ무 μμ£Ό λ°μνλ©΄ working set μ λλ €μ£Όκ³ PF κ° λ무 κ°λ λ°μνλ©΄ working set μ μ€μ¬μ€λ€.
βοΈ Requires a use bit to be associated with each page in memory
- Bit is set to 1 when that page is accessed
- When a page fault occurs, the OS notes the virtual time since the last page fault for that process
- If the amount of time since the last page fault is less than a threshold (μκ³μ ), then a page is added to the working set of the process
- Otherwise, discard all pages with a use bit of 0, and shrink the working set accordingly. At the same time reset the use bit on the remaining pages
- The strategy can be refined by using 2 thresholds: An upper threshold is used to trigger a growth in the working size while a lower threshold is used to trigger a shrink in the working set size
βοΈ Does not perform well during the transient periods when there is a shift to a new locality
Multiprogramming
process μκ° λ무 λ§κ±°λ μ μΌλ©΄ λ¬Έμ λ€!
νλμ process μκ° λ무 λ§μΌλ©΄ working set μ΄ μ€μ΄λ λ€. π ν process μμμ page fault κ° λ°λ³΅λλ€ π thrashing
βοΈ Load Control
- Determine the number of processes that will be resident in main memory
- This number is called multiprogramming level
βοΈ Too few processes lead to swapping
- Since all the processes can be blocked
βοΈ Too many processes, lead to insufficient working set size, resulting in thrashing (frequent faults)
Process Suspension
βοΈ If the degree of multiprogramming is to be reduced,
- One or more of the currently resident processes must be swapped out
βοΈ Six Possibilities
- Lowest-priority process
- Faulting process
- Last process activated
- Process with the smallest working set
- Largest process
- Process with the largest remaining execution window
π Reference
[KUOCW] μ΅λ¦° κ΅μλμ μ΄μ체μ κ°μλ₯Ό μκ°νκ³ μ 리ν λ΄μ©μ
λλ€. μλͺ»λ λ΄μ©μ΄ μλ€λ©΄ λκΈλ‘ μλ €μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€ π