CSE323 Memory Management
These slides were compiled from the OSC textbook slides (Silberschatz, Galvin,
and Gagne) and the instructor’s class materials.
CSE323 Memory Management 1
Base and Limit
Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be sure it is
between base and limit for that user
Hardware Address Protection with Base and Limit Registers
Address Binding
Programs on disk, ready to be brought into memory to execute form an input queue
Inconvenient to have first user process physical address always at 0000
Addresses represented in different ways at different stages of a program’s life
Source code addresses are usually symbolic
A Compiler bind this symbolic address to relocatable addresses
i.e. “14 bytes from beginning of this module”
Linker or loader will bind relocatable addresses to absolute addresses
i.e. 74014
Each binding maps one address space to another
From Source to Executable Machine
memory
source object
program modules
other
foo.c foo.o
programs
main() main load ...
sub1() sub1 module ?
Compiler data main
data a.out
sub1 (system
main
data calls)
sub1
static (Run Time) printf
data
library exit
printf Loader
libc.a data
exit
printf data
Linkage other
scanf
Editor ...
gets
fopen ...
exit
data “Load time”
... kernel
Dynamic library case not shown
CSE323 Memory Management 5
Binding of Instructions and Data to
Memory Addresses
Compile time: If memory location known a priori, absolute code can be
generated; must recompile code if starting location changes.
Load time: If memory location is not known at compile time, compiler
must generate relocatable code.
Loader knows final location and binds addresses for that location
Execution time: If the process can be moved during its execution,
binding must be delayed until run time. Need hardware support for
address maps (e.g., base and limit registers).
CSE323 Memory Management 6
Memory-Management Unit
(MMU)
MMU is the Hardware device that maps virtual address to physical address at run time
To start, consider simple scheme where the value in the relocation register is added to
every address generated by a user process at the time it is sent to memory
Base register now called relocation register
MS-DOS on Intel 80x86 used 4 relocation registers
The user program deals with logical addresses; it never sees the real physical
addresses
Execution-time binding occurs when reference is made to location in memory
Logical vs. Physical
Address Space
Dynamic relocation using a
relocation register
Physical address: The
actual hardware memory
address.
32-bit CPU’s physical
address 0 ~ 232-1
(00000000 – FFFFFFFF)
128MB’s memory
address0 ~ 227-1
(00000000 – 07FFFFFF)
Logical address: Each
(relocatable) program
assumes the starting
location is always 0 and
the memory space is
much larger than actual
memory
CSE323 Memory Management 8
Memory Protection
For each process
Logical space is mapped to a contiguous portion of physical space
A relocation and a limit register are prepared
Relocation register = the value of the smallest physical address
Limit register = the range of logical address space
CSE323 Memory Management 9
Memory Protection
When the CPU scheduler selects a process for
execution, the dispatcher loads the relocation
and limit registers with the correct values as
part of the context switch.
Every address generated by the CPU is
checked against these registers.
CSE323 Memory Management 10
Dynamic Loading
Unused routine is
never loaded
Useful when the code
size is large
Unix execv can be
main( ) { categorized:
f1( ); 1. Loaded when called Overloading a
} necessary program
f1( ) { 2. Loaded when called
memory onto the current
f2( ); program.
}
f2( ) { 3. Loaded when called
f3( );
}
CSE323 Memory Management 11
Dynamic Linking
Linking postponed until
execution time.
Small piece of code,
stub, used to locate
int x;
the appropriate
int x;
void main(){ void main(){ memory-resident
stub = dlopen(“lib”): stub = dlopen(“lib”): library routine.
f = dlsym(stub, “f1”); f = dlsym(stub, “f1”); Stub replaces itself
f( ); f( );
} }
with the address of the
memory routine, and executes
lib.so.a the routine.
extern int x; extern int x; Operating system
f1( ) { f1( ) {
x = 5;
needs to check if
x = 5;
} routine is in processes’
}
memory address
CSE323 Memory Management 12
Overlays
Code and data used
at any given time
are placed in
memory.
New code is
overloaded onto the
code not used any
more.
It is not so frequently
used now.
CSE323 Memory Management 13
Swapping
When a process p1
is blocked so long
(for I/O), it is
swapped out to the
backing store, (swap
area in Unix.)
When a process p2
is (served by I/O and
) back to a ready
queue, it is swapped
in the memory.
Use the Unix top
command to see
which processes are
swapped out.
CSE323 Memory Management 14
Contiguous Memory
Allocation
The main memory is usually divided into two
parts : one for the operating system and one
for the user processes.
Where the OS will reside will decide upon the
location of interrupt vector.
CSE323 Memory Management 15
Memory Allocation
(Fixed sized partition)
Memory is divided to fixed-
OS
sized partitions
Each partition is allocated to
a process
process1 IBM OS/360
Then, how about this
process?
process2 ?
process4
process3
CSE323 Memory Management 16
Variable-Sized Partitions
Primarily used in Batch environment
The OS keeps a table indicating which parts of
memory are available and which are occupied
Initially all memory is available for the user
processes and is considered as one large block
of available memory, “HOLE”.
When a process arrives and needs memory, we
search for a hole large enough for this process.
If we find one, we allocate only as much
memory as is needed.
CSE323 Memory Management 17
Variable-Sized Partitions
Whenever one of running processes, (p8) is terminated
Find a ready process whose size is best fit to the hole, (p9)
Allocate it from the top of hole
If there is still an available hole, repeat the above (for p10).
Any size of processes, (up to the physical memory size) can
be allocated.
OS OS OS OS
process 5 process 5 process 5 process 5
process 9 process 9
process 8 process 10
process 2 process 2 process 2 process 2
CSE323 Memory Management 18
Dynamic Storage-
Allocation Problem
First-fit: Allocate the first hole that is big enough. (Fastest
search)
Best-fit: Allocate the smallest hole that is big enough; must
search entire list, unless ordered by size. Produces the
smallest leftover hole. (Best memory usage)
Worst-fit: Allocate the largest hole; must also search entire
list. Produces the largest leftover hole (that could be used
effectively later.)
http://thumbsup2life.blogspot.com/2011/02/best-fit-first-fit-and
-worst-fit-memory.html
First-fit and best-fit better than worst-fit in terms of
speed and storage utilization.
CSE323 Memory Management 19
Example Size
Memory Block
Block 1 50
Block 2 200
List of Jobs Size Turnaroun Block 3 70
d
Block 4 115
Job 1 100k 3
Job 2 10k 1
Block 5 15
Job 3 35k 2
Job 4 15k 1
Job 5 23k 2
Job 6 6k 1
Job 7 25k 1
Job 8 55k 2
Job 9 88k 3
Job 10 100k 3
CSE323 Memory Management 20
Best Fit (Turn 1 and 2)
Block Size Process
1 50 P3
2 200 P5
3 70 P4
4 115 P1
5 15 P2
Block Size Process
1 50 P3
2 200 P5
3 70 P7
4 115 P1
5 15 P6
CSE323 Memory Management 21
Best Fit (Turn 3 and 4)
Block Size Process
1 50
2 200 P9
3 70 P8
4 115 P1
5 15
Block Size Process
1 50
2 200 P9
3 70 P8
4 115 P10
5 15
CSE323 Memory Management 22
Best Fit (Turn 5 and 6)
Block Size Process
1 50
2 200 P9
3 70
4 115 P10
5 15
Block Size Process
1 50
2 200
3 70
4 115 P10
5 15
CSE323 Memory Management 23
External Fragmentation
Problem
50-percent rule (for first
fit): total memory space
process1 exists to satisfy a
request, but it is not
contiguous.
Can’t fit
Solution
Shift up Compaction: shuffle the
process3 memory contents to
place all free memory
together in one large
process2 block
Relocatable code
Expensive
Paging: Allow non-
contiguous logical-to-
phyiscal space mapping.
CSE323 Memory Management 24
Internal Fragmentation
With the scheme of breaking the physical
memory into fixed-sized blocks, and allocate
memory in unit of block size, results internal
fragmentation.
With this approach, the memory allocated to a
process may be slightly larger than the
requested memory. The difference between
these two number is internal fragmentation.
CSE323 Memory Management 25
Segmentation
Memory-management scheme that
supports user view of memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
User’s View of a Program
Logical View of
Segmentation
Data
Data Heap
Stack
Code
Heap Stack
Code
user view of memory logical memory space
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Segment table – maps two-dimensional physical
addresses; each table entry has:
base – contains the starting physical address where
the segments reside in memory
limit – specifies the length of the segment
Segment-table base register (STBR) points to the
segment table’s location in memory
Segment-table length register (STLR) indicates
number of segments used by a program;
segment number s is legal if s < STLR
Segmentation Architecture
(Cont.)
Protection
With each entry in segment table associate:
validation bit = 0 illegal segment
read/write/execute privileges
Protection bits associated with segments; code sharing
occurs at segment level
Since segments vary in length, memory allocation is a
dynamic storage-allocation problem
A segmentation example is shown in the following
diagram
Segmentation Hardware
Segmentation Example
offset d2
offset d1 SP d1
used Currently executed
PC
d2
Stack top
used
CSE323 Memory Management 32
CSE323 Memory Management 33