MM PDF
MM PDF
Operating System
Unit - 6
Memory
Managemen
t
Topics to be covered
Basic Memory Management: Definition
Logical and Physical address map
Memory allocation
Paging
Virtual Memory: Basics of Virtual Memory
Hardware and control structures – Locality of reference
Page fault
Working Set
Dirty page/Dirty bit – Demand paging ( Concepts only)
Page Replacement Algorithms
• Faster
Register • Expensive
• Less Capacity
L1 Cache
L2 Cache
Main Memory
User
Progra User
m User Progra
Progra m
m
OS in RAM 0 0 OS in RAM 0
Partition 1
OS
Partition - 2 Partition - 2
Partition - Partition - 1
1
Operating Operating
System System
Process B
Process B
Process A Process A
Operating Operating
System System
Program 2
relocation at program address is wrong 16408
load time here 16404
16400
Program 1 Program 2 16392
16388
JMP 28 16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
Program 1
MOV 24 24 MOV 24
20 20 20
16 16 16
8 8 8
4 4 4
JMP 24 0 JMP 28 0 JMP 24 0
Unit – 6: Memory Management 11
18 LDCE,IT
Static relocation
When program was loaded at address 16384, 0 32764
.
the constant 16384 was added to every
.
program address during the load process. CMP 16412
• Slow 16408
• Required extra information from program 16404
16400
Program 1 Program 2 16392
16388
JMP 16412 16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
MOV 24 24 MOV 24
20 20 20
16 16 16
8 8 8
4 4 4
JMP 24 0 JMP 28 0 JMP 24 0
Unit – 6: Memory Management 19 LDCE,IT
Base and Limit register
• An address space is set of addresses that a 0 32764
.
process can use to address memory. .
• An address space is a range of valid CMP 16412
16408
addresses in memory that are available for a 16404
program or process. 16400
• Two registers: Base and Limit 16392
16388
1. Base register: Start address of a program JMP 16412 16384
in physical memory. 0 16380
.
2. Limit register: Length of the program. .
For every memory access ADD 28
MOV 24
Base is added to the address 20
Result compared to Limit 16
Only OS can modify Base and Limit register. 8
4
JMP 24 0
Base Limit
Registe Registe
r r
Logical Physical Ye
CPU + <= MEMORY
Address s
Address No
H 18 2 P 20 6 P 26 3 H 29 3 X
H 18 2 P 20 6 P 26 3 H 29 3 X
H 18 2 P 20 6 P 26 3 H 29 3 X
Neighbors
Before P terminate After P terminate
A P B P is replaced by H A B
P is replaced by H
A P A
and two H are merged
P is replaced by H
P B B
and two H are merged
P is replaced by H
P
and three H are merged
Unit – 6: Memory Management 21
27 LDCE,IT
Memory allocation algorithms
Four memory allocation algorithms are as follow
1. First fit
2. Next fit
3. Best fit
4. Worst fit
• Here process of size 426k will not get any partition for allocation.
• Here process of size 426k will not get any partition for allocation.
• Here process of size 426k will not get any partition for allocation.
Virtual
Address space HDD
Unit – 6: Memory Management 39 LDCE,IT
Virtual Memory
• In a system using virtual memory, the RAM
physical memory is divided into page
RAM
frames and the virtual address space is
divided in into equally-sized partitions Another
HDD
called pages.
Process’
• Virtual memory works fine in a s
Memory
multiprogramming system, with bits and RAM
pieces of many programs in memory at
once.
HDD
RAM
Virtual
Address space HDD
Unit – 6: Memory Management 40 LDCE,IT
Paging
Paging is a storage mechanism used to retrieve processes from
the secondary storage (Hard disk) into the main memory (RAM)
in the form of pages.
The main idea behind the paging is to divide each process in the
form of pages. The main memory will also be divided in the form
of frames.
One page of the process is to be stored in one of the frames of the
memory.
The pages can be stored at the different locations of the memory
but the priority is always to find the contiguous frames or holes.
40K – 44K
36K – 40K Virtual page
Virtual 32K – 36K 5
Address 28K – 32K
Space
24K – 28K
Physical
20K – 24K 3 Page frame 5 20K – 24K
16K – 20K 4 4 16K – 20K Memory
3 12K – 16K Address
12K – 16K 0
8K – 12K 2 8K – 12K
4K – 8K 1 1 4K – 8K
0K – 4K 2 0 0K – 4K
15 000 0
14 000 0
13 000 0
12 000 0
11 111 1
10 000 0
9 101 1 12 bit offset
Page 8 000 0
copied directly from
Tabl 7 000 0
input to output
e 6 000 0
Virtual page=2 5 011 1
is used as an 4 100 1
index into the 3 000 1
2 110 1 110
page table
1 001 1 Present/
0 010 1
Absent bit
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0
Present/ absent
Modified bit: Modified bit says whether the page has been
modified or not. If the page in memory has been modified, it
must be written back to disk. This bit is also called as dirty bit as it
reflects the page’s state.
Referenced bit: A references bit is set whenever a page is
referenced, either for reading or writing. Its value helps
operating system in page replacement algorithm.
Cashing Disabled bit: This feature is important for pages that
maps onto device registers rather than memory. With this bit
cashing can be turned off.
Logical Address
Physical Address
P D F3 D
P2
performance is
reduced by half
CPU
For every instruction
Memory reference Memory
occur two time
Use a hardware
Page Table
TLB (Translation
Lookaside Buffer) Page
Frame
P1 F2
P2 F3
P3 F1
Unit – 6: Memory Management 61 LDCE,IT
63
Mapping from virtual address to physical address must be fast
Logical Address
Physical Address
P D F1
F3 D
P3 P2
TLB
Page
CPU Frame Hardware
P1 F2
Memory
P2 F3
Page
Instruction 1
Instruction 2 Page Table
Instruction 3 Page
… Frame Data Structure
… P1 F2 Inside memory
Instruction 100
P2 F3
P3 F1
Unit – 6: Memory Management 61 LDCE,IT
64
Mapping from virtual address to physical address using TLB
Steps in TLB hit:
1. CPU generates virtual address.
2. It is checked in TLB (present).
3. Corresponding frame number is retrieved, which now tells where in the
main memory page lies.
Page Frame
P1 F301
P2 F302
Page
Requests 7 0 1 2 0 3 0 4 2 3 0 3 2 0 2 0 1 7 0 1
Frame 1 7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1
Frame 2 0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
Frame 3 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7
Page F F F F F F F F F
Faults
(9)
Page
Requests 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Frame 1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Frame 2 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
Frame 3 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Page F F F F F F F F F F F F F F F
Faults
(15)
0 1 0 1 0
Remove B C D E A Add at end
1 0 1 0 1
C D E A F
0 0 0 0 0
A B C D E
0 0 0 0 1
B C D E F
A hand points to
1 1
the oldest page.
E C
0
D
1
A
1 0
F B
1 1
E C
0
D
1
A
1 1
F G
B
0
1 1
E C
0
D
1
A
1 0
F B
1 0
E 1
0 C
D
This process is repeated until
a page is found with R = 0.
Unit – 6: Memory Management 81 LDCE,IT
LRU (Least Recently Used) Page Replacement Algorithm
A good approximation to the optimal algorithm is based on the
observation that pages that have been heavily used in last few
instructions will probably be heavily used again in next few
instructions.
When page fault occurs, throw out the page that has been used
for the longest time. This strategy is called LRU (Least Recently
Used) paging.
To fully implement LRU, it is necessary to maintain a linked list of
all pages in memory, with the most recently used page at the
front and the least recently used page at the rear.
The list must be updated on every memory reference.
Finding a page in the list, deleting it, and then moving it to the
front is a very time consuming operations.
Page Requests 1 2 3 4 1 2 5 1 2 3 4 5
Frame 1 1 1 1 4 4 4 5 5 5 5 5 5
Three Frames
Frame 2 2 2 2 1 1 1 1 1 3 3 3
Frame 3 3 3 3 2 2 2 2 2 4 4
Page Faults (9) F F F F F F F F F
Page Requests 1 2 3 4 1 2 5 1 2 3 4 5
Frame 1 1 1 1 1 1 1 5 5 5 5 4 4
Four Frames
Frame 2 2 2 2 2 2 2 1 1 1 1 5
Frame 3 3 3 3 3 3 3 2 2 2 2
Frame 4 4 4 4 4 4 4 3 3 3
Page Faults (10) F F F F F F F F F F
LRU:- When page fault occurs, throw out the page that has been
used for the longest time.
Page page-1 is not used for the long time from all four, so LRU
suggest replacing page-1.