Professional Documents
Culture Documents
CS 241 Section Week #9 (04/09/09)
CS 241 Section Week #9 (04/09/09)
CS 241 Section Week #9 (04/09/09)
(04/09/09)
Topics
• LMP2 Overview
• Memory Management
• Virtual Memory
• Page Tables
LMP2 Overview
LMP2 Overview
• LMP2 attempts to encode or decode a
number of files the following way:
– encode: %> ./mmap -e -b16 file1 [file2 ...]
– decode: %> ./mmap -d -b8 file1 [file2 ...]
• It has the following parameters:
– It reads whether it has to encode (‘-e’) or
decode(‘-d’);
– the number of bytes (rw_units) for each
read/write from the file;
LMP1 Overview
• You have TWO weeks to complete and submit
LMP2. We have divided LMP2 into two
stages:
– Stage 1:
• Implement a simple virtual memory.
• It is recommended you implement the my_mmap()
function during this week.
• You will need to complete various data structures to
deal with the file mapping table, the page table, the
physical memory, etc.
LMP1 Overview
• You have TWO weeks to complete and submit
LMP2. We have divided LMP2 into two stages:
– Stage 2
• Implement various functions for memory mapped files
including:
– my_mread() , my_mwrite() and my_munmap()
• Handle page faults in your my_mread() and
my_mwrite() functions
• Implement two simple manipulations on files:
– encoding
– decoding
Memory Management
Memory
• Contiguous allocation and compaction
• Internal Fragmentation
– Result of reserving space without ever using its part
– Caused by allocating fixed size of storage
Contiguous Allocation
• Memory is allocated in monolithic segments
or blocks
size
Compaction
• After numerous malloc() and free() calls,
our memory will have many holes
– Total free memory is much greater than that of any
contiguous chunk
• Access history: 0 1 2 3 4 4 3 2 1 0 …
– And here?
Virtual Memory
Why Virtual Memory?
• Use main memory as a Cache for the Disk
– Address space of a process can exceed physical memory size
– Sum of address spaces of multiple processes can exceed physical
memory
Why Virtual Memory?
• Use main memory as a Cache for the Disk
– Address space of a process can exceed physical memory size
– Sum of address spaces of multiple processes can exceed physical
memory
• Simplify Memory Management
– Multiple processes resident in main memory.
• Each process with its own address space
– Only “active” code and data is actually in memory
Why Virtual Memory?
• Use main memory as a Cache for the Disk
– Address space of a process can exceed physical memory size
– Sum of address spaces of multiple processes can exceed physical
memory
• Simplify Memory Management
– Multiple processes resident in main memory.
• Each process with its own address space
– Only “active” code and data is actually in memory
• Provide Protection
– One process can’t interfere with another.
• because they operate in different address spaces.
– User process cannot access privileged information
• different sections of address spaces have different permissions.
Principle of Locality
• Program and data references within a process
tend to cluster
Principle of Locality
• Program and data references within a process
tend to cluster
• Only a few pieces of a process will be needed
over a short period of time (active data or
code)
Principle of Locality
• Program and data references within a process
tend to cluster
• Only a few pieces of a process will be needed
over a short period of time (active data or
code)
• Possible to make intelligent guesses about
which pieces will be needed in the future
Principle of Locality
• Program and data references within a process
tend to cluster
• Only a few pieces of a process will be needed
over a short period of time (active data or
code)
• Possible to make intelligent guesses about
which pieces will be needed in the future
• This suggests that virtual memory may work
efficiently
VM Address Translation
• Parameters
– P = 2p = page size (bytes).
– N = 2n = Virtual address limit
– M = 2m = Physical address limit
n–1 p p–1 0
virtual page number page offset virtual address
address translation
m–1 p p–1 0
physical page number page offset physical address
•Etc.
Example: Two-level Page Table
• 32-bit address space (2GB)
Example: Two-level Page Table
• 32-bit address space (2GB)
1010 110
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
2 0
3 1
4 1
5 0
6 1
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
2 0
3 1
4 1 1010
5 0
6 1
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
2 0
3 1
4 1 1010
5 0
6 1
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
Index == 4 (100)
2 0
3 1
4 1 1010
5 0
6 1
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
Index == 4 (100)
2 0
3 1
4 1 1010
5 0
6 1
100
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
Index == 4 (100)
2 0
3 1
4 1 1010
5 0
6 1
100 110
Example
Page Table Virtual Address (1010110)
Index Present Virtual Addr
0 0 1010 110
1 1
Index == 4 (100)
2 0
3 1
4 1 1010
5 0
6 1
100 110