06 Memory
06 Memory
Raju Pandey
Department of Computer Sciences
University of California, Davis
Spring 2011
Overview
• Goals of memory management:
Subdividing memory to accommodate multiple processes
Memory needs to be allocated to ensure a reasonable supply of
ready processes to consume available processor time
• Preparing a Program for Execution
Program Transformations
Logical-to-Physical Address Binding
• Memory Partitioning Schemes
Fixed Partitions
Variable Partitions
• Allocation Strategies for Variable Partitions
• Dealing with Insufficient Memory
Application
Program
VirtualAlloc()
exec() VMQuery()
shmalloc() sbrk() VirtualLock() VirtualFree()
getrlimit() ZeroMemory()
Memory Mgr
Memory Mgr
Process Mgr
Process Mgr
Device Mgr
Device Mgr
File Mgr
File Mgr
UNIX Windows
Hardware
• Sharing
Allow several processes to access the same portion of memory
Better to allow each process access to the same copy of the program
rather than have their own separate copy
• Physical Organization
Memory available for a program plus its data may be
insufficient
o Overlaying allows various modules to be assigned the same region
of memory
...
static int gVar;
...
int proc_a(int arg){
...
gVar = 7;
put_record(gVar);
...
}
Code Segment
Relative
Address Generated Code
0000 ...
... Data Segment
0008 entry proc_a Relative
... Address Generated variable space
0220 load =7, R1 ...
0224 store R1, 0036 0036 [Space for gVar variable]
0228 push 0036 ...
0232 call ‘put_record’ 0049 (last location in the data segment)
...
0400 External reference table
...
0404 ‘put_record’ 0232
...
0500 External definition table
...
0540 ‘proc_a’ 0008
...
0600 (symbol table)
...
0799 (last location in the code segment)
Code Segment
Relative
Address Generated Code
0000 (Other modules)
...
1008 entry proc_a
Data Segment
...
Relative
1220 load =7, R1
Address Generated variable space
1224 store R1, 0136
...
1228 push 1036
0136 [Space for gVar variable]
1232 call 2334
...
...
1000 (last location in the data segment)
1399 (End of proc_a)
... (Other modules)
2334 entry put_record
...
2670 (optional symbol table)
...
2999 (last location in the code segment)
• Static binding
Programming time
Compilation time
Linking time
Loading time
• Dynamic binding
Execution time
Multi-programmed:
o Divide memory into partitions of different sizes
Fixed partitions:
1 queue per partition vs 1 queue for all partitions
Sizes: 1, 2, 4, 8, 16
a) 3 blocks allocated
& 3 holes left
b) Block of size 1
allocated
• Hierarchical Paging
10 10 12
where pi is an index into the outer page table, and p2 is the
displacement within the page of the outer page table
• Shared code
One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window
systems).
Shared code must appear in same location in the
logical address space of all processes
4
1
3 2
4
• Relocation.
dynamic
by segment table
• Sharing.
shared segments
same segment number
• Allocation.
first fit/best fit
external fragmentation
0
0
page table
• 50% of the time the page that is being replaced has been
modified and therefore needs to be swapped out
- Copy-on-Write
3. Read the desired page into the (newly) free frame. Update
the page and frame tables.
1 4
2 6 page faults
3
4 5
3 5 4
4 3
• Counter implementation
Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter
When a page needs to be changed, look at the counters to determine which are
to change
• Stack implementation – keep a stack of page numbers in a double link
form:
Page referenced:
o move it to the top
o requires 6 pointers to be changed
No search for replacement
si = size of process pi
S = ∑ si
m = total number of frames m = 64
s si = 10
ai = allocation for pi = i × m
S s2 = 127
10
a1 = × 64 ≈ 5
137
127
a2 = × 64 ≈ 59
137
• Consider I/O. Pages that are used for copying a file from a
device must be locked from being selected for eviction by a
page replacement algorithm.