Professional Documents
Culture Documents
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Chapter 8: Memory Management
Background
Swapping
Contiguous Memory Allocation
Paging
Structure of the Page Table
Segmentation
Example: The Intel Pentium
Operating System Concepts – 8th Edition 8.2 Silberschatz, Galvin and Gagne ©2009
Objectives
Operating System Concepts – 8th Edition 8.3 Silberschatz, Galvin and Gagne ©2009
Background
Program must be brought (from disk) into memory and placed within a
process for it to be run
Main memory and registers are only storage CPU can access directly
Operating System Concepts – 8th Edition 8.4 Silberschatz, Galvin and Gagne ©2009
Memory Hierarchy
Operating System Concepts – 8th Edition 8.5 Silberschatz, Galvin and Gagne ©2009
Purpose of Memory Management
Operating System Concepts – 8th Edition 8.6 Silberschatz, Galvin and Gagne ©2009
Background
We must ensure correct operation has to protect the operating
system from access by user processes and to protect user
processes from one another.
To do this …
Each process has a range of legal addresses.
Process can access only these legal addresses.
Operating System Concepts – 8th Edition 8.7 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
A pair of base and limit registers define the logical address space
30004 + 12090 =
42094!!
Operating System Concepts – 8th Edition 8.8 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
Then, we compare every address generated in user mode with the
registers.
Any attempt (in user mode) to access operating-system memory or other
users’ memory results in a fatal error.
Therefore, we can prevent a user program from modifying the code or
data structures of either the operating system or other users.
Operating System Concepts – 8th Edition 8.9 Silberschatz, Galvin and Gagne ©2009
Memory Management
Operating System Concepts – 8th Edition 8.10 Silberschatz, Galvin and Gagne ©2009
Memory Manag. 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
(relocated)
Memory references in the code must be translated to actual
physical memory address
Operating System Concepts – 8th Edition 8.11 Silberschatz, Galvin and Gagne ©2009
Memory Manag. Requirements
Sharing
Allow several processes to access the same portion of
memory
Better to allow each process access to the same copy of the
program rather than have their own separate copy
Operating System Concepts – 8th Edition 8.12 Silberschatz, Galvin and Gagne ©2009
Fixed Partitioning
Equal-size partitions
Any process whose size is less than or equal to the partition
size can be loaded into an available partition
If all partitions are full, the operating system can swap a
process out of a partition
A program may not fit in a partition. The programmer must
design the program with overlays
Main memory use is inefficient. Any program, no matter
how small, occupies an entire partition. This is called
internal fragmentation.
Operating System Concepts – 8th Edition 8.13 Silberschatz, Galvin and Gagne ©2009
Fixed Partitioning
Operating System Concepts – 8th Edition 8.14 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm
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
Operating System Concepts – 8th Edition 8.15 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm
Operating System Concepts – 8th Edition 8.16 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm
Operating System Concepts – 8th Edition 8.17 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
Operating System Concepts – 8th Edition 8.18 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
Operating System Concepts – 8th Edition 8.19 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
Operating System Concepts – 8th Edition 8.20 Silberschatz, Galvin and Gagne ©2009
Internal / External Fragmentation
Operating System Concepts – 8th Edition 8.21 Silberschatz, Galvin and Gagne ©2009
Reducing External Fragmentation
Operating System Concepts – 8th Edition 8.22 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
Best-fit algorithm
Chooses the block that is closest in size to the request
Worst performer overall
Since smallest block is found for process, the smallest
amount of fragmentation is left
Memory compaction must be done more often
Operating System Concepts – 8th Edition 8.23 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
First-fit algorithm
Scans memory form the beginning and chooses the first
available block that is large enough
Fastest
May have many process loaded in the front end of memory
that must be searched over when trying to find a free block
Worst-fit algorithm
Search the entire list of available memory and allocate the
largest block.
(The justification for this scheme is that the leftover block
produced would be larger and potentially more useful than
that produced by the best-fit approach.)
Operating System Concepts – 8th Edition 8.24 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
Next-Fit algorithm
Operating System Concepts – 8th Edition 8.25 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
Operating System Concepts – 8th Edition 8.26 Silberschatz, Galvin and Gagne ©2009
Question
Assume that the main memory has the following 5 fixed
partitions with the following sizes: 100KB,
500KB, 200KB, 300KB and 600KB (in order)
a) How would each of the First-fit, Best-fit, next fit, Worst-fit
algorithms place processes of 212KB,
417KB, 112KB and 426KB (in order)?
c) Compute the total memory size that is not used for each
algorithm.
d) Which algorithm makes the efficient use of the memory?
Operating System Concepts – 8th Edition 8.27 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
A pair of base and limit registers define the logical address space
Operating System Concepts – 8th Edition 8.28 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
Base register
Starting address for the process
Limit + Base / Bounds register
Ending location of the process
These values are set when the process is loaded or when the process
is swapped in
The value of the base register is added to a relative address to
produce an absolute address
The resulting address is compared with the value in the bounds
register
If the address is not within bounds, an interrupt is generated to the
operating system
Operating System Concepts – 8th Edition 8.29 Silberschatz, Galvin and Gagne ©2009
Logical and Physical addresses
Operating System Concepts – 8th Edition 8.30 Silberschatz, Galvin and Gagne ©2009
Logical and Physical addresses
Operating System Concepts – 8th Edition 8.31 Silberschatz, Galvin and Gagne ©2009
Logical and Physical addresses
Operating System Concepts – 8th Edition 8.32 Silberschatz, Galvin and Gagne ©2009
Memory-Management Unit (MMU)
The user program deals with logical addresses; it never sees the real
physical addresses
Operating System Concepts – 8th Edition 8.33 Silberschatz, Galvin and Gagne ©2009
Dynamic relocation using a
relocation register
Operating System Concepts – 8th Edition 8.34 Silberschatz, Galvin and Gagne ©2009
Hardware Support for Relocation
and Limit Registers
Operating System Concepts – 8th Edition 8.35 Silberschatz, Galvin and Gagne ©2009
Logical and physical addresses
Operating System Concepts – 8th Edition 8.36 Silberschatz, Galvin and Gagne ©2009
Swapping
A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution
Operating System Concepts – 8th Edition 8.37 Silberschatz, Galvin and Gagne ©2009
Schematic View of Swapping
Operating System Concepts – 8th Edition 8.38 Silberschatz, Galvin and Gagne ©2009
Swapping
Examples:
In a round-robin system …
When a quantum expires, the memory manager
will swap out the process that just finished
And swap in another memory for execution.
In a priority-based system …
If a higher-priority process arrives, the memory
manager can swap out a lower-priority process.
Then load and execute the higher-priority process.
This scheme is sometimes called roll out, roll in.
Operating System Concepts – 8th Edition 8.39 Silberschatz, Galvin and Gagne ©2009
Cost of Swapping
Process size = 1 MB
Transfer rate = 5 MB/sec
Swap out time = 1/5 sec
= 200 ms
Average latency = 8 ms
Net swap out time = 208 ms
Swap out + swap in = 416 ms
Operating System Concepts – 8th Edition 8.40 Silberschatz, Galvin and Gagne ©2009
Paging
Physical address space of a process can be noncontiguous
To run a program of size n pages, need to find n free frames and load
program
Operating System Concepts – 8th Edition 8.41 Silberschatz, Galvin and Gagne ©2009
Address Translation Scheme
Operating System Concepts – 8th Edition 8.42 Silberschatz, Galvin and Gagne ©2009
Paging Hardware
Operating System Concepts – 8th Edition 8.43 Silberschatz, Galvin and Gagne ©2009
Paging Model of Logical and
Physical Memory
Operating System Concepts – 8th Edition 8.44 Silberschatz, Galvin and Gagne ©2009
Free Frames
Operating System Concepts – 8th Edition 8.45 Silberschatz, Galvin and Gagne ©2009
Paging Example
Page size = 4 bytes 0
Page Frame
Process address space 1
= 4 pages 0111
11011
Operating System Concepts – 8th Edition 8.47 Silberschatz, Galvin and Gagne ©2009
Paging Example
Operating System Concepts – 8th Edition 8.48 Silberschatz, Galvin and Gagne ©2009
Paging Example
Operating System Concepts – 8th Edition 8.49 Silberschatz, Galvin and Gagne ©2009
Example
Operating System Concepts – 8th Edition 8.51 Silberschatz, Galvin and Gagne ©2009
Another Example
Operating System Concepts – 8th Edition 8.52 Silberschatz, Galvin and Gagne ©2009
Page Size
Smaller page size, less amount of internal
fragmentation
Smaller page size, more pages required per
process
More pages per process means larger page tables
Operating System Concepts – 8th Edition 8.53 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
Operating System Concepts – 8th Edition 8.54 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
Operating System Concepts – 8th Edition 8.55 Silberschatz, Galvin and Gagne ©2009
Paging Hardware With TLB
Operating System Concepts – 8th Edition 8.56 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
Teffective on a hit = TTLB + Tmem
Teffective on a miss = TTLB + 2Tmem
If HR is hit ratio and MR is miss ratio, then
Operating System Concepts – 8th Edition 8.57 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
Tmem = 100 nsec
TTLB = 20 nsec
Hit ratio is 80%
Teffective = ?
Operating System Concepts – 8th Edition 8.58 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
Tmem = 100 nsec
TTLB = 20 nsec
Hit ratio is 98%
Teffective = ?
Operating System Concepts – 8th Edition 8.59 Silberschatz, Galvin and Gagne ©2009
Memory Protection
Operating System Concepts – 8th Edition 8.60 Silberschatz, Galvin and Gagne ©2009
Valid (v) or Invalid (i)
Bit In A Page Table
Operating System Concepts – 8th Edition 8.61 Silberschatz, Galvin and Gagne ©2009
Segmentation
Operating System Concepts – 8th Edition 8.62 Silberschatz, Galvin and Gagne ©2009
User’s View of a Program
Operating System Concepts – 8th Edition 8.63 Silberschatz, Galvin and Gagne ©2009
Logical View of Segmentation
4
1
3 2
4
Operating System Concepts – 8th Edition 8.64 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Operating System Concepts – 8th Edition 8.65 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware
Operating System Concepts – 8th Edition 8.66 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware
Operating System Concepts – 8th Edition 8.67 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
Operating System Concepts – 8th Edition 8.68 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
Logical and Physical Addresses
(2, 399) – PA: 4300+399 = 4699
(4, 0) – PA: 4700+0 = 4700
(4, 1000) trap
(3, 1300) trap
(6, 297) trap
Operating System Concepts – 8th Edition 8.69 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
Operating System Concepts – 8th Edition 8.70 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
Operating System Concepts – 8th Edition 8.71 Silberschatz, Galvin and Gagne ©2009
Paged Segmentation
Divide every segment in a process into fixed size pages
Need for a page table per segment
CPU’s memory management unit must support both
segmentation and paging
s d
index segment
table
p d’
index page table offset within the page
p
Operating System Concepts – 8th Edition 8.72 Silberschatz, Galvin and Gagne ©2009
Examples
Operating System Concepts – 8th Edition 8.73 Silberschatz, Galvin and Gagne ©2009
In system where page size is 1024 bytes,
and the available physical memory is
equal to 218=256KB, the length of the
physical address is equal to ??
Operating System Concepts – 8th Edition 8.74 Silberschatz, Galvin and Gagne ©2009
Paging Example
Operating System Concepts – 8th Edition 8.75 Silberschatz, Galvin and Gagne ©2009
Example
Operating System Concepts – 8th Edition 8.76 Silberschatz, Galvin and Gagne ©2009
If no TLB is used, how long does it take for a
user program to load a byte from main memory
into a CPU register? Assume the desired page is
already in main memory.
Suppose we add a TLB to the system to speed
up logical-to-physical address translation. With a
TLB access time of 10 ns and a TLB hit rate of
0.9, what is the effective memory access time?
Assume we only do a main-memory page table
lookup after a TLB miss, and that the desired
pages are always in main memory.
Operating System Concepts – 8th Edition 8.77 Silberschatz, Galvin and Gagne ©2009
End of Chapter 8
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009