Lecture 27

advertisement
ITEC 352
Lecture 27
Memory(4)
Review
• Questions?
• Cache control
– L1/L2  Main memory example
– Formulas for hits
Memory (4)
Objectives
• Cache memory
• Intro to virtual memory
Memory (4)
Summary
so far …
• We have seen different types of memory elements and
gone up the hierarchy of memory.
– We have developed RAM, ROM, Registers and looked at
Caches.
• Next: we will see how programs that we develop are
allocated memory.
• Some terminology:
– Process: any program in execution is called a process.
• E.g., A java program that you write is simply a program, unless you execute
it. During its execution it becomes a process.
• There can be multiple processes of the same program.
Memory (4)
Memory
Allocation
• Two problems in memory allocation.
– Processes must not have conflicts in memory
they use. (must have separate memory).
• E.g., you wouldn’t want memory used by your process to
be overwritten by that of another user.
• Solution: Relocatable Code
– Small amount of physical memory (RAM) must be
used to execute a large number of processes
– (or execute large processes each of which
exceed the size of RAM)
– Solution: Virtual Memory.
Memory (4)
Relocatable code
and
Virtual Memory
• Relocatable assembly code: processes must be able to reside
in any portion of the physical memory.
– Why ? Consider an operating system, where all executions of
Microsoft Word must only reside at address 0x800000. What
would happen ?
– Hence, compiled and assembled programs do not have fixed
addresses allocated to them.
 Virtual memory: use hard disk as an extension for RAM.
Intuition: processes are allocated memory in a special
portion of the hard disk. They are loaded into RAM only
when they are executing on the CPU
Memory (4)
Relocatable
code
• Program binary code, such that the
addresses of the process address space
can be changed dynamically.
• I.e. it doesn’t map to main memory
• It has it’s own memory space
• Operating system replaces with what it
deems appropriate
Memory (4)
Relocatable
Code
#include <…>
…
int main() {
int x = 10;
return x;
}
Compiler + <main+0>: push %ebp BINARY CODE
assembler
0x0804867c <main+0>:
0x0804867d <main+1>:
0x0804867f <main+3>:
…
0x0804869c <main+32>:
0x0804869d <main+33>:
Memory (4)
<main+1>:
<main+3>:
mov %esp,%ebp
sub $0x8,%esp
…
<main+32>: ret
<main+33>: nop
push %ebp
mov %esp,%ebp
sub $0x8,%esp
ret
nop
Compiler generates relocatable code:
e.g., return instruction is 32 bytes away
from beginning of code segment
When process is created. The process
is given addresses by OS Address
called as logical address
Address binding: binding each
logical address to a location in
physical memory
Relocatable
Code
#include <…>
…
int main() {
int x = 10;
return x;
}
Compiler + <main+0>: push %ebp BINARY CODE
assembler
0x0804867c <main+0>:
0x0804867d <main+1>:
0x0804867f <main+3>:
…
0x0804869c <main+32>:
0x0804869d <main+33>:
Memory (4)
<main+1>:
<main+3>:
mov %esp,%ebp
sub $0x8,%esp
…
<main+32>: ret
<main+33>: nop
push %ebp
mov %esp,%ebp
sub $0x8,%esp
ret
nop
Compiler generates relocatable code:
e.g., return instruction is 32 bytes away
from beginning of code segment
When process is created. The process
is given addresses by CPU. Address
called as logical address
Address binding: binding each
logical address to a location in
physical memory
Using a HD
as memory
© Image from Silberschatz and Galvin
Memory (4)
Three types of memory addresses
C program
Binary code
(relocatable addresses)\
Aka linear addresses
Compiler/Assembler
Three types of addresses: linear address (addresses in
compiled binaries), logical address (address in the virtual
memory) and physical address (address in the RAM
when a program is loaded).
Logical Address
Bound to Physical memory
address
Loader
When
program is
Executed.
CPU generated Logical Addresses
Also called Virtual address
Dynamic Relocation using relocation register: mapping logical to physical
address.
© Image from Silberschatz and Galvin
Virtual
Memory (2)
• When a process is executed, the OS allocates memory for the
process on the disk (usually 4 GB of memory is allocated).
– This includes a code segment, data segment and program stack.
– In UNIX, we usually call this portion of the disk as swap partition.
• Advantage: Process memory is not tied to the amount of RAM.
• However, for a CPU to execute a process, the process must be in
the physical memory.
– Hence, OSes provide methods to map process address space on
virtual memory (called logical memory) to physical memory
(memory addresses in RAM).
Memory (4)
Mapping
• How can we map logical address spaces to
physical address spaces? (brainstorming
section).
Memory (4)
Mapping
1. Demand paging:
1. Slice up the process address space into equal
sized blocks (or frames).
2. Slice up the RAM into the same sized frames
called pages.
3. Map a frame to a page
2. Segmentation:
1. Slice up the process address space using
logic.
Memory (4)
Paging Model of Logical and Physical Memory
Simple table to map
logical address to
physical address.
Process address
space on hard disk
© Image from Silberschatz and Galvin
Process address
space in RAM
Address
Translation
• How do we map a logical address to a
physical address ?
• Each address is separated into two parts:
– Page number (p) –index into a page table
– Page offset (d) – combined with base address to
define the physical memory address that is sent to
the memory unit
page number
Memory (4)
page offset
p
d
m-n
n
© Silberschatz and Galvin
Paging Hardware
© Image from Silberschatz and Galvin
Paging Example
Here, the logical page “0” is located at frame “5”. Page “5” in physical memory is at
address: 5 * 4 = 20. This is because, each page/frame stores 4 bytes. Assume this is a
byte addressable memory.
© Silberschatz and Galvin
32-byte memory and 4-byte pages
Free Frames
The Operating System maintains a free frame list – which it can then easily allocate to new processes. Here is a
before and after allocation of a frame diagram.
Before allocation
© Image from Silberschatz and Galvin
After allocation
Demand
Paging
• Some programs hog memory resources: they
are huge and eat up too much physical
memory.
• Most systems attack this problem by only bringing into the
physical memory portions of the process that are actually
needed.
•
•
In other words, some pages in the process are not loaded
into the physical memory – as long as they are not needed.
This is called demand paging.
Memory (4)
Summary
• Cache access
• Virtual memory
Memory (4)
Download