Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

CS837: ADV.

OPERATING SYSTEMS
MEMORY MANAGEMENT: RELOCATION,
SEGMENTATION & PAGING

Dr. Mian M.Hamayun


mian.hamayun@seecs.edu.pk
http://seecs.nust.edu.pk/faculty/mianhamayun.html
Relocation 
• With early fixed partitioning schemes, processes were 
always assigned to the same memory area 
– No changes in addressing memory locations 
• With dynamic schemes, processes can be relocated in 
memory 
– When processes are swapped in they may not be placed in 
the same memory area 
– actual physical location may change whenever a process is 
swapped in again 
• Hardware support for a dynamic relocation of 
processes in memory via logical address translation 
 
Addressing Memory 
• A programmer cannot know in advance where in 
memory a program will be loaded 
• Compiler produces code that refers to memory 
locations
– These addresses cannot be absolute (physical addresses), 
but have to be “relative” to the start address of the
program 
• Linker combines pieces of a program (with libraries) 
into a loadable image 
– Assumes the program to be loaded at address 0 
• Addresses generated by the compiler and linker have 
to be bound to the actual memory locations 
Processing of a User Program 
• How to locate a program in 
memory 
– Compile time 
• If memory location is known, absolute 
code can be generated 
– Load time 
• Must generate relocatable code if 
memory location is not known at 
compile time 
– Execution time 
• Binding is delayed until runtime, if the 
process can be moved around during its 
execution (needs hardware support) 
Relocation 
• A compiled program should be independent of physical 
memory locations 
• Programs have to be executable at any location in 
memory 
– Program instructions refer to memory addresses 
• Load data into register, or write register back to memory 
• Addresses used in program cannot be actual physical 
addresses 
– makes program relocation impossible 
– If we swap out a process image and reload it, it would have 
to be loaded into exactly the same memory location 
Relocation 
• Solution: 
– Programs operate as if they all start at address 0 
• Programs operate with relative addressing mechanism: 
– Reference to memory in program independent of 
actual physical location 
– Addresses in program are offsets from its base 
address (the starting address of the process image)
– add the base address whenever there is an access to 
memory 
Addressing Memory 
• Physical Addresses 
– Physical memory locations 
• Logical Addresses: 
– Reference to memory in program, independent of 
actual physical location 
– Relative addressing schemes, addressing 
mechanisms in hardware 
• MMU translates logical addresses into physical 
addresses 
Relocation with Relative Addressing 
Relocation with Relative Addressing 
• The address space of a process is mapped onto physical 
memory 
• Address translation with hardware 
• Two registers 
– Base register: physical start address of the process image 
in memory 
– Bound register: physical end address of the process image 
in memory 
• Memory access 
– Process uses relative address for memory location 
– This access is translated into a physical address by adding 
the content of the base register to the relative address 
Memory Abstraction: Address Space 
• Address space 
– Is a contiguous sequence of “logical” memory
addresses 
– Each process lives within its own address space 
– Size of address space depends on processor 
architecture 
• 32bit architecture: 4 GB, 4 billion logical addresses 
Bytes  Exponent 
1,024  210  1kb  1024bytes 
1,048,576  220  1MB  1024kb  1024 x 1024 
1,073,741,824  230  1GB  1024MB  1024 x 1024 x 1024 
4,294,967,296  232  4GB  4 x 1024MB  4 x 1024 x 1024 x 1024 
1,099,511,627,776  240  1TB  1024GB  1024 x 1024 x 1024 x 1024 
1,125,899,906,842,620  250  1PB  1024TB  1024 x 1024 x 1024 x 1024 x 1024 
1,152,921,504,606,850,000  260  1EB  1024PB  1024 x 1024 x 1024 x 1024 x 1024 x 1024 
18,446,744,073,709,600,000   264  16EB  16 x 1024 x 1024 x 1024 x 1024 x 1024 x 1024 
Virtual Memory Management 
• Modern approach 
– All memory references are logical addresses that are 
dynamically translated into physical addresses at run 
time 
– Non­contiguous allocation of memory for processes 
• A process may be broken up into a number of pieces – pages 
that don’t need to occupy a contiguous area of main
memory 
• Segments may start at different physical locations 
– Processes only partially loaded into physical memory 
• Use paging to load memory of processes on demand 
 
 
Segmentation 
Segmentation 
• Segments allow Programs to be divided into logical 
units of variable length 
– Process image regarded as a collection of segments 
• Code segment 
• Data segment 
• Stack, Heap, shared libraries etc. 
• Each segment is a separate address space 
– Has a base address where the segment starts, memory 
within a segment is addressed relative to this base address 
– Allows parts of a program code to be altered and 
recompiled independently 
• Shared libraries 
• Segments are visible to programmer 
Segmentation 
• With segmentation, program may occupy non­
contiguous memory 
– Each segment in separate memory area 
– Better utilisation of memory 
• Smaller contiguous segments of memory needed 
• Segmentation is a dynamic partitioning 
scheme 
– No internal fragmentation, but external 
fragmentation 
Segmentation 
• Each process has a segment table 
– Each segment has a base address 
– Segments are of variable size 
• Segment table entry has to specify the length of a segment 
– Held in the PCB 
• Entry in the segment table: 
– Base address  Segment Table 
– Length of a segment  l
e
Length  Base  offset  n
g
t
Virtual Address  Segment   Offset  h 
Segmentation 
Segmentation 
• Each segment is a separate address space 
– Programmer may view memory as consisting of multiple address spaces 
• Address translation via process segment table 
 
