You are on page 1of 34

Memory Management

Fred Kuhns
(fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk)

Department of Computer Science and Engineering


Washington University in St. Louis

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Recall the Von Neumann Architecture

Central Processing Unit (CPU)

Arithmetic-Logical Unit
(ALU) Control Unit

Device Controller
Primary Memory Device Controller
Device
Device Controller
Device
Device Controller
Device
Device

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 2


Memory Hierarchy

CPU Registers ~500 Bytes


1 clock cycle

Executable
Cache Memory
<10MB
Memory 1-2 Clock cycles

Primary Memory <1GB


1-4 Clock cycles

Rotating Magnetic Memory < 100GB (per device)


5-50 usec

Secondary
Optical Memory < 15GB (per device)
Storage 25 usec – 1 sec

Sequential Accessed Memory < 5GB (per tape)


seconds

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 3


Principle of Locality
• Well designed programs tend to access groups of
variables/objects which changes slowly with
time. Likewise, instructions tend to be clustered
in groups (for loops) that are repeated for some
period of time.
• Hardware and software are implemented to
exploit the principle of locality.
– Temporal locality: if location is referenced once, then
it is likely to be referenced again in the “near” future.
– Spatial locality: if a memory location is referenced
then other “nearby” locations will be referenced.
• Stride-k (data) reference patterns
– visit every kth element of a contiguous vector.
– stride-1 reference patterns are very common.

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 4


Caching: A possible Scenario
Client Host Web Server
CPU DRAM Disk
page.html
4 (Primary) (files) page.html
page.html
cache page.html
image.jpg image.jpg

3 2

1. Copy of web page moved to a file on the client (cached).


2. Part of the file is copied into primary memory so program
can process data (cached)
3. A cache “line” is copied into cache for program to use
4. Finally the individual words are copied into CPU registers
as they are manipulated by program
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 5
Caching terminology
• Cache hit if requested data is found in cache
• Cache miss if data not there
– cold miss: cache is empty
– conflict miss: cache line occupied by a different
memory location
– capacity miss: working set is larger than cache
• Placement policy – where new block is placed
• Replacement policy – controls which block is
selected for eviction

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 6


Cache/Primary Memory Structure
Set Cache
Memory Number
Address V Tag Block
0 Set 0 V Tag Block
1 ...
2 Block
3
V Tag Block
Set S-1 V Tag Block

E lines per set


S = 2s sets in the cache
B = 2b data Bytes per line
M = 2m = max memory address
t = m – (s+b) tag bits per line
1 valid bit per line
Block (may also require a dirty bit)
Cache size = C = B * E * S
2n - 1 m address bits (= t + s + b)
Word Length t bits s bits b bits

Address
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 7
Cache Design
• Write policy
– hit: write-through versus write-back
– miss: write-allocate versus no-write-allocate
• Replacement algorithm
– determines which block to replace (LRU)
• Block size
– data unit exchanged between cache and main
memory

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 8


Primary Memory
• Primary Memory Design Requirements
1. Minimize access time: hardware and software
requirement
2. Maximize available memory: using physical and virtual
memory techniques
3. Cost-effective: limited to a small percentage total

• Memory Manager Functions


1. Allocate memory to processes
2. Map process address space to allocated memory
3. Minimize access times while limiting memory
requirements

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 9


Process Address Space
• Compiler produces relocatable object
modules

• Linker combines modules into an absolute


module (loadable module).
– addresses are relative, typically starting at 0.

• Loader loads program into memory and


adjusts addresses to produce an
executable module.
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 10
UNIX Process Address Space
Low Address
(0x00000000)
Text (shared)

Initialized Data
Unitialized Data
Process
Address space Heap (Dynamic)

stack (dynamic)

High Address Environment


(0x7fffffff)

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 11


Big Picture
kernel memory

proc struct

kernel stack/u area kernel stack/u area kernel stack/u area


Stack Stack Stack

Data Data Data


Text (shared) Text (shared) Text (shared)

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 12


Next …
• Memory Partitioning schemes
– Fixed partitions
– Dynamic partitions
– Simple paging
– simple segmentation
• Relocation

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 13


Memory Management: Requirements
• Relocation
– Why/What:
• programmer does not know where the program will be placed in memory when it is
executed
• while the program is executing, it may be swapped to disk and returned to main
memory at a different location
– Consequences/Constraints:
• memory references must be translated in the code to actual physical memory
address
• Protection - Protection and Relocation are interrelated
– Why/What:
• Protect process from interference by other processes
• processes require permission to access memory in another processes address
space.
– Consequences/Constraints:
• impossible to check addresses in programs since the program could be relocated
• must be checked at run time
• Sharing - Sharing and Relocation are interrelated
– allow several processes to access the same data
– allow multiple programs to share the same program text

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 14


Fixed Partitioning
• Partition available memory into regions
with fixed boundaries
• Equal-size partitions
– process size <= partition size can be loaded
into available partition
– if all partitions are full, the operating system
can swap a process out of a partition
– If program size > partition size, then
programmer must use overlays
• Unequal-size partitions
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 15
Fixed Partitions
• Main memory use is inefficient, suffers from Internal
Fragmentation - Part of partition unused
Operating System
Operating System
8M 8M

8M 2M
process 1 4M
process 1
6M
8M
Unused
8M
8M
8M

8M
12 M

