09-SegmentationPaging
09-SegmentationPaging
• Paging
• Virtual Page and Page Frame
• Address Translation using Page Table
• Required Readings
• Chapter 16 – Segmentation
• http://pages.cs.wisc.edu/~remzi/OSTEP/vm-segmentation.pdf
• Section 17.1 & 17.3 of Chapter 17 Free-Space Management
• http://pages.cs.wisc.edu/~remzi/OSTEP/vm-freespace.pdf
• Chapter 18 – Paging: Introduction
• http://pages.cs.wisc.edu/~remzi/OSTEP/vm-paging.pdf
• Only allocate physical memory to memory segments that are in use; thus,
reduce internal fragmentation → avoid waste
• Assumption 2 (in Lecture 8 slide # 14) is no longer valid and Assumption 3 is not
relevant anymore
• Each virtual address v’ is in one segment only. Can we make use of the
information within a virtual address to determine which segment it
belongs to?
• A virtual address v’ can be divided into two parts
• Top few bits are for determining which segment this virtual address v’ is in
• Remaining bits are the offset, which points to the memory location relative to the
segment base address
10 base bounds
+ physical address = base + offset
decimal
number
Each process has a segment table (may or may
0
not be stored in MMU) that contains the address
Segment table translation information of all segments in the
process.
Principles of Operating Systems 8
Table Entry of Segment Table
Protection
bits
base bounds G R W E V
Grow Valid
• Protection bits direction bit
• Indicate whether or not a program can Read or Write to a segment, or even Execute code in
the segment
• If process tries to write to a read-only segment, CPU would raise an exception to alert OS to
deal with the offense
base bounds G R W E V
Grow Valid
direction bit
• Grow bit
• Indicate which direction the segment grows
• e.g., heap grows upwards, stack grows downwards
• Valid bit
• When the segment is not in used, valid bit should set to false
• If a process tries to access such memory, an exception will be generated
• Processes have multiple segments; when the process terminated, all segment B
physical memory used by its segments would be released
• During normal runtime, many processes arrive, use up physical memory, segment C
terminate and release physical memory
segment D
• So, where are those free slots? they appear in different places in the physical memory
and are changing over time
segment E
• Physical memory quickly becomes full of little holes of free space, which are segment F
not large enough to hold a segment
• However, the sum of the holes might large enough to accommodate some segment G
more segments
• Compaction
• Relocates all occupied areas of memory to one end
• Forms a single contiguous block free space at the other end
• Significant overhead
• Processes have to be suspended during relocation and change their segment register
values before resuming them
• Extensive memory copy involved
The Crux
What strategy can be used that is both fast and minimizes external
fragmentation?
• Example:
Freelist →[16KB]→[5KB]→[30KB]→[14KB]
• Leaves another “large” hole, making it more likely that another segment can fit in
the hole
• Quite significant performance overhead
• A full search of the list is required
• Example:
Freelist →[16KB]→[5KB]→[30KB]→[14KB]
• Paging
• The whole process address space is divided into fixed-size memory block,
called (virtual) page
• Physical memory is also divided into memory blocks of the same size of a
virtual page – we call it a page frame or simply a frame
• Advantages of Paging
• Simply, because all pages/frames are of the same size
• Completely avoid external fragmentation as each block is of the same size
• Any virtual page of any process can be placed in any free page frame
• Thus, a page table needs to store 220 = 1048576 entries of address translation information
for a 32-bit process
• Address Translation
• Given a virtual address v, split it into two parts
• The top left p bits , we call it the virtual page number, are for determining which
virtual page this virtual address is in
• Remaining bits on the right are the offset d, which points to the memory location
relative to the starting address of a page/frame
• How many virtual pages are there? And what is the size of a page?
• No. of virtual pages = 2p Size of a page = 2d bytes
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 0 1 0 0 0 0 1 1 1 0 0 0 1 1 0
Use p as an index
to page table
p’
40 Page frame no. + physical address = p’ 210 + d
decimal
number
shift left 10 bits
• Present bit
• Indicates whether this virtual page is in physical memory or on disk
• If present bit is false, the value stored in PFN may not be correct or may not refer
to a valid page frame
• CPU uses a register called Page-table base register to point to the starting physical
address of the page table
• OS must reload this register during context switch for next process
• In summary, we need to access physical memory twice for each memory reference
• No matter how smart the allocation policies, external fragmentation still exists.