OS Lecture-12 (Paging and Segmentation)
OS Lecture-12 (Paging and Segmentation)
Memory Management
Schemes (/Strategies)
Contiguous
Paging Segmentation
Allocation
Multiple-partition Conventional
method Variable size Paging Hierarchical Hashed Inverted
(Fixed-partition method Paging Paging Paging
allocation) (basic method)
Memory Management Strategy #2:
Paging
Paging
• Paging is a memory-management scheme that permits the physical
address space of a process to be non-contiguous.
• Paging method also used to manage backing store (e.g. hard disk).
Paging: Basic Method
Frame
number
0
1 page 0
page 0 0 1
2
page 1 1 4
3 page 2
page 2 2 3
4 page 1
page 3 3 7
5
Logical Page table
memory 6
7 page 3
Physical
memory
Paging: Basic Method (cont.)
• The basic methodology:
– Breaking physical memory into fixed-sized blocks called frames
– Breaking logical memory into blocks called pages (where frame size=page size)
• When a process is to be executed, its pages are loaded into any available
memory frames from the backing store.
• The backing store is divided into fixed-sized blocks that are of the same
size as the memory frames.
─ Where:
• m = number of bits in logical address ⇒ Size of memory = 2m
• n = number of bits in offset part ⇒ Size of page = 2n
• m - n = number of bits in page number part ⇒ # of pages = 2m-n
Paging Hardware
0
Example Frame #0
4 i
j
Given: n=2 bits, m=4 bits Frame #1 k
l
Page size = 4 bytes, Physical memory = 32 bytes 8 m
n
Convert the logical address (13) to physical address. Frame #2 o
p
12
0 a
1 b 0 5 Frame #3
2 c Page #0
3 d 1 6 16
4 e 2 1 Frame #4
5 f
6 g Page #1
3 2 20 a
7 h b
8 i Page table Frame #5 c
9 j d
10 k Page #2
24 e
11 l f
12 m Frame #6 g
13 n h
14 o Page #3
28
15 p
Frame #7
Logical memory
Physical memory
Example (cont.)
Solution: 0 5
Physical address = base address + offset 1 6
= (frame # * frame size) + offset 2 1
3 2
Logical address = 13 (in decimal) = (1101) in binary Page table
Logical address = 1 1 0 1
• # of pages= 2m-n
d) Width of page table entry = # bits to specify frame number = 8 bits = 1 byte
Ans: (c) 2 MB
Frame Allocation Procedure
• When a process arrives in the system to be executed:
– Then, when a page is loaded into a frame, its frame number is put
into the page table for this process…. and so on.
Frame Allocation Procedure (cont.)
• Generally, page sizes have grown over time as processes, datasets, and main
memory have become larger.
Paging Hardware
Tables Used in Paging Scheme
• Page table:
─ Part of process control block (PCB)
─ Each process has one page table
─ Contains 1 entry per logical page
─ Used to translate logical addresses to physical addresses in software
• Frame table:
─ Maintained by OS
─ Contains 1 entry per physical frame
• Whether free or allocated
• If allocated it contains allocation information (Process ID, page#)
Paging Hardware
• The hardware implementation of the page table can be done in several
ways.
• Different methods to implement (/store) the page table(PT):
─ Method 1: Storing PT in dedicated registers:
• The simplest case
• Page table is stored in a set of dedicated, high-speed registers
• Instructions to load/modify PT registers are privileged
• Acceptable solution if page table is small
If TLB hit >>> it takes (1) TLB access & (1) memory access
Memory access time if TLB hit = 20 + 100 = 120 ns
If TLB miss >>> it takes (1) TLB access & (1) memory access to read PT &
(1) memory access to access the desired byte
Memory access time if TLB miss= 20 + 100 + 100 = 220 ns
• Note that with 4K pages, this would take 1024 pages just to hold
the page table!
Two-Level Page-Table Scheme
• A logical address (on 32-bit machine with 4K page size) is
divided into:
– a page number consisting of 20 bits.
– a page offset consisting of 12 bits.
• Each element contains (1) the virtual page number (2) the value of
the mapped page frame (3) a pointer to the next element.
• Entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns that
page.
• Decreases memory needed to store each page table, but increases time
needed to search the table when a page reference occurs.
• Use hash table to limit the search to one — or at most a few — page-table
entries.
– TLB can accelerate access.
stack
1400
Segment 0
Subroutine
Segment 3
Limit base
Segment 0 Symbol 1000 1400
table 400 6300 3200
sqrt 400 4300
Segment 4 Segment 3
1100 3200
Main 1000 4700
4300
Segment 1 Segment 2
program Segment 4700
Segment 2 table
Segment 4
stack
1400
Segment 0
Subroutine
Segment 3
Limit base
Segment 0 Symbol 1000 1400
table 400 6300 3200
sqrt 400 4300
Segment 4 1100 3200 Segment 3
Which of the following logical address will produce trap addressing error?
a) 0, 430
b) 1, 11
c) 2, 100
d) 3, 425
e) 4, 95
Paging vs. Segmentation
Paging Segmentation
A page is of the fixed block size. A segment is of variable size.
In Paging, the hardware decides the The segment size is specified by the
page size. user.
Page table stores the page data Segmentation table stores the
segmentation data.
Paging vs. Segmentation (cont.)
Paging Segmentation
Paging does not facilitate any sharing of Segmentation allows for the sharing of
procedures. procedures.