0% found this document useful (0 votes)
2 views

09-SegmentationPaging

Uploaded by

Karina Nathalie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

09-SegmentationPaging

Uploaded by

Karina Nathalie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Mechanisms:

Segmentation & Paging


Principles of Operating Systems
2024/25 COMP3230B 1
Contents
• Segmentation
• Address Translation
• Protection and Sharing of Memory
• External Fragmentation
• Strategies in Managing Free Space
• Best-fit, Worst-fit, First-fit, & Next-fit

• Paging
• Virtual Page and Page Frame
• Address Translation using Page Table

Principles of Operating Systems 2


Related Learning Outcomes

• ILO 2b - describe the principles and techniques used by OS in


effectively virtualizing memory resources.

• ILO 3 [Performance] - analyze and evaluate the algorithms of . . .


and explain the major performance issues . . .

Principles of Operating Systems 3


Readings & References

• 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

Principles of Operating Systems 4


Segmentations
• A segment is a block of contiguous virtual addresses of a particular length
within a process’s address space, which is for a specific purpose/usage
• e.g., text segment, data segment, heap segment, and stack segment

• OS can place each segment of a process in different parts of physical memory

• 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

Principles of Operating Systems 5


Hardware Support
• To support segmentation, MMU needs to know how to
translate multiple logical segments within a process’s
address space
• Multiple base-and-bounds register pairs
• Each base-and-bounds pair for the address translation of one
logical segment
• Address Translation of a virtual address v’
v’
start

offset = virtual address v’ – starting virtual address of the segment


physical address = segment_base + offset Starting physical address of
the segment

• Protection Upper limit of the


segment

if offset > segment_bounds then generate segmentation fault


Principles of Operating Systems 6
Operational Issue
• 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

• 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

Principles of Operating Systems 7


Address Translation
Assume 16-bit virtual address 41414, the system uses top 4 bits to select the segment
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
No. of segments in
a process segment id offset
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 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

• Sharing of memory segment between processes


• e.g., code sharing
• By appropriate setting of protection bits, OS maps the same physical segment into different
logical segments of different process address spaces.

Principles of Operating Systems 9


Table Entry of Segment Table
Protection
bits

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

Principles of Operating Systems 10


External Fragmentation
• An issue arises from allocating memory to segments of different
sizes OS

• OS needs to find suitable space (block) in physical memory and allocates to


new segments or to grow existing segments
segment A

• 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

Principles of Operating Systems 11


External Fragmentation
• Possible ways to combat external fragmentation
• Coalescing
• OS maintains a list of free memory blocks
• When a process releases a segment, OS tries to see whether there are adjacent
free blocks (i.e., they are in consecutive memory locations)
• If so, combine adjacent free blocks into one large block

• 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

Principles of Operating Systems 12


Managing Free Space
 OS needs to allocate physical memory to individual segment; thus,
OS needs to know where to find free memory

 A straightforward approach is to use a free-list


 Contains references to all of the free chunks of space in physical memory
 OS just needs to search through the list to locate a suitable free block and
allocates to a new request

 The Crux
 What strategy can be used that is both fast and minimizes external
fragmentation?

Principles of Operating Systems 13


Allocation Strategy – Best Fit
• Best-fit
• Search the free list and place the segment in the smallest possible memory block in which it will
fit

• Example: Freelist →[16KB]→[5KB]→[30KB]→[14KB]


A new request for placing a segment of size 13KB

After allocation, the freelist becomes


Freelist →[16KB]→[5KB]→[30KB]→[1KB]

• By allocating a “just fit” block, we try to reduce wasted space


• However, still leave small amount of unused space
• On long run, it too suffers with problem of numerous unusable small free areas
• Quite significant performance overhead
• An exhaustive search for the best suit free block

Principles of Operating Systems 14


Allocation Strategy – Worst Fit
• Worst-fit
• Place the segment in the largest available memory block in which it will fit

• Example:
Freelist →[16KB]→[5KB]→[30KB]→[14KB]

After allocation, the freelist becomes


