Chapter 03 OS
Chapter 03 OS
Memory Management
4.1 Basic memory management
4.2 Swapping
4.3 Virtual memory
4.4 Design issues for paging systems
4.5 Implementation issues
4.6 Segmentation
1
Memory Management
2
Memory Management
• Ideally programmers want memory that is
– large
– fast
– non volatile
• Memory hierarchy
– small amount of fast, expensive memory – cache
– some medium-speed, medium price main memory
– gigabytes of slow, cheap disk storage
• Memory manager handles the memory hierarchy
3
Basic Memory Management
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
4
Basic Memory Management
Monoprogramming without Swapping or Paging
6
Basic Memory Management
Dynamic relocation using a relocation register
7
Basic Memory Management
Relocation and Protection
• Cannot be sure where program will be loaded in
memory
– address locations of variables, code routines cannot be
absolute
– must keep a program out of other processes’ partitions
• Use base and limit values
– address locations added to base value to map to physical
addr
– address locations larger than limit value is an error
8
Basic Memory Management
Relocation and Protection
• Relocation registers used to protect user processes
from each other, and from changing operating-
system code and data
– Base register contains value of smallest
physical address
– Limit register contains range of logical
addresses – each logical address must be less
than the limit register
– MMU maps logical address dynamically
9
Basic Memory Management
HW address protection with base and limit registers
10
Swapping (1)
Schematic View of Swapping
11
Swapping (2)
12
Swapping (3)
13
Swapping (4)
Multiple-partition allocation
• 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)
– There are two ways to keep track of memory usages
• Memory Management with Bit Maps
• Memory Management with Linked Lists
14
Swapping (4)
Multiple-partition allocation
15
Swapping (5)
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
• Next fit: Start seaching the list from the place where it
left off last time
• 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
16
1. Xem PC của chúng ta: (CPU) Cache, Ram,
HDD: capacity, price.
2. List 4 Operating systems in PC environments
– Market shares? Which one having the maximum
market share value?
– Reasons to make that one become the best one?
3. List 4 Operating systems in Smart Phone
environments
– The same questions as above ones.
17
Virtual Memory
Paging
18
Virtual Memory
• Virtual memory – separation of user logical memory from
physical memory.
– Only part of the program needs to be in memory for
execution
– Logical address space can therefore be much larger than
physical address space
– Allows address spaces to be shared by several processes
– Allows for more efficient process creation
• Virtual memory can be implemented via:
– Demand paging
– Demand segmentation
19
Virtual Memory
Paging
20
Virtual Memory
Paging
22
Virtual Memory
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
23
Virtual Memory
Paging Hardware
24
Virtual Memory
Paging: Example
25
Virtual Memory
Page Tables: Example
28
Virtual Memory
Implementation of Page Table
29
Virtual Memory
Paging Hardware With TLB
30
Virtual Memory
TLBs – Translation Lookaside Buffers
32
Virtual Memory
Steps in Handling a Page Fault
33
Virtual Memory
Page Replacement Algorithms
• What happens if there is no free frame?
• Page replacement – find some page in
memory, but not really in use, swap it out
– algorithm
– performance – want an algorithm which will
result in minimum number of page faults
• Same page may be brought into memory
several times
34
Virtual Memory
Basic Page Replacement
1. Find the location of the desired page on disk
2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page
replacement algorithm to select a victim frame
3. Bring the desired page into the (newly) free
frame; update the page and frame tables
4. Restart the process
35
Virtual Memory
Page Replacement
36
Virtual Memory
Page Replacement Algorithms
37
• Example: the process has 5 pages, and is allocated in 3 frames
38
39
• Example: the process has 5 pages, and is allocated
in 3 frames
40
41
42
43
44
Virtual Memory
Design issues: Local versus Global Allocation Policies
47
Virtual Memory
Design issues: Separate Instruction and Data Spaces
50
Virtual Memory
Implementation Issues
Page Fault Handling (1)
51
Virtual Memory
Implementation Issues
Page Fault Handling (2)
6. OS brings schedules new page in from disk
7. Page tables updated
8. Faulting instruction backed up to when it began
9. Faulting process scheduled
10. Registers restored, Program continues
52
Virtual Memory
Implementation Issues
Separation of Policy and Mechanism
54
Virtual Memory
Segmentation (1)
58
Segmentation translation
• Virtual address: <segment-number, offset>
• Segment table maps segment-number to segment
information
– Base: starting address of segment in physical memory
– Limit: length of segment
– Addition metadata inlcudes protection bits
• Limit & protection checked on each access
• 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.
59
Virtual Memory
Segmentation (3)
62
Virtual Memory
Segmentation with Paging: Pentium (2)
A Pentium selector
GDT (Global Descriptor Table), LDT (Local Descriptor Table)
63
Virtual Memory
Segmentation with Paging: Pentium (3)
64
Virtual Memory
Segmentation with Paging: Pentium (4)
65
Virtual Memory
Segmentation with Paging: Pentium (5)
66
Virtual Memory
Segmentation with Paging: Pentium (6)
67
Virtual Memory
Segmentation with Paging: Pentium (7)
69
Pentium address translation
1 1 1
0 0 2
Can cover up to 4
MB
physical address
70
space
Virtual Memory
Segmentation with Paging: Pentium (9)
Level