How to manage memory. In the past, this has been the key resource (only after the CPU). Effective solution requires both hardware and software support. Key issues: speed and flexibility.
Recall:
+---------+
+---------+ \
+---------+ \|
+---------+ \|+
+---------+ \|+ e.g. memory.c
| Source \|+
| Program |+
+----------+
|
v
+----------+ \
| Compiler | |
| or | e.g. gcc > Compile time
| Assembler| |
+----------+ /
|
v
+----------+ By and large, UNIX does
| Object | e.g. memory.o not use file types, but
| Module | systems software (like
+----------+ loaders) rely on "magic
| numbers", i.e., a systems
+---------+ | +---------+ \ routine will pick a particular
| Compiler| | | User |+--------+ | integer value at the beginning
| Object | | | Object ||other | | of a file (to identify it as
| Library | | | Library ||libs, X | | an object file, for example).
+---------+ | +---------+|windows | | Another systems program will
| v | +--------+ | check the magic number to see
| +----------+ | | | if things make sense.
-> | Link |<- | |
| Editor |<--------- > Load time
+----------+ |
+---------+ | |
| System | | |
| Library | | |
+---------+ V |
| +----------+ |
| | Loader | /
->| |
+----------+
|
v +------------+ \
+----------+ | Dynamically| |
| binary | | Loaded | |
| image in | | Binaries | > Execution time
| mem ready| +------------+ |
| to exec. | | |
+----------+<------- /
Alternatives:
+-------------+ 0
| |
| OS |
| | OS usually loaded in low memory.
+-------------+
| |
| Static |
| |
| User |
| |
| Space |
| |
+-------------+
| Dynamic |
| User |
| Space |
+-------------+
|/////////////|
|/////////////|
|/////////////|
|/////////////|
|/////////////|
|/////////////|
|/////////////|
|/////////////|
+-------------+ Max Mem.
+-------------+ 0
| |
| Static |
| OS | OS usually loaded in low memory.
| Space |
+-------------+
| Dynamic |
| OS Space | |
+-------------+ | Can grow as needed
|/////////////| V
|/////////////|
|/////////////|
|/////////////|
+-------------+
| Dynamic |
| User | ^
| Space | | Can grow as needed
+-------------+ |
| Static |
| User |
| Space |
+-------------+ Max Mem.
+----------+ +---+
| xxx| Rel. Reg. | |
+----------+ | | physical
+---+ logical | physical | |
|CPU|-------------> + ----------------->| | memory
+---+ address address | |
| |
| |
+---+
or base & limit registers:
limit+------+ +----------+ +---+
reg | | | xxx| Rel. Reg. | |
+------+ +----------+ | | physical
+---+ logical | yes | physical | |
|CPU|------> < -----------> + ----------------->| | memory
+---+ address | address | |
| no | |
V | |
error +---+
we have more flexibility. This simplifies system software by delaying BINDING TIME physical addresses.
+-------------+ 0
| OS |
| Space |
+-------------+ Suppose P1 completes. Then what?
| P1 |
| | Scan job queue for next process to
| | inititate.
| | First fit
+-------------+ Best fit
| P2 | Worst fit
| |
| | Reassignment of memory (after process
+-------------+ complete) usually results in external
| P3 | fragmentation.
+-------------+
|/////////////|
+-------------+ Max. mem
Can address fragmentation problems with compaction (if its worth the overhead). This causes problems with I/O, DMA.
address +----+
logical ---------------- | |
address / V | |
+---+ +---+---+ +---+---+ | | p: offset into
|CPU|------| p | d | | f | d |---->| | page table
+---+ +---+---+ +---+---+ | | d: offset into
| ^ | | page
| +---+ | | |
| / | | | | |
| p < | | | | |
| \ | | | | |
-----> | f | ----- +----+
| | frames
| |
+---+
page table
Disadvantage: Size of page tables. Internal fragmentation
HW support (TLB, etc) To avoid two memory refs (remember, relative to CPU speeds, accesses to memory are slow), have small associative memory (also called associative registers and translation look-aside buffers).
Typically a 10% time increase in unmapped memory references.
Too many pages to keep entire page table in associative memory. Only keep those actively referenced.
Example:
effective access time = 0.9 * 110 + 0.1 * 210
= 120
(if hit rate were 0.98, then 112, not very different from 110)
Motorola 68030 has a 22 entry Translation Look-aside Buffer;
Intel 80486 has 32 registers, claims hit rate of 0.98.
This scheme supports some memory protection and shared memory.
Primary use of shared memory is to keep only one copy of executable memory resident; several different processes can all run the same copy. Each has its own program counter. Also requires a compiler which generates reentrant code: each process has its own memory location for all local variables.
Programs consist of memory related meaningful units. pages (as above) are entirely artificial. Say, ftn1, ftn2, memblock1, memblock2, ..., main. (memblocks may be arrays, set of local variables associated with a particular procedure).
/ +-----+-----+
| | | |
-------> s < | | |
| | | | |
+---+ | \ |limit| base| +----+
|CPU|--> (s,d) | | | | |
+---+ | | | | | |
| +-----+-----+ | |
| / \ | |
| / yes \ | |
-----------> < ------> + -->| |
| | |
|no | |
v | |
trap +----+
This can easily support memory sharing.
Advantage: If routine never called, never loaded.
Problem: Fragmentation (sige segments are of different sizes)
Paged Segmentation
+---+ +---+---+---+
|CPU|-->| s | p | d |
+---+ +---+---+---+
| |
| ------------------------+----
| V | +---+
| ------------> >= | | |
| | | | | |
| +------+----+ V | | |
| /| | | trap | | |
V s+r | | | | | / +---+ | |
+ ---- < | | | | | | | | |
^ | | | | | | | | | |
| \|length|ptbr|----- t V p+t< | | | |
| r | | | -----> + | | | +-+-+ | |
| | | | \ | f |- |f|d|->| |
STBR +------+----+ | | +-+-+ | |
Segment table +---+ | |
| |
| |
+---+
Based on Multics.
(Finally) Virtual Memory
Motivations:
Virtual memory gives programs to appearance of access to much larger physical memory than really exists. Performance depends on:
+---+ +---+-+ +---+
0 | A | 0 | 4 |x| 0 | |
1 | B | 1 | | | 1 | |
2 | C | 2 | 6 |x| 2 | | -----------
3 | D | 3 | | | 3 | | / \
4 | E | 4 | | | 4 | A | |\ /|
5 | . | 5 | 9 |x| 5 | | | ------------ |
6 | . | 6 | | | 6 | C | | A E |
| |
. . . . . . | BC |
| |
n | . | n| | | n | | \ D /
+---+ +----+-+ +---+ ------------
Logical view page ^ main backing store
of memory table | memory (disk)
(program view in mem \ /
flag ------------v-------------
Physical Memory
As always, this isn't always easy. Depends in part on architecture of machine. In particular, on some machines, "partially executed" instructions are a problem. Most instructions cause no problems -- can reexecute in entirety. But some do. Examples:
MOV (R2)+,-(R3) copies contents of register R2 into location pointed to by register R3 and R2 is incremented by 2, R3 decremented by 2 (since PDP is byte addressable) Must reset registers if instruction does not complete.
Must be able to undo effect of partially completed instruction.
Overhead of Demand Paging
When page fault occurs:
| Index | Previous | Next |
|---|
Copyright ©2004, G. Hill Price
Send comments to G. Hill Price