Professional Documents
Culture Documents
Memory Management
Memory Management
3
Memory Management
• Background
• Requirements
• Swapping
• Contiguous Memory Allocation
• Paging
• Structure of the Page Table
• Segmentation
4
Memory Management Requirements
• Relocation
• 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
• memory references must be translated in the code to actual
physical memory address
Memory Management Requirements
• Protection
• processes should not be able to reference memory locations in another process
without permission
• impossible to check addresses in programs since the program could be
relocated
• must be checked during execution
Memory Management Requirements
• Sharing
• allow several processes to access the same portion of memory
• better to allow each process (person) access to the same copy of the program
rather than have their own separate copy
Memory Management Requirements
• Logical Organization
• programs are written in modules
• different degrees of protection given to modules (read-only, execute-only)
• share modules
Memory Management Requirements
• Physical Organization
• memory available for a program plus its data may be insufficient
• overlaying allows various modules to be assigned the same region of memory
• secondary memory cheaper, larger capacity, and permanent
Background
10
Base and Limit Registers
• A pair of base and limit registers define the logical
address space
11
Address Binding
• The process of associating program instructions and data
(addresses) to physical memory addresses is called address
binding, or relocation.
Compile time:
• The compiler or assembler translates symbolic addresses (e. g.,
variables) to absolute addresses.
12
Address Binding
Load time:
• The compiler translates symbolic addresses to relative
(relocatable) addresses. The loader translates these to absolute
addresses.
Execution time:
13
Multistep Processing of a User Program
14
From source to executable code
15
Address Binding
Functions of a Linker
• A linker collects (if possible) 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.
16
Address Binding
Functions of a Loader
17
Logical vs. Physical Address Space
• The user program deals with logical addresses; it never sees the real
physical addresses.
19
Dynamic relocation using a relocation register
20
Dynamic relocation using a relocation register
21
Dynamic Loading
22
Dynamic Linking
• Linking postponed until execution time.
23
Overlays
24
Overlays for a Two-Pass Assembler
25
Swapping
26
Swapping
27
Swapping (Contd…)
• “if needed, the operating system can always make room for high-
priority jobs, no matter what!”
28
Schematic View of Swapping
29
Contiguous Allocation
• Main memory usually into two partitions:
• Resident operating system, usually held in low memory with
interrupt vector.
• User processes then held in high memory.
30
Fixed Partitioning
Operating System
8M
8M
8M
8M
8M
Fixed Partitioning
• Unequal-size partitions
• lessens the problem with equal-size partitions
Operating System
8M
2M
4M
6M
8M
8M
12 M
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
One Process Queue per Partition
Operating
System
New
Processes
One Common Process Queue
• When its time to load a process into main memory the smallest available
partition that will hold the process is selected
Operating
System
New
Processes
Dynamic Partitioning
Process 2 224 K
896 K
576 K
352 K
Example Dynamic Partitioning
64 K 64 K 64 K
Example Dynamic Partitioning
Operating Operating
System System
64 K 64 K
Dynamic Partitioning Placement Algorithm
• First-fit algorithm
• starts scanning memory from the beginning and chooses
the first available block that is large enough.
• Next-fit
• starts scanning memory from the location of the last
placement and chooses the next available block that is
large enough
• Compaction is required to obtain a large block at the
end of memory
Hardware Support for Relocation and Limit Registers
42
HW address protection with base and limit
registers
43
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 OS OS OS
process 8 process 10
44
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes.
45
Fragmentation
• External Fragmentation – total memory space exists to satisfy a request, but it
is not contiguous.
• Internal Fragmentation – allocated memory may be slightly larger than
requested memory; this size difference is memory internal to a partition, but not
being used.
46
Paging
• Logical address space of a process can be noncontiguous; process is
allocated physical memory whenever the latter is available.
• Divide physical memory into fixed-sized blocks called frames (size
is power of 2, between 512 bytes and 8,192 bytes).
• 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
• Internal fragmentation.
47
Address Translation Scheme
• Address generated by CPU is divided into:
m–n n
p d
48
Paging Hardware
49
Paging Model of Logical and Physical Memory
50
Paging Example
51
Free Frames
53
Implementation of Page Table
54
Associative Memory
55
Paging Hardware With TLB
56
Effective Access Time
• The performance of a TLB depends on its hit ratio
• Hit ratio is the percentage of time that a frame number is found in the
TLB
Example
• If a = 20 ns, m = 100 ns, and r = 80%, then
• EAT = (100 + 20) 0.8 + (2(100) + 20)(1 - 0.8) = 140 ns
• For r = 98%, then EAT = 122 ns
57
Memory Protection
58
Valid (v) or Invalid (i) Bit In A Page Table
59
Shared Pages
• 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.
60
Shared Pages Example
61
Structure of the Page Table
• Hierarchical Paging
62
Hierarchical Page Tables
63
Multilevel Paging
64
Multilevel Paging
• A logical address (on 32-bit machine with 4K page size) is
divided into two parts
• 20-bit page number.
• 12-bit frame offset.
65
Two-Level Page-Table Scheme
66
Address-Translation Scheme
• Address-translation scheme for a two-level 32-bit paging
architecture
67
Three-level Paging Scheme
• Consider a computer with an address space of 264 With a 4k
page and for convenience we make the inner page table fit in
one page (i.e., 210 * 4 bytes), we have
68
Hashed Page Tables
69
Hashed Page Table
70
Inverted Page Table
• Another approach to dealing with the resource requirements of page
tables, is inverted page tables.
71
Inverted Page Table Architecture
72
Paging Examples
8
0
2
Consider logical address 1025 and the following
page table for some process P0. Assume a 15-bit
address space with a page size of 1K. What is
the physical address to which logical address
1025 maps?
0 000010000000001
2
Consider logical address 1025 and the following
page table for some process P0. Assume a 15-bit
address space with a page size of 1K. What is
the physical address to which logical address
1025 maps?
2 00001 0000000001
Consider logical address 1025 and the following
page table for some process P0. What is the
physical address?
81
User’s View of a Program
82
Logical View of Segmentation
4
1
3 2
4
83
Segmentation
• Addresses specify both the segment name and the offset within the
segment.
84
Segmentation Architecture (Cont.)
85
Segmentation Architecture (Cont.)
86
Segmentation Architecture (Cont.)
• Protection
• With each entry in segment table associate:
• validation bit = 0 illegal segment
• read/write/execute privileges
87
Segmentation Hardware
88
Example of Segmentation
89
Sharing of Segments
90