Chapter 03
Chapter 03
Third Edition
ANDREW S. TANENBAUM
Chapter 3
Memory Management
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory Management
Memory (RAM) is an important and rare resource
Programs expand to fill the memory available to them
Programmer’s view
Memory should be private, infinitely large, infinitely fast,
nonvolatile…
Reality
Best of people’s mind: memory hierarchy
Register, cache, memory, disk, tape
Memory manager
Efficiently manage memory
Keep track the free memory, allocate memory to
programs…
Memory management
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Multiple problems without
abstraction
IBM 360
Memory divided into 2-KB blocks and each one
with a 4-bit protection key
PSW also has a 4-bit protection key
Hardware will trap any attempt tries to access
memory with a protection code different from
PSW key
Multiple Programs Without Memory
Abstraction: Drawback
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Drawback of no abstraction
Problem is that both programs reference absolute
physical memory
People hope that they can have a private space,
that is, addresses local to it
IBM 360
Modify the second program on the fly as it loaded it into
memory
Static relocation
When a program is loaded into 16384, then the constant is added
to every address
Slow down loading, needs extra information
No abstraction memory still used in embedded and
smart systems
Abstraction: address space
Not to expose physical address to programmers
Crash OS
Hard to parallelize
Two problems to solve:
Protection
Relocation
Address space:
A set of memory processes can use to address
memory
Each process has its own address space, independent
of each other
How?
Dynamic relocation
Figure 3-3. Base and limit registers can be used to give each
process a separate address space.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Base and Limit Registers
Disadvantage:
Need to perform an addition and a comparison on
every memory reference
Swapping
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Swapping
Problems
Addresses different as swaps in and out
Static relocation/dynamic relocation
Memory holes
Memory compaction
Require CPU time
Move 4 byes in 20ns, then 5 sec to compact 1 GB
How much memory allocate for a program
Programs tend to grow
Both data segment (heap) and stack
Swapping (2)
Figure 3-5. (a) Allocating space for growing data segment. (b)
Allocating space for growing stack, growing data segment.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Managing free memory
Figure 3-6. (a) A part of memory with five processes and three
holes. The tick marks show the memory allocation units. The
shaded regions (0 in the bitmap) are free. (b) The
corresponding bitmap. (c) The same information as a list.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory Management with Linked Lists
9KB
For first fit? Best fit? Worst fit? And next fit?
Virtual Memory
Manage bloatware
Where programs are too big to fit into memory
Being split by programs is a bad idea (overlays)
Virtual memory
Every program has its own address space
The address space is divided into chunks called pages
Each page is a contiguous area and mapped to physical
address
But, not all pages are needed to in physical memory
OS maps page addresses and physical addresses on the fly
When a needed page not in memory, OS needs to get it in
Every page needs relocation
Virtual Memory – Paging (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Page Table Structure
Protection
What kinds of access are permitted
Modified:
When a page is written to (dirty)
Referenced:
When a page is referenced
Cache disabling
Data inconsistency
Speeding Up Paging
Paging implementation issues:
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Speeding up paging
To keep the page table in register?
No more memory access needed during process execution
But unbearably expensive
To keep the page table entirely in memory?
• Solution:
– A special fast-lookup hardware cache called
associative registers or translation look-aside
buffers (TLBs)
Translation Lookaside Buffers
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
TLB
Figure 3-13. (a) A 32-bit address with two page table fields.
(b) Two-level page tables.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
?
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Impact of page fault
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
LRU
Page fault: 12
Replacement: 9
LRU implementation
• Hard to implement efficiently:
– Use an extra counter (time-of-use) field in a page
• replace the oldest page
• Requires the hardware with a counter
• On page fault, examines all the counters to find the
lowest one
– Store a stack of page numbers (software)
• replace the bottom page
• The size of the stack is the size of physical frames
• If the page is in the stack, take it out and push;
otherwise, push it in
LRU with stack (graph)
LRU Page Replacement Algorithm (hardware)
Figure 3-17. LRU using a matrix when pages are referenced in the order
0, 1, 2, 3, 2, 1, 0, 3, 2, 3.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
Ex.
Figure 3-18. The aging algorithm simulates LRU in software. Shown are
six pages for five clock ticks. The five clock ticks are represented
by (a) to (e).
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
Differences with LRU
• Advantage of LRU:
– statistical analysis and proving show that it is sub-
optimal
• Situations where MRU is preferred:
– if there are N pages in the LRU pool, an
application executing a loop over array of N + 1
pages will cause a page fault on each and every
access
– Ex: the reference sequence is 1,2…,501, and the
frame is 500
Working Set Page Replacement
• Demand paging:
– Load pages on demand, not in advance
• Locality of reference:
– During execution, process references only a
relatively small fraction of its pages
• Working set:
– Set of pages that a process is currently using
– If the working set is in, won’t cause many page
faults until next phase
Given the reference sequence,
56214( t1)563433434434231(t2 )657345,
then working set w(10,t1) = , and the
w( 10,t2)= .
Working set model
Figure 3-19. The working set is the set of pages used by the k most
recent memory references. The function w(k, t) is the size of the
working set at time t.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
Replacement with WS
Figure 3-21. Operation of the WSClock algorithm. (a) and (b) give an
example of what happens when R = 1.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
WSClock page replacement
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Local versus Global Allocation Policies (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Static library
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Page Fault Handling (3)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Instruction Backup
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Backing Store (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Backing Store (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Separation of Policy and Mechanism (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Separation of Policy and Mechanism (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation (1)
A compiler has many tables that are built up as
compilation proceeds, possibly including:
• The source text being saved for the printed listing (on
batch systems).
• The symbol table – the names and attributes of variables.
• The table containing integer, floating-point constants
used.
• The parse tree, the syntactic analysis of the program.
• The stack used for procedure calls within the compiler.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation with Paging: MULTICS (7)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation with Paging: MULTICS (8)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation with Paging: MULTICS (9)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Segmentation with Paging: The Pentium (2)