CSI3131 Mod 7 Mem Manag
CSI3131 Mod 7 Mem Manag
Systems
I/O g OS
Swap u ti n Overview
Management omp ems
C yst
t o ra ge I n S
S tr o CPU Operations
I / O du
Hard Drive a nd ct i o
Management n Memory Hard Drive
CSI3131 Structure
Peripherals
Topics
M ag
M
em em
an
c es s
or en
P ro nt
y t
m e
age
Sy d D e
o ry M a n P
an
e m roce
nc
al M ss
V ir t u Th re e s &
hro adlo
ads
niz cks
ati
U g
on
P
C u l in
e m ory IP C
cM ed
Basi germtn Sc
h
a
Man
Module 7: Memory Management
Reading: Chapter 8
2
Memory Paging
Management Segmentation
Continuous
Background Memory Allocation
Basic
Hardware
Address
Binding
Physical vs
Logical
Addressing
Swapping
3
Memory Management
Challenge:
To accommodate multiple processes in main memory…
Allocate and deallocate the memory for them
Make sure they do not corrupt each other’s memory
Make sure that all of this is done efficiently and
transparently
Not concerned with how addresses are generated.
Main requirements
Relocation.
Sharing.
Allocation.
Protection
4
Basic Hardware
CPU accesses main memory for execution of instructions
For protection base and limit registers are used to trap
illegal accesses to memory.
Only OS allowed to change base and limit registers.
All is done in hardware for efficiency.
5
Address Binding
When are addresses of instructions and data bound to
memory addresses?
Compile time: Compiler generates absolute code
Can run only from fixed memory location,
must recompile the code if the memory
location changes
Load time: Compiler generates relocatable code,
loader sets the absolute memory references at
load time
Once loaded, it cannot be moved
6
Logical versus Physical Address Space
The crucial idea is to distinguish between a logical address
and a physical address
A physical address (absolute address) specifies a physical
location in the main memory
These addresses occur on the address bus
A logical address (or virtual address) specifies a location in the
process (or program)
This location is independent of the physical
structure/organization of main memory
Compilers and assemblers produce code in which all
memory references are logical addresses
Relocation and other MM requirements can be satisfied by
clever mapping of logical to physical addresses
Logical and physical addresses are the same in compile-
time and load-time address-binding schemes;
Logical and physical addresses differ in execution-time
7 address-binding scheme
Simple Address Translation
A relative address is an example of logical address
in which the address is expressed as a location
relative to some known point in the program (ex:
the beginning)
Program modules are loaded into the main
memory with all memory references in relative
form
Physical addresses are calculated “on the fly” as
the instructions are executed
For adequate performance, the translation from
relative to physical address must by done by
hardware
8
Translating logical addresses
physical addresses
10
Dynamic address binding and checking
11
Dynamic Loading
By default, a routine is not loaded
It is loaded only when it is called for the
first time
Better memory-space utilization; unused
routine is never loaded
Useful when large amounts of code are
needed to handle infrequently occurring
cases
No special support from the operating
system is required, implemented through
program design
12
Overlays
Keep in memory only those instructions
and data that are needed at any given
time
Needed when process is larger than
amount of memory allocated to it
Implemented by user, no special support
needed from operating system
programming design of overlay structure
is complex
used in small/simple resource-starved
systems
13
Overlays for a Two-Pass Assembler
14
Dynamic Linking
Linking postponed until execution time
Small piece of code, stub, used to locate the
appropriate memory-resident library routine
Stub replaces itself with the address of the routine,
and executes the routine
Operating system needed to check if routine is in
processes’ memory address
Dynamic linking is particularly useful for libraries
If the library is updated/fixed, the new version is
automatically loaded without the need to touch the
executable file
Careful with library versions, forward/backward
compatibility…
15
Swapping
Run out of memory?
Move process out of main memory
Where?
Backing store – fast disk large enough to
accommodate copies of all memory images for
all users; must provide direct access to these
memory images
Modified versions of swapping are found on many
systems (i.e., UNIX, Linux, and Windows)
Major part of swap time is transfer time; total
transfer time is directly proportional to the
amount of memory swapped
Needs intelligent decisions, if serious swapping
starts, you can go for a coffee
16
Schematic View of Swapping
17
Memory Paging
Management Segmentation
Continuous
Background Memory Allocation
Mapping
Basic
and
Hardware
Protcction
Address
Binding Memory
Allocation
Physical vs
Logical Fragmentation
Addressing
Swapping
18
Contiguous Allocation
What do we mean by contiguous allocation?
Each process occupies a contiguous block of
physical memory
Where is the OS kernel?
usually held in low memory, together with the
interrupt vector
With contiguous allocation, a base and a limit
register are sufficient to describe the address
space of a process
Fixed Partitioning or Dynamic Partitioning can be
used
19
Hardware Support for Base/Relocation
and Limit Registers
20
Memory Allocation:
Fixed Partitioning
Partition main memory into
a set of non overlapping
regions called partitions
Partitions can be of equal
or unequal sizes
any process whose size is
less than or equal to a
partition size can be loaded
into the partition
if all partitions are
occupied, the operating
system can swap a process
out of a partition
21
Placement Algorithm with Partitions
Equal-size partitions
If there is an available partition, a process
can be loaded into that partition
• because all partitions are of equal size, it
does not matter which partition is used
If all partitions are occupied by blocked
processes, choose one process to swap out
to make room for the new process
22
Placement Algorithm with Partitions –
unequal sizes
23
Fixed Partitioning – Problems
Main memory use is inefficient. Any
program, no matter how small, occupies an
entire partition. This is called internal
fragmentation.
Unequal-size partitions lessens these
problems but they still remain...
Equal-size partitions was used in early
IBM’s OS/MFT (Multiprogramming with a
Fixed number of Tasks)
24
Memory Allocation: Dynamic
Allocation
Idea: Forget partitions, put a new process into a large enough
hole of unused memory.
The physical memory consists of allocated partitions and
holes – blocks of available memory between already
allocated.
Initially, there is one huge hole containing all free memory
Operating system maintains information about:
a) allocated partitions b) free partitions (holes)
OS OS OS OS
process 8 process 10
25
Dynamic Allocation: an example
30
Memory Paging
Management Segmentation
Continuous
Background Memory Allocation
Program
Mapping Segments
Basic
and
Hardware
Protcction
Segment
Address Table
Binding Memory
Allocation Segmentation
Physical vs Hardware
Logical Fragmentation
Addressing
Examples
Swapping
31
Non-contiguous Allocation
OK, contiguous allocation seems natural,
but we have the fragmentation problem.
Also: how do we grow/shrink the address
space of a process?
Idea: If we can split the process into
several non-contiguous chunks, we
would not have these problems
Two principal approaches:
Segmentation
Paging
Can be combined
32
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,
global variables,
common block,
stack,
33
symbol table, arrays
User’s View of a Program
34
Logical View of Segmentation
1
4
1
3 2
4
35
Segmentation Architecture
How do we logically address a memory location?
Segment number + offset within segment:
<segment-number, offset>
What information do we need to store in order to be able to
translate logical address into physical?
For each segment:
• Start of the segment (base)
• Length of the segment (limit)
Where do we store this information?
Segment table
36
Segmentation Architecture (Cont.)
Relocation.
dynamic
change the base pointer in the segment table entry
Sharing.
shared segments
same segment table entries
Allocation.
first fit/best fit
external fragmentation
Protection
protection bits associated with each segment
code sharing occurs at segment level
37
Segmentation Hardware
How to calculate physical address with segmentation?
38
Details on address translation (in
hardware).
In the program/code.
40
Sharing of Segments
41
Segmentation and protection
Each entry in the segment table contains
protection information
Segment length
User privileges on the segment: read, write,
execution.
• If at the moment of address translation, it is
found that the user does not have access
permissions interruption
• This info can vary from user to user (i.e.
process to process), for the same segment.
42
Discussion of simple segmentation
Advantages: the unit of allocation is
Smaller than the entier program/process
A logical unit known by the programmer
Segments can change place in memory
Easy to protect and share the segments (in principle).
43
Segmentation versus pagination
44
Memory Paging
Management Segmentation
Page
Continuous Tables
Background Memory Allocation
Program Paging
Mapping Segments Hardware
Basic
and
Hardware
Protcction Page Table
Segment Structures
Address Table
Binding Memory Hierarchical
Allocation Segmentation Hashed
Hardware Inverted
Physical vs
Logical Fragmentation
Addressing
Examples
Segmentation
Swapping
with paging
45
Paging
Paging Page
Tables
Paging
Hardware
Divide physical memory into fixed-sized blocks called
Page Table
frames (size is power of 2, usually between 512 and 16M) Structures
Divide logical memory into blocks of the same size Hierarchical
Hashed
called pages Inverted
46
Example of process loading
47
Example of process loading (cont.)
50
Address Translation in Paging
51
Logical-to-Physical Address Translation in
Paging
53
Free Frames
54
Paging
57
Effective Access Time
Associative Lookup = time units
Assume memory cycle time is 1 microsecond
Hit ratio – percentage of times that a page
number is found in the associative registers;
ratio related to number of associative
registers
Hit ratio =
Effective Access Time (EAT)
EAT = (1 + ) + (2 + )(1 – )
=2+–
58
Memory Protection
We can associate several bits with each page
read only/read-write
valid-invalid
these bits are stored for each entry of the page table
59
Valid (v) or Invalid (i) Bit In A Page
Table
60
Shared Pages
Shared code
One copy of read-only (reentrant) code
shared among processes (i.e., text editors,
compilers, window systems).
61
Shared Pages Example
62
Paging
Paging
Hardware
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
63
Hierarchical Page Tables
Idea: The page table itself is paged
A simple technique is a two-level page
table:
The outer page table entries point to the
pages of the real page table
Sometimes (i.e. SPARC), three-level
page tables are used
64
Two-Level Paging Example
A logical address (on 32-bit machine with 4K page size) is divided
into:
a page number consisting of 20 bits
a page offset consisting of 12 bits
Since the page table is paged, the page number is further divided into:
a 10-bit page number
a 10-bit page offset
Thus, a logical address is as follows:
where pi is an index into the outer page table, and p2 is the displacement within the
page number
page of the outer page table page offset
pi p2 d
10 10 12
65
Two-Level Page-Table Scheme
66
Address-Translation Scheme
Address-translation scheme for a two-level 32-
bit paging architecture
68
Hashed Page Table
69
Inverted Page Table
We can have plenty of processes…
… and each one of them has big page
table.
Of course, most of the page table
entries are invalid, as most of the
processes do not fit into memory
Still, we are wasting memory on their
page tables…
70
Inverted Page Table
Now we need only one entry per physical frame,
not per each process’s logical page
But how do we translate from page # to frame #?
We have to search the inverted page table until
we find match for the page # and process ID
That can be really slow….
What to do?
• Use hash table to limit the search to one — or at
most a few — page-table entries
71
Inverted Page Table Architecture
72
Paging
74
Intel 30386 Address Translation
Intel 30386 has segmentation with two level hierarchical paging
75
Linux on Intel 80x86
Uses minimal segmentation to keep memory
management implementation more portable
Uses 6 segments:
Kernel code
Kernel data
User code (shared by all user processes, using
logical addresses)
User data (likewise shared)
Task-state (per-process hardware context)
LDT
Uses 2 protection levels:
Kernel mode
User mode
76
Memory Paging
Management Segmentation
Page
Continuous Tables
Background Memory Allocation
Program Paging
Mapping Segments Hardware
Basic
and
Hardware
Protcction Page Table
Segment Structures
Address Table
Binding Memory Hierarchical
Allocation Segmentation Hashed
Hardware Inverted
Physical vs
Logical Fragmentation
Addressing
Examples
Segmentation
Swapping
with paging
77