1. 1
Outlin
e
• Memory management definition
• Binding of Instructions and Data to Memory
• Executable code
• Simple memory management schemes
• Swapping
• Logical vs. Physical Address Space
• Memory protection with dynamic relocation
(MMU)
• Contiguous memory allocation
• Segmentation
• Paging
• Segmentation with paging
• Memory caching
2. Memory
Management
• The CPU fetches instructions and data of
a program from memory;
• Therefore, both the program and its data must
reside in the main memory (RAM and ROM).
• Modern multiprogramming systems are
capable of storing more than one program,
together with the data they access, in the
main memory.
• A fundamental task of the memory
management component of an operating
system is to ensure safe execution of programs
by providing:
– Sharing of memory
– Memory protection
3
3. Memory
Management…
Issues in sharing memory
Transparency
• Several processes may co-exist, unaware of each other,
in the main memory and run regardless of the
number and location of processes.
Safety (or protection)
• Processes must not corrupt each other (nor the OS!)
Efficiency
• CPU utilization must be preserved and memory
must be fairly allocated.
Relocation
• Ability of a program to run in different memory
locations.
19
4. 4
Memory Management:
Definitions
• Relocatable - Means that the program image can
reside anywhere in physical memory.
• Binding - Programs need real memory in which to
reside. When is the location of that real memory
determined?
– This is called mapping logical to physical addresses.
– This binding can be done at compile or run time.
• Compiler - If it’s known where the program will
reside, then absolute code is generated. Otherwise
compiler produces relocatable code.
• Load - Binds relocatable to physical address.
• Execution - The code can be moved around
during execution.
5. 5
Creating an executable
code
• Before a program can be executed by the
CPU, it must go through several steps:
– Compiling (translating) - generates re-
locatable object code.
– Linking - combines the object code into a
single self-sufficient executable code.
– Loading - copies the executable code
into memory.
– Execution - dynamic memory
allocation.
6. Creating an
executable
code
Compiler
Program/source code
Preprocessor
C or C++ program with
macro substitutions
and file inclusions
Assembly code
Assembler
Relocatable object module
Other relocatable
object modules
or library
modules Linker
Executable code
Loader
Absolute machine code
Execution
code
data
Work space
Main
memory 23
7. 7
Functions of a
linker
Linker collects and puts together all the
required pieces to form the executable code.
Issues:
• Relocation
Where to put pieces.
• Cross-reference
where to find pieces.
• Re-organization
new memory layout.
8. 8
Functions of a
loader
A loader places the executable code in main
memory starting at a pre-determined location
(base or start address).
This can be done in several ways, depending
on hardware architecture:
Absolute loading : always loads programs
into a designated memory location.
Relocatable loading : allows loading programs
in different memory locations.
Dynamic (run - time) loading : loads functions when
first called (if ever).
9. 9
Binding of Instructions and Data to
Memory
• Address binding of instructions and data to
memory addresses can happen at three
different stages.
– Compile time: If memory location known a
priori, absolute code can be generated, other wise
relocatable
– Load time: Compiler must generate relocatable
code if memory location is not known at compile
time.
– Execution time: Binding delayed until run
time if the process can be moved during its
execution from one memory segment to another.
– Need hardware support for address maps (e.g.,
base and
11. 11
Simple management
schemes
An important task of a memory
management system is to bring (load)
programs into main memory for execution.
The following contiguous memory
allocation techniques were commonly
employed by earlier operating systems :
• Direct placement
• Overlays
• Partitioning
12. 12
Direct
placement
Memory allocation is
trivial.No
special relocation is needed,
because the user programs
are always loaded (one at a
time) into the same memory
location (absolute loading ).
The linker produces the
same loading address for
every user program.
Examples: Early batch
monitors, MS-DOS
OS(drivers, buffers)
unused
User program
Operating system
0
ma
x
use
r
13. Overlay
s
A program was
organized (by the
user) into a tree-
like structure of
object modules,
called overlays .
The root overlay was
always loaded into
the memory,
whereas the sub
trees were (re-
loaded as needed
by simply overlaying
existing code.)
0.0
1.0 2.0 3.0
1.1 1.2 2.1 2.2 2.3 2.3 2.3
1.1
1.0
0.0
OS
13
2.1
2.0
0.0
OS
Memory
snapshot
Overlay
tree
14. Partitionin
g
A simple method to accommodate several
programs in memory at the same time (to support
multiprogramming) is partitioning.
In this scheme, the memory is divided into a
number of contiguous regions, called partitions.
Two forms of memory partitioning, depending on
when and how partitions are created (and
modified), are possible:
• Static partitioning
• Dynamic partitioning
These techniques were used by the IBM
OS/360 operating system:
MFT (Multiprogramming with Fixed Number of
Tasks) and
MVT (Multiprogramming with Variable Number of
Tasks.)
14
15. Partitioning
…
Static partitioning
Main memory is divided
into fixed number of (fixed
size) partitions during
system generation or
start-up.
Programs are queued to
run in the smallest
available partition.
An executable prepared to
run in one partition may
not be able to run in
another, without being re-
linked.
This technique uses
absolute loading.
Small jobs
Average jobs
Larger jobs
Operatin
g
System
500
k
100
k
10 k
15
Main
memory
16. 16
Partitioning
…
Dynamic
partitioning
• Any number of
programs can be
loaded to memory as
long as there is room
for each.
• When a program is
loaded (relocatable
loading ), it is allocated
memory in exact
amount as it needs.
• Also, the addresses in
the program are fixed
after loaded, so it
cannot move.
The operating system
keeps track of each
partition (their size and
locations in the
memory.)
A
OS
B
A
OS
B
OS
K
OS
Partition allocation at different
times
Main
memory
17. Swappin
g
Operatin
g
system
The basic idea of swapping is to treat main
memory as a ‘‘pre- emptable ’’ resource.
A high-speed swapping device is used as the
backing storage of the pre-empted processes.
Swap-in
Swap-
out
Swapping
device
Memor
y
35
18. 18
Swapping
…
A process can be swapped temporarily out of memory
to a backing store, and then brought back into
memory for continued execution.
Backing store – fast disk large enough to
accommodate copies of all memory images for all
users; must provide direct access to these memory
images.
Roll out, roll in – swapping variant used for priority-
based scheduling algorithms.
lower-priority process is swapped out so higher-priority
process can be loaded and executed.
UNIX, Linux, and Windows.
21. 21
Swapping
…
Swapping is a medium-term scheduling
method.
The responsibilities of a swapper include:
• Selection of processes to swap out
criteria: suspended/blocked state, low priority, time
spent in memory
• Selection of processes to swap in
criteria: time spent on swapping device, priority
• Allocation and management of swap space
on a swapping device.
Swap space can be:
system wide
dedicated
22. 22
Logical vs. Physical Address
Space
The concept of a logical address space that is bound to
a separate physical address space is central to proper
memory management.
Logical address – generated by the CPU; also
referred to as virtual address.
Physical address – address seen by the memory
unit.
Logical and physical addresses are the same in
compile- time and load-time address-binding
schemes;
logical (virtual) and physical addresses differ
in execution-time address-binding scheme.
23. 23
Memory
Protection
The other fundamental task of a memory
management system is to protect programs
sharing the memory from each other.
This protection also covers the operating system
itself.
Memory protection can be provided at either of the
two levels:
Hardware:
address translation
Software:
language dependent: strong typing
language independent: software fault isolation
25. Two views of
memory
Dynamic relocation leads to two different
views of main memory, called address
spaces.
0
SC
0
SB
0
SA
Operatin
g
system
PA starts
PA ends
PB starts
PB ends
PC starts
PC ends
Physical view
(physical address
space) 25
Logical
view
(logical address
space)
26. 26
Memory-Management Unit
(MMU)
Hardware device that maps virtual to
physical address.
In MMU scheme, the value in the
relocation register is added to every
address generated by a user process at
the time it is sent to memory.
The user program deals with logical
addresses; it never sees the real
physical addresses.
28. 28
Contiguous
Allocation
Main memory is divided into two partitions:
Resident operating system, usually held in low
memory with interrupt vector.
User processes then held in high memory.
Single-partition allocation
Relocation-register scheme used to protect
user processes from each other, and from
changing operating-system code and data.
Relocation register contains value of smallest
physical address;
Limit register contains range of logical addresses –
each logical address must be less than the limit register.
29. Hardware Support for Relocation and Limit
Registers for memory protection discussed
before
29
30. Contiguous Allocation
(Cont.)
Multiple-partition allocation
Hole – block of available memory; holes of various
size are scattered throughout memory.
When a process arrives, it is allocated memory
from a hole large enough to accommodate it.
Operating system maintains information about:
a) allocated partitions b) free partitions
(hole)
OS
process 5
process 8
process 2
OS
process 5
process 2
OS
process 5
process 2
OS
process 5
process 9
process 2
process 9
process 10
Main
Memory
48
31. 31
Dynamic Storage-Allocation
Problem
How to satisfy a request of size n from a list
of free holes.
First-fit: Allocate the first hole that is big
enough.
Best-fit: Allocate the smallest hole that is big
enough; must search entire list, unless ordered by
size. Produces the smallest leftover hole.
Worst-fit: Allocate the largest hole; must also
search entire list. Produces the largest leftover
hole.
First-fit and best-fit better than worst-fit in terms
of speed and storage utilization.
32. 32
Fragmentatio
n
Fragmentation refers to the unused memory
that the management system cannot allocate.
Internal fragmentation
o Waste of memory within a partition, caused by the
difference between the size of a partition and the
process loaded.
o Severe in static (fixed) partitioning schemes.
External fragmentation
Waste of memory between partitions, caused by
scattered non-contiguous free space.
Severe in dynamic (variable size) partitioning schemes.
Compaction is a technique that is used to overcome
external fragmentation.
33. Gentle reminder: Base and bounds
relocation
Each program is
loaded into a
contiguous region of
memory.
This region appears to
be ‘ ‘private’ ’ and the
bounds register limits
the range of the logical
address of each
program.
Hardware
implementation is
cheap and efficient:
2 registers plus an
adder and a
comparator.
Logical Address
Registers
bounds
base
>
+
Physical Address
Fault!
51
34. 34
Segmentatio
n
The most important problem with base-and-
bounds relocation is that there is only one
segment for each process.
Segmentation is a memory management scheme that
supports user’s view of memory.
A segment is a region of contiguous memory.
Segmentation generalizes the base-and-
bounds technique by allowing each process to
be split over several segments.
A segment table holds the base and bounds of each
segment.
Although the segments may be scattered in memory,
each segment is mapped to a contiguous region.
Additional fields (Read/Write and Shared) in the
segment table adds protection and sharing capabilities
to segments.
35. 35
Segmentation
…
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
36. 36
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.
40. 40
Segmentation
…
When a process is created, a pointer to an empty
segment table is inserted into the process control
block.
Table entries are filled as new segments are allocated for
the process.
The segments are returned to the free segment pool
when the process terminates.
Segmentation, as well as the base and bounds
approach, causes external fragmentation and
requires memory compaction.
An advantage of the approach is that only a segment,
instead of a whole process, may be swapped to make
room for the (new) process.
41. 41
Pagin
g
Paging is a memory-management scheme that
permits the physical address space of a process to
be noncontigious.
Divide physical memory into fixed-sized blocks
called frames (size is power of 2, between 512
bytes and 16 mbytes per page).
Divide logical memory into blocks of same size
called
pages.
Keep track of all free frames.
To run a program of size n pages, need to find n
free frames and load program.
Set up a page table to translate logical to
physical addresses.
Problem: Internal fragmentation.
42. Paging
…
Physical memory is divided into a number of fixed
size blocks, called frames.
The logical memory is also divided into chunks of
the same size, called pages.
The size of frame/page is determined by the
hardware and typically is some value between 512
bytes (VAX) and 16 megabytes (MIPS 10000)!
A page table maps the base address of pages for
each frame in the main memory.
The major goals of paging are:
to make memory allocation and swapping easier and
to reduce fragmentation.
Paging also allows allocation of non-contiguous memory
(i.e.,
pages need not be
adjacent.)
62
43. 43
Address Translation
Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page
table which contains base address of each page
in physical memory.
Page offset (d) – combined with base address to
define the physical memory address that is
sent to the memory unit.
48. 48
Paging
…
Paging itself is a form of dynamic relocation.
Every logical address is bound by the paging HW
to some physical address.
Using paging is similar to using a table of base
(relocation) registers, one for each frame of
memory.
When using paging scheme, there is no
external fragmentation.
Any free frame can be allocated to a process
that needs it.
However, we may have some internal
fragmentation.
49. 49
Implementation of Page
Table
Page table is kept in main memory.
Page-table base register (PTBR) points to the
page table.
Page-table length register (PRLR) indicates size of
the page table.
In this scheme every data/instruction access
requires two memory accesses.
One for the page table and
one for the data/instruction.
The two memory access problem can be solved by
the use of a special fast-lookup hardware cache
called associative memory or translation look-aside
buffers (TLBs)
50. 50
Associative
memory
Problem: Both paging and segmentation
schemes introduce extra memory references
to access translation tables.
Solution? Translation buffers.
Based on the notion of locality (at a given time a
Process is only using a few pages or segments) ,
a very fast but small associative (content
addressable) memory is used to store a few of
the translation table entries.
This memory is known as a translation look-
aside buffer or TLB
51. 51
Hardware support for
paging
There are different hardware
implementations of page tables to support
paging.
A set of dedicated registers, holding
base addresses of frames.
In memory page table with a page table
base register (PTBR).
Same as above with multi-level page
tables.
52. Memory
catching
Similar to storing memory
addresses in TLBs,
frequently used data in main
memory can also be stored in
fast buffers, called cache
memory, or simply cache.
memory access occurs as
follows:
2n-1
for each memory reference
if data is not in cache
<miss> if cache is full
remove some data (make
space)
if read access
issue memory
read
place data in cache
return data
else <hit>
if read
access
return data
else
98
53. Cache
Memory…
Memory access may
be slow
Cache is small but
fast memory close
to processor
Holds copy of part
of memory
Hits and misses
Processor
Memory
Cache
Fast/expensive technology,
usually on the same chip
103
Slower/cheaper technology,
usually on a different chip