0% found this document useful (0 votes)
95 views20 pages

9-Memory Management PDF

The document discusses memory management in operating systems. It describes the memory hierarchy from fast but small cache memory to slower but larger disk storage. It explains basic memory management techniques like fixed partitions and relocation to map program addresses to physical memory. The document also covers paging and virtual memory which divide processes into pages that can be swapped between memory and disk as needed. Finally, it discusses the memory management unit that translates virtual addresses to physical addresses using a page table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views20 pages

9-Memory Management PDF

The document discusses memory management in operating systems. It describes the memory hierarchy from fast but small cache memory to slower but larger disk storage. It explains basic memory management techniques like fixed partitions and relocation to map program addresses to physical memory. The document also covers paging and virtual memory which divide processes into pages that can be swapped between memory and disk as needed. Finally, it discusses the memory management unit that translates virtual addresses to physical addresses using a page table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CS 550 Operating Systems

Spring 2019

Memory Management

1
Memory Management
• Ideally programmers want memory that is
• large
• fast
• non volatile

• Memory hierarchy
• small amount of fast, expensive memory – cache
• some medium-speed, medium price main memory
• Lots of slow, cheap disk storage

• Memory manager handles the memory hierarchy

2
Typical Memory Hierarchy

SMALLER
FASTER

SLOWER BIGGER

3
Basic Memory Management
"Mono-programming" without Swapping or Paging

Three simple ways of organizing memory


- an operating system with one user process
4
Multiprogramming with Fixed Partitions

• Fixed memory partitions


(a) separate input queues of processes for each partition
(b) single input queue

5
Relocation and Protection
• Problem: A programmer doesn’t
know where a program will be LIMIT
loaded in memory
Relative Addresses in
• address locations of variables and
code routines cannot be absolute original program binary
• must keep a program out of other
processes’ partitions 0
• Solution: Use base and limit
values Physical
• Relocation MAX
• Address locations in a program are BASE +
relative. LIMIT Relocated Addresses
• They are added to a base value to
map to physical addresses. in Executing Binary

• Protection
BASE
• Access to address locations larger
than limit value results in an error
0
6
What if physical memory is not enough to hold all
processes?
— Swapping

• Physical memory may not be enough to accommodate the needs of all


processes
• Memory allocation changes as
• processes come into memory
• leave memory and are swapped out to disk
• Re-enter memory by getting swapped-in from disk
• Shaded regions are unused memory
7
Virtual Memory
• Swapping the memory of an entire
process is useful when the sum of memory Virtual Address Space
needed by all processes is greater than the of a single Process
total RAM available in the system.

• But sometimes, a single process might


require more memory than the total RAM
} Page

in the system.
Entire
• In such cases swapping an entire process Physical RAM
is not enough.

• Rather, we need to break up the memory


space of a process into smaller equal-sized
pieces, called PAGES.

• OS then decides which pages stay in


memory and which get moved to disk.

• Virtual memory: means that each process


gets an illusion that it has more memory
than the physical RAM in the system.
8
Memory Management Unit (MMU)

• MMU is a hardware module that accompanies the CPU


• It translates the Virtual Address used by executing
instructions to Physical Addresses in the main memory. 9
Question1.
• Why a 32-bit system can address 2^32 bytes
memory?
Size of address space (in bytes) as a
function of address size (in bits)
Number of bits in address Maximum address space size
(bytes)
0 20 = 1 byte
1 21 = 2 bytes
2 22 = 4 bytes
10 210 = 1024 = 1KiB
12 212 = 4KiB
16 216 = 64 KiB
32 232 = 4GiB (Gibibytes)
64 264 = 16 EiB (Exbibytes)
Question
• Consider a machine that has a 32-bit virtual address space
and 4KByte page size.

1.How many virtual pages could a process have?


4GB/4KB = 2^20

12
Page Table

• An array that stores the mapping from


virtual page numbers to physical
numbers

• The OS maintains
• One page table per userspace
process.
• And usually another page table
for kernel memory.

13
Question
• Consider a machine that has a 32-bit virtual address space
and 4KByte page size.

1.How many virtual pages could a process have?


4GB/4KB = 2^20
2. How many page-table entries (for one process) are there
in the page table?
4GB/4KB = 2^20

14
Translating
Virtual address (VA) to physical address (PA)
Virtual Address Space Physical RAM
Byte Address =
Page Number x Page Size +
Byte Offset in the page
6
Byte Offset
VA = VPN x Page Size + Byte Offset
5
PA = PPN x Page Size + Byte Offset
4

Virtual
3
Physical
Page 2
2 Page (frame)
Numbers Byte Offset
Numbers
(VPN)
(PPN)
1
1
0
0
Question
• Consider a machine that has a 32-bit virtual address space
and 4KByte page size.

1.How many virtual pages could a process have?


4GB/4KB = 2^20
2. How many page-table entries (for one process) are there
in the page table?
4GB/4KB = 2^20
3. How many bits do we need for Byte Offset
Log2(4096) = 12

16
Translating
Virtual address (VA) to physical address (PA)
Virtual Address Space Physical RAM
Byte Address =
Page Number x Page Size +
Byte Offset in the page
6
Byte Offset
VA = VPN x Page Size + Byte Offset
5
PA = PPN x Page Size + Byte Offset
4

Virtual
3
Physical
Page 2
2 Page (frame)
Numbers Byte Offset
Numbers
(VPN)
(PPN)
1
1
0
0
Virtual Address Translation
For Small Address Space

Internal operation of MMU with 16 4 KB pages 18


Quiz
• Consider a machine that has a 32-bit virtual address space
and 8KByte page size.

1.What is the total size (in bytes) of the virtual address space
for each process?

2.How many bits in a 32-bit address are needed to determine


the page number of the address?

3.How many bits in a 32-bit address represent the byte offset


into a page?

4.How many page-table entries are present in the page


table?

19
Quiz Answers
• Consider a machine that has a 32-bit virtual address space and 4KByte
page size.

1. Total size (in bytes) of the virtual address space for each process =
2^32 = 4 * 1024 * 1024 *1024 bytes = 4 GB

2. Number of pages in virtual address space = 4GB/8KB = 512*1024 =


2^9*2^10 = 2^19
• So the number of bits in a 32-bit address are needed to determine the
page number of the address = log2(4GB/8KB) = log2(2^19) = 19 bits

3. How many bits in a 32-bit address represent the byte offset into a
page?
• log2(8KB) = log2(2^13) = 13
• Also, 32 – 19 = 13 bits

4. How many page-table entries are present in the page table?


• Number of PTEs = Number of pages in virtual address = 4GB/8KB = 2^19
pages

20

You might also like