ch8-1 OS
ch8-1 OS
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
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.5 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
A pair of base and limit registers define the logical address space
30004
30004++12090
12090=
=
42094!!
42094!!
Operating System Concepts – 8th Edition 8.6 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.7 Silberschatz, Galvin and Gagne ©2009
Memory Management
Operating System Concepts – 8th Edition 8.8 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.9 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.10 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.11 Silberschatz, Galvin and Gagne ©2009
Fixed Partitioning
Operating System Concepts – 8th Edition 8.12 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.13 Silberschatz, Galvin and Gagne ©2009
Problems with equal size fixed partitions:
If program is bigger than a partition size, use of overlays
Main memory utilization is extremely inefficient; Internal Fragmentation – waste
of space internal to partition due to the fact that block of data loaded is smaller
than partition
Operating System Concepts – 8th Edition 8.14 Silberschatz, Galvin and Gagne ©2009
Placement Algorithm
Operating System Concepts – 8th Edition 8.15 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
Operating System Concepts – 8th Edition 8.16 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
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
Internal / External Fragmentation
Operating System Concepts – 8th Edition 8.19 Silberschatz, Galvin and Gagne ©2009
Reducing External Fragmentation
Operating System Concepts – 8th Edition 8.20 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.21 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.22 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
Next-Fit algorithm
Operating System Concepts – 8th Edition 8.23 Silberschatz, Galvin and Gagne ©2009
Placement Algorithms
Operating System Concepts – 8th Edition 8.24 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.25 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.26 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.27 Silberschatz, Galvin and Gagne ©2009
Logical vs. Physical Address Space
Operating System Concepts – 8th Edition 8.28 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.29 Silberschatz, Galvin and Gagne ©2009
Dynamic relocation using a
relocation register
Operating System Concepts – 8th Edition 8.30 Silberschatz, Galvin and Gagne ©2009
Hardware Support for Relocation
and Limit Registers
Operating System Concepts – 8th Edition 8.31 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.32 Silberschatz, Galvin and Gagne ©2009
Schematic View of Swapping
Operating System Concepts – 8th Edition 8.33 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.34 Silberschatz, Galvin and Gagne ©2009
Swapping
The swapping time:
Assume that:
The user process is 10 MB.
The backing store is a standard hard disk with a transfer rate of 40MB/sec.
No head seeks.
Average latency is 8 ms.
For efficiency, we want the execution time for each process to be long
relative to the swap time.
In this example, the time quantum should be larger than 0.516 seconds.
Operating System Concepts – 8th Edition 8.35 Silberschatz, Galvin and Gagne ©2009
Paging
Divide physical memory into fixed-sized blocks, called frames
Divide logical memory into blocks of the same size, called pages
Size of a page is a power of 2
Typical page sizes: 1K – 16K
To run a program of size n pages, need to find n free frames and load
program
Internal fragmentation
Operating System Concepts – 8th Edition 8.36 Silberschatz, Galvin and Gagne ©2009
Address Translation Scheme
Operating System Concepts – 8th Edition 8.37 Silberschatz, Galvin and Gagne ©2009
Paging Hardware
Operating System Concepts – 8th Edition 8.38 Silberschatz, Galvin and Gagne ©2009
Paging Model of Logical and
Physical Memory
Operating System Concepts – 8th Edition 8.39 Silberschatz, Galvin and Gagne ©2009
Free Frames
Operating System Concepts – 8th Edition 8.40 Silberschatz, Galvin and Gagne ©2009
Paging Example
Page size = 4 bytes 0
Page Frame
Process address space
1
= 4 pages 0111
Physical address space 2
= 8 frames
3
Logical address: (1,3)
= 0111
Physical address: (6,3)
= 11011
11011
Operating System Concepts – 8th Edition 8.42 Silberschatz, Galvin and Gagne ©2009
Paging Example
1502 >>
Operating System Concepts – 8th Edition 8.43 Silberschatz, Galvin and Gagne ©2009
Paging Example
Operating System Concepts – 8th Edition 8.44 Silberschatz, Galvin and Gagne ©2009
Example
32-bit linear address
Process address space = 232 B
= 4 GB
4K page size
Maximum pages in a process address space =
232 / 4K = 1M
Number of bits for d = log2 4K
= 12
Number of bits for p = 32 - 12
Operating System Concepts – 8th Edition 8.45 Silberschatz, Galvin and Gagne ©2009
Another Example
Logical address = 32-bit
Process address space = 232 B
= 4 GB
Main memory = RAM = 512 MB = 229
Page size = 4K
Maximum pages in a process address
space = 232 / 4K = 1M
Operating System Concepts – 8th Edition 8.46 Silberschatz, Galvin and Gagne ©2009
Another Example
Operating System Concepts – 8th Edition 8.47 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.48 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
Each page reference can cause two physical
memory accesses
One to fetch the page table (PTBR) Page table
base register
One to fetch the data
To overcome this problem a high-speed cache is set
up for page table entries
Called a Translation Lookaside Buffer (TLB)
Contains page table entries that have been most
recently used
Operating System Concepts – 8th Edition 8.49 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer (TLB)
Given a logical address, processor examines the
TLB
If page table entry is present (TLB hit), the frame
number is retrieved and the real address is formed
If page table entry is not found in the TLB (TLB
miss), the page number is used to index the process
page table
Operating System Concepts – 8th Edition 8.50 Silberschatz, Galvin and Gagne ©2009
Paging Hardware With TLB
Operating System Concepts – 8th Edition 8.51 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.52 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.53 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.54 Silberschatz, Galvin and Gagne ©2009
Memory Protection
Operating System Concepts – 8th Edition 8.55 Silberschatz, Galvin and Gagne ©2009
Valid (v) or Invalid (i)
Bit In A Page Table
Operating System Concepts – 8th Edition 8.56 Silberschatz, Galvin and Gagne ©2009
Segmentation
Operating System Concepts – 8th Edition 8.57 Silberschatz, Galvin and Gagne ©2009
User’s View of a Program
Operating System Concepts – 8th Edition 8.58 Silberschatz, Galvin and Gagne ©2009
Logical View of Segmentation
4
1
3 2
4
Operating System Concepts – 8th Edition 8.59 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Operating System Concepts – 8th Edition 8.60 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware
Operating System Concepts – 8th Edition 8.61 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware
Operating System Concepts – 8th Edition 8.62 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
Operating System Concepts – 8th Edition 8.63 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.64 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation
Operating System Concepts – 8th Edition 8.65 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
Dynamic Storage Allocation
First fit
Best fit
Worst fit
External fragmentation
Operating System Concepts – 8th Edition 8.66 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.67 Silberschatz, Galvin and Gagne ©2009
Examples
Operating System Concepts – 8th Edition 8.68 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.69 Silberschatz, Galvin and Gagne ©2009
Paging Example
Operating System Concepts – 8th Edition 8.70 Silberschatz, Galvin and Gagne ©2009
End of Chapter 8
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009