Protection and Sharing 
• Each segment can have special protection
(read / write / execute policies) 
– Each segment is specified in physical memory by a 
base address and its length, each memory access 
has to be checked against these two parameters 
– E.g.: text segment is read­only 
• Segments can be shared among processes 
Paging 
Paging 
• Organise both virtual address space of process 
image and physical memory into fixed­sized 
blocks: 
– Frames: Physical memory is divided into equal fixed­
sized blocks, called “frames” or “page frames” 
– Pages: Virtual address space of a process is divided 
into equal fixed­sized blocks called “pages” 
– Any page can be assigned to any free page frame 
• When process is loaded into physical memory, its 
pages are loaded into frames 
– Pages and frames are of equal size 
 
 
Paging 
• Allocation of pages to frames in physical memory 
can be arbitrary and non­contiguous 

Page 0  1 
Page 0 

0  1  Page Frames in 
Page 1 
1  4  Page 2  3  physical memory 
Page 2  2  3 
Page 1  4 
3  7 
Page 3 

Page Table 
Logical 

Memory 
Page 3  7 
Physical 
Memory 
Paging 
• Each process has its own page table 
– Records the memory frames where the pages of a process are 
currently located 
– Each entry is the base address of a page loaded into a frame 
– The size of this table depends on the size of the process 
– At context switch, process table is loaded into Memory Management 
Unit 
• Process table entry 
– Frame number, info where to find page on secondary storage 
– Present bit: is page in physical memory?
– Modified bit: was there a write operation?
– Control bits: read­only, kernel / user pages 
• Page management not visible to programmer, compiler or linker 
• Free Frame List 
– list of free page frames 
 
Paging: Address Translation 
• Logical addresses have two parts 
– Page number (p) 
• Used as an index into the page table 
• An entry in this table contains the base address where a particular 
page is located in physical memory 
– Page offset (d) 
• Combined with the base address, defines the physical memory 
address within the space in memory occupied by the page 
– The high­order m­n bits encode the page number (index into 
page table), lower n bits encode the page offset 
 