Equal Sized Partitions Unequal Sized Partitions


Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 16
Placement Algorithm with Partitions
• Equal-size partitions
– because all partitions are of equal size, it does
not matter which partition is used
• Unequal-size partitions
– can assign each process to the smallest
partition within which it will fit
• queue for each partition
– processes are assigned in such a way as to
minimize wasted memory within a partition

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 17


One Process Queue per Partition
• Wait for “best” partition
Operating
System

New
Processes

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 18


One Process Queue for All
• smallest available partition that will
hold the process is selected
Operating System

New
Processes

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 19


Variable Partitioning
• External Fragmentation - small holes in
memory between allocated partitions.
• Partitions are of variable length and
number
• Process is allocated exactly as much
memory as required
• Must use compaction to shift processes so
they are contiguous and all free memory is
in one block

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 20


Example Dynamic Partitioning

Operating Operating Operating


128 K
System System System

Process 1 320 K Process 1 320 K

Process 2 224 K
896 K
576 K
352 K

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 21


Example Variable Partitioning

Operating Operating Operating


System System System
Process 1 320 K Process 1 320 K Process 1 320 K

224 K 224 K Process 4 128 K


Process 2
96 K

Process 3 288 K Process 3 288 K Process 3 288 K

64 K 64 K 64 K

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 22


Example Variable Partitioning

Operating Operating
System System
320 K Process 2 224 k
96 K
Process 4 128 K Process 4 128 K
96 K 96 K

Process 3 288 K Process 3 288 K

64 K 64 K

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 23


Variable Partition Placement Algorithm
• Best-fit : generally worst performer overall
– place in smallest unused block (that process fits in)
– results in minimally sized fragments requiring compaction
• Worst-fit
– place in largest unused block
• First-fit : simple and fast
– scanning from beginning and choose first that is large enough.
– may have many process loaded in the front end of memory that must
be scanned
• Next-fit : tends to perform worse than first-fit
– scan memory from the location of the last allocation and chooses the
next available block that is large enough
– more often allocate a block of memory at the end of memory where
the largest block is found
– compaction is required to obtain a large block at the end of memory

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 24


Variable Partition Placement Algorithm
alloc 16K block
8K 8K

12K First Fit 12K


22K
Last Best Fit 6K
allocated 18K
block (14K) 2K

8K 8K
6K 6K

Allocated block
14K Free block 14K
Next Fit

36K
20K
Before After
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 25
Addresses
• Logical Address
– reference to a memory location independent of
the current assignment of data to memory
– translation from logical physical address
• Relative Address (type of logical address)
– address expressed as a location relative to
some known point
• Physical Address
– the absolute address or actual location

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 26


Relocation
• Fixed partitions: When program loaded
absolute memory locations assigned
• In general a process may occupy
different partitions over time
– swapping
– Compaction causes a program to occupy a
different partition which means different
absolute memory locations
• relative address can be used with HW
support – Dynamic Address Relocation
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 27
Hardware Support for Relocation
Relative address

Process Control
Base Register Block

Adder Program
Absolute
Bounds Register Comparator address

Data
Interrupt to
operating system

Stack

Process image in
main memory

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 28


Registers Used during Execution
• Register values are set when the process is
loaded and when the process is swapped in
• Base register: starting address for the
process
– The value of the base register is added to a
relative address to produce an absolute address
• Bounds register: ending location of the
process
– The resulting address is compared with the value
in the bounds register
– If the address is not within bounds, an interrupt
is generated to the operating system
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 29
Paging
• Partition memory into small equal-size
chunks
– Chunks of memory are called frames
• Divide each process into the same size
chunks
– Chunks of a process are called pages
• Operating system maintains a page table
for each process
– contains the frame location for each process
page
– memory address = page number + offset
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 30
Paging
Frame physical memory
Process A loaded Process A & B loaded
Number
0 0 A.0 0 A.0
1 1 A.1 1 A.1
2 2 A.2 2 A.2
3 3 A.3 3 A.3
4 4 4 B.0
5 5 5 B.1
6 6 6 B.2
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 31
Paging
0 A.0 0 A.0 0 A.0
1 A.1 1 A.1 1 A.1
2 A.2 2 A.2 2 A.2
3 A.3 3 A.3 3 A.3
4 B.0 4 4 D.0
5 B.1 5 5 D.1
6 B.2 6 6 D.2
7 C.0 7 C.0 7 C.0
8 C.1 8 C.1 8 C.1
9 C.2 9 C.2 9 C.2
10 C.3 10 C.3 10 C.3
11 11 11 D.3
12 12 12 D.4
13 13 13
14 14 14
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 32
Page Tables : Example
0 0 A.0
0
1 A.1
1 1 0 ---
2 2 2 A.2
1 ---
3 3
2 ---
3 A.3
Process A Process B 4 D.0
5 D.1
0 4
0 6 D.2
7 1 5
1 8 2 6 7 C.0
2 9 3 11 8 C.1
3 10 4 12 9 C.2
Process C Process D 10 C.3
11 D.3
13 12 D.4
14 13
Free Frame List 14
Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 33
Segmentation
• All segments of all programs do not have
to be of the same length
• There is a maximum segment length
• Addressing consist of two parts - a
segment number and an offset
• Since segments are not equal,
segmentation is similar to dynamic
partitioning

Fred Kuhns (01/17/09) Cs422 – Operating Systems Organization 34

You might also like