Lecture20
Lecture20
Multiprocessing Illustration
Memory
Process 1 Process 2 Process N
Stack Stack Stack
Heap Heap Heap
Data Data Data
Code Code Code
CPU
Registers
2
Carnegie Mellon
Problem 1: Space
Memory
Process 1 Process 2 Process N
Stack Stack Stack
Heap Heap Heap
Data Data Data
Code Code Code
3
Carnegie Mellon
Problem 1: Space
Memory
Process 1 Process 2 Process N
Stack Stack Stack
Heap Heap Heap
Data Data Data
Code Code Code
• Space:
• Each process’s address space is huge (64-bit): can memory hold it
(16GB is just 34-bit)?
• 2^48 bytes is 256 TB
• There are multiple processes, increasing the storage requirement
further
3
Carnegie Mellon
Hard Disk/
CPU SSD
Main/Physical
Cache Memory Several
Registers (SRAM) (DRAM) TBs
(DFF)
Several GBs
~1 ns 1-10 ns
100 ns
~ 10 us
4
Carnegie Mellon
• Challange: who is moving data back and forth between the DRAM/main
memory/physical memory and the disk?
• Ideally should be managed by the OS, not the programmer.
Hard Disk/
CPU SSD
Main/Physical
Cache Memory Several
Registers (SRAM) (DRAM) TBs
(DFF)
Several GBs
~1 ns 1-10 ns
100 ns
~ 10 us
4
Carnegie Mellon
Problem 2: Security
• Different programs/processes will share the same physical memory
•Or even different uses. A CSUG machine is accessed by all students, but
there is one single physical memory!
• What if a malicious program steals/modifies data from your program?
• If the malicious program get the address of the memory that stores your
password, should it be able to access it? If not, how to prevent it?
• We need isolation.
Hard Disk/
CPU SSD
Main/Physical
Cache Memory Several
Registers (SRAM) (DRAM) TBs
(DFF)
Several GBs
5
Carnegie Mellon
base 1
Program 1 Program 1
bound 1
base 2
Program 2
bound 2
Program 2
Main/Physical
Memory Hard Disk/
SSD 6
Carnegie Mellon
128 MB
Program 2
Program 2
12 MB
Main/Physical
Memory Hard Disk/
SSD 7
Carnegie Mellon
Program 1
CPU stdlib.so
stdlib.so
Cache
Registers (SRAM)
(DFF) Program 2
stdlib.so
Main/Physical
Memory Hard Disk/
SSD 8
Carnegie Mellon
…
of Process 1
99
100
101
Process 1 102
103
Physical/Main
104
Memory
105
… 1
2
Virtual Memory 3
of Process 2
… 4
99
100
101
Process 2 102
103
104
105
…
9
Carnegie Mellon
…
of Process 1
99 Unallocated
100 Data 1
101 …
103 … B …
Physical/Main
104 … C …
Memory
105 … D …
… 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
10
Carnegie Mellon
…
of Process 1
99 Unallocated
100 Data 1
101 …
103 … B …
Physical/Main
104 … C …
Memory
105 … D …
… 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
11
Carnegie Mellon
…
of Process 1
99 Unallocated
100 Data 1
101 …
103 … B …
Physical/Main
104 … C …
Memory
105 … D …
… 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
11
Carnegie Mellon
Page Table
Virtual Memory Hard Drive
Page Table
…
of Process 1
…
99 Unallocated Invalid
100 Data 1 1
101 … A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
12
Carnegie Mellon
Demand Paging
Virtual Memory Hard Drive
Page Table
…
of Process 1
…
99 Unallocated Invalid
100 Data 1 1
101 Data X A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
13
Carnegie Mellon
Demand Paging
Virtual Memory Hard Drive
Page Table
…
of Process 1
…
99 Unallocated Invalid
100 Data 1 1
101 Data X A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
13
Carnegie Mellon
Demand Paging
Virtual Memory Hard Drive
Page Table
…
of Process 1
…
99 Unallocated Invalid
100 Data 1 1
101 Data X A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2 Data X
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
13
Carnegie Mellon
Demand Paging
Virtual Memory Hard Drive
Page Table
…
of Process 1
…
99 Unallocated Invalid
100 Data 1 1
101 Data X A
2
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2 Data X
Virtual Memory 3
of Process 2
… 4 Data 2
99
100
101
Process 2 102
103
104
105
…
13
Carnegie Mellon
…
of Process 1
…
of Process 1
99 Unallocated Invalid
100 Data 1 1
Process 1 101 … A
102 Data 2 4 A …
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory Page Table 3 Data 3
of Process 2
… …
of Process 2 4 Data 2
99 … O …
O
100 Unallocated P …
Unallocated
101 … P Q …
102 Data 3 3 R …
103 Data 2 4
Process 2
104 … Q
105 … R
… …
14
Carnegie Mellon
…
of Process 1
…
of Process 1
99 Unallocated Invalid
100 Data 1 1
Process 1 101 … A
102 Data 2 4 A …
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory Page Table 3 Data 3
of Process 2
… …
of Process 2 4 Data 2
99 … O …
O
100 Unallocated P …
Unallocated
101 … P Q …
102 Data 3 3 R …
103 Data 2 4
Process 2
104 … Q
105 … R
… …
14
Carnegie Mellon
…
of Process 1
…
of Process 1
99 Unallocated Invalid
100 Data 1 1
101 … A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory Page Table 3 Data 3
of Process 2
… …
of Process 2 4 Data 2
99 … O …
O
100 Unallocated P …
Unallocated
101 … P Q …
102 Data 3 3 R …
103 Data 2 4
Process 2
104 … Q
105 … R
… …
15
Carnegie Mellon
16
Carnegie Mellon
16
Carnegie Mellon
• Benefits
• Ease of management: Guest could change rooms (physical
address). You can still find her without knowing it
• Protection: Guest could have block on calls, block on calls from
specific callers (permissions)
• Sharing: Multiple guests (virtual addresses) can share the same
room (physical address)
16
Carnegie Mellon
M-1:
Data word
17
Carnegie Mellon
M-1:
Data word
19
Carnegie Mellon
19
Carnegie Mellon
19
Carnegie Mellon
Today
• VM basic concepts and operation
• Other critical benefits of VM
• Address translation
20
Carnegie Mellon
VM Concepts
• Virtual memory is an array of N contiguous pages (each page has a certain amount
of continuous bytes).
• Physical memory is also divided into pages. Each physical page (sometimes called
frames) has the same size as a virtual page. Physical memory has way fewer pages.
• A page can either be on the (“uncached”) disk or in the physical memory (“cached”).
21
Carnegie Mellon
VM Concepts
• Virtual memory is an array of N contiguous pages (each page has a certain amount
of continuous bytes).
• Physical memory is also divided into pages. Each physical page (sometimes called
frames) has the same size as a virtual page. Physical memory has way fewer pages.
• A page can either be on the (“uncached”) disk or in the physical memory (“cached”).
What programmers see
Virtual memory Physical memory
0
VP 0 Unallocated
0
VP 1 In-memory PP 0
On-disk PP 1
Unallocated Unoccupied PP 2
In-memory PP 3
15
On-disk
In-memory
VP7 On-disk
31
21
Carnegie Mellon
VM Concepts
• Virtual memory is an array of N contiguous pages (each page has a certain amount
of continuous bytes).
• Physical memory is also divided into pages. Each physical page (sometimes called
frames) has the same size as a virtual page. Physical memory has way fewer pages.
• A page can either be on the (“uncached”) disk or in the physical memory (“cached”).
What programmers see Assuming page size is 4B
Virtual memory size is 32B
Virtual memory Physical memory Physical memory size is 16B
0
VP 0 Unallocated
0
VP 1 In-memory PP 0
On-disk PP 1
Unallocated Unoccupied PP 2
In-memory PP 3
15
On-disk
In-memory
VP7 On-disk
31
21
Carnegie Mellon
VM Concepts
• Virtual memory is an array of N contiguous pages (each page has a certain amount
of continuous bytes).
• Physical memory is also divided into pages. Each physical page (sometimes called
frames) has the same size as a virtual page. Physical memory has way fewer pages.
• A page can either be on the (“uncached”) disk or in the physical memory (“cached”).
What programmers see Assuming page size is 4B
Virtual memory size is 32B
Virtual memory Physical memory Physical memory size is 16B
0
VP 0 Unallocated
0
VP 1 In-memory PP 0 3-bit 2-bit
On-disk PP 1
Unallocated Unoccupied PP 2 Virtual page number offset
In-memory PP 3
15
On-disk
In-memory
VP7 On-disk
31
21
Carnegie Mellon
VM Concepts
• Virtual memory is an array of N contiguous pages (each page has a certain amount
of continuous bytes).
• Physical memory is also divided into pages. Each physical page (sometimes called
frames) has the same size as a virtual page. Physical memory has way fewer pages.
• A page can either be on the (“uncached”) disk or in the physical memory (“cached”).
What programmers see Assuming page size is 4B
Virtual memory size is 32B
Virtual memory Physical memory Physical memory size is 16B
0
VP 0 Unallocated
0
VP 1 In-memory PP 0 3-bit 2-bit
On-disk PP 1
Unallocated Unoccupied PP 2 Virtual page number offset
In-memory PP 3
15
On-disk
In-memory 2-bit 2-bit
VP7 On-disk
31 Physical page number offset
Physical pages (PPs)
Virtual pages (VPs)
cached in DRAM
21
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
22
Carnegie Mellon
23
Carnegie Mellon
Disk
VP 1
VP 2
VP 3
VP 4
VP 6
VP 7 23
Carnegie Mellon
Valid
PTE 0 0 null
1
1
0
1
0 null
0
Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7 23
Carnegie Mellon
Page Hit
•Page hit: reference to VM word that is in physical memory
Physical memory
Physical page (DRAM)
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 4 PP 3
1
0
1
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7
24
Carnegie Mellon
Page Hit
•Page hit: reference to VM word that is in physical memory
Physical memory
Physical page (DRAM)
Virtual address
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 4 PP 3
1
0
1
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7
24
Carnegie Mellon
Page Fault
•Page fault: reference to VM word that is not in physical memory
Physical memory
Physical page (DRAM)
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 4 PP 3
1
0
1
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7
25
Carnegie Mellon
Page Fault
•Page fault: reference to VM word that is not in physical memory
Physical memory
Physical page (DRAM)
Virtual address
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 4 PP 3
1
0
1
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7
25
Carnegie Mellon
Physical memory
Physical page (DRAM)
Virtual address number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 4 PP 3
1
0
1
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 6
VP 7
26
Carnegie Mellon
Allocating Pages
• Allocating a new page (VP 5) of virtual memory.
Physical memory
Physical page (DRAM)
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 3 PP 3
1
1
0
0 null
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 5
VP 6
VP 7
30
Carnegie Mellon
Allocating Pages
• Allocating a new page (VP 5) of virtual memory.
Physical memory
Physical page (DRAM)
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 3 PP 3
1
1
0
0
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 5
VP 6
VP 7
30
Carnegie Mellon
Allocating Pages
• Allocating a new page (VP 5) of virtual memory.
Physical memory
Physical page (DRAM)
number or
VP 1 PP 0
Valid disk address
VP 2
PTE 0 0 null
VP 7
1 VP 3 PP 3
1
1
0
0
0 Disk
PTE 7 1 VP 1
Memory resident VP 2
page table
VP 3
(DRAM)
VP 4
VP 5
VP 6
VP 7
30
Carnegie Mellon
31
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
• M << N
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
• M << N
• On a 64-bit machine, virtual memory size = 264
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
• M << N
• On a 64-bit machine, virtual memory size = 264
• Physical memory size is much much smaller:
• iPhone 8: 2 GB (231)
• 15-inch Macbook Pro 2017: 16 GB (234)
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
• M << N
• On a 64-bit machine, virtual memory size = 264
• Physical memory size is much much smaller:
• iPhone 8: 2 GB (231)
• 15-inch Macbook Pro 2017: 16 GB (234)
• Store only the most frequently used pages in the physical memory
32
Carnegie Mellon
VM Concepts Summary
• Conceptually, virtual memory is an array of N pages
• Each virtual page is either in physical memory, or on disk, or
unallocated
• The physical memory (PM) is an array of M pages stored in DRAM.
• Page size is the same for VM and PM
• M << N
• On a 64-bit machine, virtual memory size = 264
• Physical memory size is much much smaller:
• iPhone 8: 2 GB (231)
• 15-inch Macbook Pro 2017: 16 GB (234)
• Store only the most frequently used pages in the physical memory
• If a page is not on the physical memory, have to first swap it from
the disk to the DRAM.
32
Carnegie Mellon
33
Carnegie Mellon
33
Carnegie Mellon
33
Carnegie Mellon
33
Carnegie Mellon
33
Carnegie Mellon
33
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
Today
• VM basic concepts and operation
• Other critical benefits of VM
• Address translation
35
Carnegie Mellon
0 Address 0
Virtual Physical
translation
Address VP 1 Address
Space for VP 2 PP 2 Space
Process 1: ... (DRAM)
N-1
(e.g., read-only
PP 6
library code)
0
Virtual PP 8
Address VP 1
Space for VP 2
... ...
Process 2:
N-1 M-1
36
Carnegie Mellon
38