Page number  Page offset  Logical address space: 2 m 
p  d 
Size of a page: 2n 
m  n  0 
Number of pages: 2m­n 
m – n bytes   n bytes 
Paging: Address Translation 
Process 
Image
Paging 
Address Space  Physical Memory 
0000 0000 
0000 0001 
Page Table 
... 
...  0000  1001 
0000 1111  0001  0010 
0001 0000 
Page number  0010  1101 
0001 0001  0011  0101  1100 0000 
...  ...  ...  1100 0001 
...  ... 
0001 1111  Frame number  ... 
1100 1111 
0010 0000 
Logical Address  0010 0001  Physical Address  1101 0000 
...  1101 0001  1101 0001 
...  Offset within Page  ... 
0010 1111  ... 
1101 1111 
0011 0000 
0011 0001 
... 
... 

 
1111 1111 
Addressing Virtual Memory 
• Pages are typically of sizes between 512 bytes and 16MB 
• Consider a 32­bit memory address 
– 4kB per page: we need 12 bits (2 12 = 4096) to address a location 
within the page 
– 20 bits for the page number: we can have 2 20 = 1 Mio pages 
• Page table must have 1 Mio entries 
• Address space: 4GB of virtual memory 

Bytes  Exponent 
1,024  210  1kb  1024bytes 
1,048,576  220  1MB  1024kb  1024 x 1024 
1,073,741,824  230  1GB  1024MB  1024 x 1024 x 1024 
4,294,967,296  232  4GB  4 x 1024MB  4 x 1024 x 1024 x 1024 
1,099,511,627,776  240  1TB  1024GB  1024 x 1024 x 1024 x 1024 
17,592,186,044,416  244 16TB  16 x 1024GB  16 x 1024 x 1024 x 1024 x 1024 
1,125,899,906,842,620  250  1PB  1024TB  1024 x 1024 x 1024 x 1024 x 1024 
1,152,921,504,606,850,000  260  1EB  1024PB  1024 x 1024 x 1024 x 1024 x 1024 x 1024 
18,446,744,073,709,600,000   264  16EB  16 x 1024 x 1024 x 1024 x 1024 x 1024 x 1024 
Address Translation 

• The physical address of memory location is generated by retrieving 
the physical base address of a page from the page table (index into 
page table contained in logical address) and combining it with the 
offset 
Fragmentation 
• There is no external fragmentation 
– Any free frame can be allocated to a process that needs it 
– This is due to the fixed page size the correspondence 
between page size and frame size 
• There is internal fragmentation 
– As page / frame sizes are fixed, there may be some waste 
• The last frame allocated to a process may not be needed in its 
completeness 
– Worst case: process may need n frames plus one 
additional byte! A complete additional frame has to be 
allocated just for this additional byte of required memory 
– Expected internal fragmentation: on average one­half page 
per process 
 
Virtual Memory 
Paging and Virtual Memory 
• Helps to implement virtual memory 
– Page file held on hard disk, is also organised in blocks 
of the same size as a page 
– Pages of a process can easily been swapped in and out
of memory 
– We can implement a process image only being 
partially loaded, pages are loaded on demand 
• No time­consuming search for free best­fit  
memory fragments 
• No external memory fragmentation 
• Paging is supported by modern hardware 
Virtual Memory 
• Virtualisation of memory management 
– Not all pages of a process must be in physical memory 
• A large part of the process image can be held on secondary 
storage 
• A page load mechanism is needed to load pages on demand 
– Only those pages that are actually needed, are loaded 
into memory 
• When program accesses memory, always check first whether 
page is in memory 
– If page is not loaded – “page fault”, leads to process
interruption and I/O operations 
Page Table for Virtual Memory 
• Page table has one 
entry per page of 
the process image 
• All pages 
represented on disk 
• Only some of them 
loaded into physical 
memory 
Control Bits 
• Page table entries are 
annotated with control bits: 
Frame # valid-invalid bit
– Present or valid bit: indicates  v
whether a page is loaded in  v
memory or not  v
– Modify bit: indicates whether a  v
page has been modified since it i
was loaded  ….
• If not, no need to write it out 
when it is replaced  i
i
– Other bits may indicate  page table
read/write protection, whether 
a page is shared etc. 
Page Fault Handling 

You might also like