Freelist →[16KB]→[5KB]→[17KB]→[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

Principles of Operating Systems 15


Allocation Strategy – First Fit
• First-fit
• Place the segment in the first memory block (relative to the list head) in the free
list in which it will fit

• Example:
Freelist →[16KB]→[5KB]→[30KB]→[14KB]

After allocation, the freelist becomes


Freelist →[3KB]→[5KB]→[30KB]→[14KB]

• Simple, low execution-time overhead


• This technique may create many small holes at the beginning of the free list

Principles of Operating Systems 16


Allocation Strategy – Next Fit
• Next-fit
• Similar to first-fit
• Instead, it searches the free list at the point where the last search was stopped at

• Example: Freelist →[16KB]→[5KB]→[30KB]→[14KB]

After allocation, the freelist becomes


Freelist →[16KB]→[5KB]→[17KB]→[14KB]

• Somewhat surprisingly, best fit results in more wasted memory than


first fit and next fit because it tends to fill up memory with tiny, useless
holes. First fit generates larger holes on the average.

Principles of Operating Systems 17


Fixed-Size Blocks

• The only real solution to combat external fragmentation is to avoid


the problem altogether, which is by never allocating physical
memory in variable-size blocks

• 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

Principles of Operating Systems 18


Paging

• 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

• This also simplifies the free-space management

Principles of Operating Systems 19


Address Translation – Page Table
• Also known as page map table

• Each process has its page table


• Contains the address translation information of every virtual page of a process

• Let’s have the calculation


• With 32-bit x86 processor, a process address space is of size 232 bytes = 4294967296 bytes =
4 GiB
• The default size of a virtual page (as well as page frame) is 212 bytes = 4096 bytes = 4 KiB
• How many virtual pages are there in one 32-bit address space?
• 232/212 = 220 virtual pages

• Thus, a page table needs to store 220 = 1048576 entries of address translation information
for a 32-bit process

Principles of Operating Systems 20


Address Translation
• Given a virtual address v, how to look up the translation information in
the process’s page table?
• We need to find out which virtual page this virtual address is in

• 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

Principles of Operating Systems 21


Address Translation
• Assume 16-bit addressing and the page size is 1024 bytes = 210
• No. of virtual pages = 2(16-10) = 64 pages
• Given a virtual address 41414, we can determine its virtual page
number p and the offset d in this virtual page

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

virtual page number p Offset d

Principles of Operating Systems 22


Address Translation
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

virtual page number p Offset d

Use p as an index
to page table

p’
40 Page frame no. + physical address = p’  210 + d

decimal
number
shift left 10 bits

0 Look up page p in the process's page table and find that


Page table page p is in page frame number p'

Principles of Operating Systems 23


Page Table Entry (PTE)
Dirty Protection
bit bits

Page Frame Number R D P R W E V


Reference Present Valid
• Valid bit bit bit bit

• Indicates whether this PTE contains valid translation information


• e.g., if this virtual page is unused, valid bit is set to false
• Accessing an invalid page will generate an exception

• 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

Principles of Operating Systems 24


Page Table Entry (PTE)
Dirty Protection
bit bits

Page Frame Number R D P R W E V


Reference Present Valid
bit bit bit
• Dirty bit
• Indicates whether this virtual page has been modified since it was brought into
memory

• Reference (or accessed or use) bit


• Indicates whether this virtual page has been recently accessed by the process
• As a useful indicator for the page replacement strategies

Principles of Operating Systems 25


Where are page tables stored?
• Page table is quite large
• The no. of entries = no. of virtual pages in the process address space
• Need to store contiguously in memory
• 32-bit with 4KiB page size
• 232/212 = 220 PTEs
• assume each PTE occupies 4 bytes, we need 4 MiB for a page table

• Each process has its own page table


• There are many processes running in the system !!!

• Must keep in physical memory – relatively slow speed of access

Principles of Operating Systems 26


Where are page tables stored?
• MMU needs to know where to find the page table for current running process

• 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

• To translate each virtual address


• MMU needs to access page table (via page-table base register) which is in the physical memory
• Based on the virtual page number, reads the PTE and performs the translation
• Fetches the desired data of that virtual address from the physical memory

• In summary, we need to access physical memory twice for each memory reference

Principles of Operating Systems 27


The Crux

• How to make paging faster and page table smaller?

• Each memory access results in two physical memory accesses

• Page table must be stored contiguously in physical memory

Principles of Operating Systems 28


Summary
• Segmentation allows better utilization of physical memory as only segments in
use are allocated with physical memory

• To support segmentation, MMU has to include more base-and-bounds register


pairs (or even a segment table) for the address translation and protection

• Allocating variable-sized segments in physical memory leads to external


fragmentation, which results in a waste of memory

• No matter how smart the allocation policies, external fragmentation still exists.

Principles of Operating Systems 29


Summary (2)

• By dividing the memory into fixed-size memory blocks, this avoids


external fragmentation and simplifies the free-space management

• To translate a virtual address, MMU


• Extracts the virtual page number from the virtual address
• Accesses process’s page table in physical memory to get the physical page
frame number
• Concatenates PFN with offset to form the physical address
• Fetches the data from the physical memory

Principles of Operating Systems 30

You might also like