CS124 Lec 18
CS124 Lec 18
Swapping
• With swapping, the total memory used by all processes
can exceed the total physical memory in the system
• Allows more programs to be run on the system at once, especially
if many of these programs are usually blocked on I/O
• (e.g. user applications waiting for user input)
• Unfortunately, swapping tends to take a lot of time
• Even though the backing store is usually a fast disk, still much
slower than main memory
• Standard swapping involves moving entire processes
into or out of physical memory
• For a large process, can easily take several seconds or more!
• Operating systems don’t generally use standard swapping
• Instead, focus on swapping portions of processes out of memory
• Much faster than swapping entire processes out of memory
10
Swapping (2)
• Mobile processors generally have virtual memory support
• Mobile OSes usually don’t implement swapping
• Don’t have a large backing store to use for swapping
• Usually have a small flash memory with a limited number of writes
• Generally, when OS needs more memory for a process, it
asks (or forces) other processes to relinquish memory
• e.g. if a process is taking up too much memory, the OS kills it
• iOS tends to be aggressive in reclaiming memory from processes
• Android will write application state to flash memory before
killing a process, so that it can be restarted quickly
• In general, mobile application developers must be more
careful about efficient memory (and other resource) usage
11
MMU: Relocation
• A simple strategy for the memory management unit:
relocate all virtual addresses by a constant amount
• A relocation register holds a constant, which is added to logical
addresses to generate physical addresses
• phys_addr = virt_addr + relocation
• Additionally, can use a limit register to enforce the upper
bound on the process’ virtual address space
if virt_addr ≥ limit:
raise fault
else:
phys_addr = virt_addr + relocation
• Interaction with these registers is protected: only the
kernel is allowed to read and write these registers
12
MMU: Segmentation
• A more advanced virtual address mapping technique is
called segmentation
• Virtual addresses also include a segment number
• Virtual address = segment number + offset within segment
• Segment number used to find an entry in segment table
• Similar mechanism to the relocation register:
• Virtual offset is checked against the segment’s limit; if limit is
exceeded, MMU generates a fault
• Otherwise, virtual address is added to the segment’s base value to
get a physical address limit base Segment
• Each process has its own limit base Table
limit base
segment table limit base
• Only manipulated by kernel … …
Compaction
• Both relocation register and segmented memory models
can suffer from external fragmentation of physical memory
• OS can mitigate this by compacting physical memory:
• Move programs within physical memory to create a single
contiguous area of free memory
• A program’s code and data can be moved within physical memory,
then the base address(es) can be adjusted to reflect new location
• Increases the number of processes that a system can run
• Clearly has a time impact on system performance
• Particularly when large programs or data areas must be moved
• OS can perform compaction when system load is lighter
• e.g. via a low-priority kernel thread
• Or, just force compaction when it can’t be avoided!
18
MMU: Paging
• Paging is the most common technique for mapping virtual
addresses to physical addresses
• Physical and virtual memory are divided into fixed-size blocks of a
particular size, e.g. 4KB, 8KB, etc.
• Blocks of physical memory are called frames
• Blocks of virtual memory are called pages
• Every virtual page is mapped to a corresponding frame in
physical memory
m-1 p p-1 0
Physical Address Physical Page Number Physical Page Offset
24
Hierarchical Paging
• To support larger address spaces, many systems use
hierarchical paging
• Page table is a sparse data structure
• Virtual page number is broken into parts – each part is
used to index a page table at a different level
• If a memory area is unused, the corresponding page table
entries are empty
n-1 Virtual Address p p-1 0
VPN 1 VPN 2 … VPN k Virtual Page Offset
… … …
31 22 21 12 11 0
Virtual Address Page Directory Entry Page Table Entry Virtual Page Offset
… …
31 12 11 0
Physical Address Physical Page Number Physical Page Offset
26
… …
31 12 11 0
Physical Address Physical Page Number Physical Page Offset
27
Next Time
• Continue discussion of virtual memory and paging