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

09 SegmentationPaging

Uploaded by

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

09 SegmentationPaging

Uploaded by

Z Goo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

¤ To 11 responses: Many thanks!

¤ (4)Having problems in following the course materials?


¤ Come to lectures?
¤ Action: Will give more examples with more detailed explanation
¤ Suggestion: Read the (required) textbook chapters

¤ (8)Having problems with assignments!


¤ Action: Sample code provided for PA2

¤ More useful Tips:


¤ Ask questions, even better, answer questions, to help your understanding
¤ Let us know how you feel, anytime, anyway!

Principles of Operating Systems 1


2022 Fall COMP3230A
¤ 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 3


¤ ILO2b - describe the principles and techniques used by
OS in effectively virtualizing memory resources.

¤ ILO3 [Performance] - analyze and evaluate the


algorithms of . . . and explain the major performance
issues . . .

Principles of Operating Systems 4


¤ 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 5


¤ 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


¤ 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’


offset = virtual address v’ – starting virtual address of the segment v’
start
physical address = segment_base + offset

¤ 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

¤ Each virtual address v’ is in one segment only. Can we make


use of 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 8


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

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

¤ 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 10


¤ Sharedcode segments, e.g., libraries
¤ Copy-on-write

Principles of Operating Systems 11


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 use, valid bit should set to false
¤ If a process tries to access such memory, an exception will be
generated

Principles of Operating Systems 12


¤ 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 segment B

terminated, all physical memory used by its segments would


be released segment C

¤ During normal runtime, many processes arrive, use up the segment D


physical memory, terminate and release the physical memory
segment E
¤ Physical memory quickly becomes full of little holes of free
space, which are not large enough to hold a segment segment F

¤ However, the sum of the holes might large enough to


accommodate some more segments segment G

Principles of Operating Systems 13


¤ Possible ways to combat external fragmentation
¤ Coalescing
¤ Maintain a list of free blocks
¤ 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 14


— 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 15


¤ Best-fit
¤ Search 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 16


¤ 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 17


¤ 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 18


¤ 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 19


NOT FOR EXAMINATION

¤ Binary Buddy Allocator


¤ The search for free space recursively
divides free space by 2 until a block
that is big enough to accommodate
the request is found (and a further split
into two would result in a space that is
too small).
¤ When a block is freed, a recursive
coalescing process continues up the
tree, either restoring the entire free
space or stopping when a buddy is
found to be in use.
Principles of Operating Systems 20
¤ 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 21


¤ Advantages of Paging
¤ Simple, 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 22


¤ 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 simple 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 23


¤ 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 24


¤ 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 25


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


Page table find that page p is in page frame number p'

Principles of Operating Systems 26


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 27


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 28


¤ 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 29
¤ 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 the next process

¤ To translate each virtual address


¤ MMU needs to access page table (via page-table base register) which is in the
physical memory
¤ Reads the PTE and performs the translation
¤ Fetches the desired data from the physical memory

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


reference

Principles of Operating Systems 30


¤ 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 31


¤ 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 32


¤ 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 33

You might also like