5.1 MemManagMultiprog
5.1 MemManagMultiprog
5.1 MemManagMultiprog
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze
Copyright Notice
© 2000-2005 David A. Solomon and Mark Russinovich
2
Roadmap for Section 5.1.
3
Memory Management Principles
4
Address Binding
Source
program
6
Memory-Management Unit (MMU)
7
Dynamic relocation using a
relocation register
relocation
register
7000
memory
CPU logical + physical
address address
642 7642
MMU
8
Dynamic Loading
9
Dynamic Linking
10
Memory Allocation Schemes
Main memory must accommodate OS + user processes
OS needs to be protected from changes by user processes
User processes must be protected from each other
Single partition allocation:
User processes occupy a single memory partition
Protection can be implemented by limit and relocation register
(OS in low memory, user processes in high memory, see below)
limit relocation
register register
11
Memory Allocation Schemes (contd.)
Multiple-Partition Allocation
Multiple processes should reside in memory simultaneously
Memory can be divided in multiple partitions (fixed vs. variable size)
Problem: What is the optimal partition size?
Dynamic storage allocation problem
Multiple partitions with holes in between
Memory requests are satisfied from the set of holes
Which hole to select?
First-fit: allocate the first hole that is big enough
Best-fit: allocate the smallest hole that is big enough
Worst-fit: allocate the largest hole (produces largest leftover hole)
First-fit & best-fit are better than worst-fit (time & storage-wise)
First-fit is generally faster than best-fit
12
Overlays
Concept: Symbol
table
Separate program in modules
Common
Load modules alternatively Pass 1 routines
Overlay driver locates Overlay
modules on disk driver
13
Swapping
In a multiprogramming environment:
Processes can temporarily be
swapped out of memory to backing Main memory
store in order to allow for execution of
other processes Operating
system Backing store
On the basis of physical addresses:
Then, processes will be swapped in
into same memory space that they
occupied previously Swap Process
out P1
On the basis of logical addresses: User
What current OSes call swapping is space
Swap Process
rather paging out whole processes. in P2
Then, processes can be swapped in
at arbitrary physical addresses.
14
Segmentation
What is the programmer‘s view of memory?
Collection of variable-sized segments (text, data, stack, subroutines,..)
No necessary ordering among segments
Logical address: <segment-number, offset>
Hardware:
Segment table containing base address and limit for each segment
Segment
s
table
limit base
Physical
CPU s d memory
yes
< +
no Trap, addressing error
15
Fragmentation
16
Paging
17
Paging: Basic Method
CPU p d f d
Logical Physical
address address Physical
p memory
offset
Page number Page frames
are typically
2-4 kb
Page table
18
Paging Example
frame
number
0
1 Page 1
Page 0 4
0 2
Page 1 1 1
3 Page 3
Page 2 2 6
4 Page 0
Page 3 3 3
5
page
logical Page 2
table 6
memory
7
physical
memory
19
Free Frames
7 Free frame list frame 7
7, 8, 10, 11,13, 16 number
8 8 Page 1
9 9
Process creation
10 10
0 11
11 11 Page 0
1 8
12 12
2 16
13 13 13 Page 3
3
14 14
New process
15 page table 15
16 16 Page 2
Free frame list
17 7, 10 17
physical
After allocation
Before allocation
memory
20
Paging: Hardware Support
Every memory access requires access to page table
Page table should be implemented in hardware
Page tables exist on a per-user process basis
Small page tables can be just a set of registers
Problem: size of physical memory, # of processes
Page tables should be kept in memory
Only base address of page table is kept in a special register
Problem: speed of memory accesses
Translation look-aside buffers (TLBs)
Associative registers store recently used page table entries
TLBs are fast, expensive, small: 8..2048 entries
TLB must be flushed on process context switches
21
Associative Memory
Page # Frame #
22
Paging Hardware With TLB
CPU p d f d
Logical Physical
address TLB hit address
p TLB miss
Page table
23
Effective Access Time with TLB
24
Memory Protection
25
Valid (v) or Invalid (i) Bit in a Page
frame
Table number
0
1 Page 1
Page 0 4 v
0 2
Page 1 1 1 v
3 Page 3
Page 2 2 6 v
4 Page 0
Page 3 3 3 v
4 i 5
logical i Page 2
5 6
memory
page
7
table
physical
Invalid pages may be paged out memory
26
Page Table Structure
Hierarchical Paging
27
Hierarchical Page Tables
28
Two-Level Paging Example
29
Two-Level Page-Table Scheme
page tables
memory
30
Address-Translation Scheme
page
table
31
Hashed Page Tables
32
Hashed Page Table
CPU p d f d
Logical Physical
address address Physical
memory
offset
Page number
hash p f q r
function
Page table
33
Inverted Page Table
34
Inverted Page Table Architecture
CPU pid p d f d
Logical Physical
address address Physical
memory
offset
Page number
Process ID
search pid p
Page table
35
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
36
Shared Pages Example frame
Process 1
number
Process 1 page table
virtual memory 0
cpp 1 cpp
1
cc1 2
4
cc2 3
11
data1 4 cc1
7
5
Process 2 Process 2 page table 6
virtual memory
7 data1
cpp 1 8 data2
cc1 4 9
cc2 11 10
data2 8 cc2
11
memory
37
Segmentation with Paging
- paged segmentation on the GE 645 (Multics)
The innovative MULTICS operating system introduced:
Logical addresses: 18-bit segment no, 16-bit offset
(relatively) small number of 64k segments
To eliminate fragmentation, segments are paged
A separate page table exists for each segment
physical
s d d memory
logical address yes
>= p d‘
no
+ segment page-table
length base Trap
+ f f d‘
segment table segment table
physical address
base register
page table for segment s
38
selector offset Intel logical
selector
Address
Intel 30386
s
limit base
descriptor
table Address Translation
offset
+
31 22 21 12 11 0 Physical Address
The Intel 386
Intel Linear
10 10 12 uses
Address
segmentation
operand with paging
PTE 4 Kb page for memory
4Kb PDE 4kb page management
frame
with a two-
Page table 22 bit operand
1024 entries offset level paging
4Mb PDE 4 Mb page
4MB page frame scheme.
Page directory
1024x4byte entries
(one per process)
cr 3 Physical Memory
Physical address
39
Summary
In a multiprogrammed OS, every memory address
generated by the CPU must be checked for legality and
possibly mapped to a physical address
Checking cannot be implemented (efficiently) in software
Hardware support is essential
A pair of registers is sufficient for single/multiple partition
schemes
Paging/segmentation need mapping tables to define address maps
Paging and segmentation can be fast
Tables have to be implemented in fast registers (Problem: size)
Set of associative registers (TLB) may reduce performance
degradation if tables are kept in memory
Most modern OS combine paging and segmentation
40
Further Reading
41