09 SegmentationPaging
09 SegmentationPaging
¤ External Fragmentation
¤ Paging
¤ Virtual Page and Page Frame
¤ Protection
if offset > segment_bounds then generate segmentation fault
Principles of Operating Systems 7
¤ Given a virtual address v’, which segment is this address in?
¤ We need to know which segment this virtual address is in so as to find
the corresponding base-and-bounds pair
segment id offset
No. of segments Max. size of each segment
= 24==??
16 = 212 = =
4096
?? bytes
10 base bounds
+ physical address = base + offset
decimal
number
Each process has a segment table (may or
0
may not be built in MMU) that contains the
Segment table address translation information of all
segments in the process.
Principles of Operating Systems 9
Protection
bits
base bounds G R W E V
Grow Valid
direction bit
¤ Protection bits
¤ 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 use, valid bit should set to false
¤ If a process tries to access such memory, an exception will be
generated
¤ 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]
¤ 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
¤ 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
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 the next process