Module 5
Module 5
2
What is Virtual Memory?
3
What is Virtual Memory?
4
Virtual Memory Block Diagram
Frame Number Valid / Invalid
Process 1 Process 3
OS p0 p0
0 p1 p1 p1
1 p0 Swap-out p2 p2
2 p1 p3 p3
3 p2 p4 p4
CPU
4 p4 p5
Process 2
Page Table 5 p2 Swap-in p6
p0
6 p1
p1 Process 4
● There are 4 processes present on HDD 7 p5
● All processes divided into pages p2 p0
8 p0
● Page size = frame size p3 p1
9
● Note that only some (required pages) p2
are fetched into main memory 10
● Pages may not stored in contiguous RAM
Hard Disk
fashion
5
Virtual Memory Block Diagram
Frame Number Valid / Invalid
Process 1 Process 3
OS p0 p0
0 — 0 0 p1 p1 p1
1 0 1 1 p0 Swap-out p2 p2
P2 2 3 1 2 p1 p3 p3
3 — 0 3 p2 p4 p4
CPU
4 p4 p5
Page Table Process 2
of P2 5 p2 Swap-in p6
p0
6 p1
● Let's assume Process P2 is getting p1 Process 4
7 p5
executed p2 p0
8 p0
● Pages P1 and P2 are already present 9
p3 p1
in main memory - observe page table p2
● Page 0 and page 3 of process P2 are 10
not present in RAM so their valid bit RAM
Hard Disk
is set to 0
6
Virtual Memory Block Diagram
Frame Number Valid / Invalid
Process 1 Process 3
OS p0 p0
0 — 0 0 p1 p1 p1
1 0 1 1 p0 Swap-out p2 p2
P2 2 3 1 2 p1 p3 p3
3 — 0 3 p2 p4 p4
CPU
4 p4 p5
Page Table Process 2
of P2 5 p2 Swap-in p6
p0
6 p1
● Let's say CPU requests page 3 of p1 Process 4
7 p5
Process 2 which is not present in mm p2 p0
● CPU checks entry in page table of P2 8 p0
p3 p1
● As there is no entry of page 2 a TRAP 9
signal gets generated p2
10
● Now a user process changes mode to
RAM
kernel mode Hard Disk
7
Virtual Memory Block Diagram
Frame Number Valid / Invalid
Process 1 Process 3
OS p0 p0
0 — 0 0 p1 p1 p1
1 0 1 1 p0 Swap-out p2 p2
P2 2 3 1 2 p1 p3 p3
3 9 1 3 p2 p4 p4
CPU
4 p4 p5
Page Table Process 2
of P2 5 p2 Swap-in p6
p0
6 p1
● In kernel mode, OS brings page 3 of p1 Process 4
7 p5
process 2 into RAM let say at free p2 p0
frame number 9 8 p0
p3 p1
● Corresponding page table entry also 9 P3
gets updated p2
10
● Now process changes its mode back to
RAM
user mode Hard Disk
● CPU access page 3 by referring page 8
table entry
Virtual Memory - Swap Area
9
Virtual Memory - Page fault
10
Virtual Memory - Page Replacement Algorithms
11
Virtual Memory - Page Replacement Algorithms
1. First In First Out
Reference
7 0 1 2 0 3 0 4 2 3 0 3 1 2 0
String
F1 1 1 1 1 0 0 0 3 3 3 3 2 2
CPU
F2 0 0 0 0 3 3 3 2 2 2 2 1 1 1
Frames
F3 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0
1. If there is a free frame and Page fault -> store new page with no
replacement
2. If no free frame and If page fault -> Replace new page with page arrived
12
first
Virtual Memory - Page Replacement Algorithms
1. First In First Out
Reference
7 0 1 2 0 3 0 4 2 3 0 3 1 2 0
String
F1 1 1 1 1 0 0 0 3 3 3 3 2 2
CPU
F2 0 0 0 0 3 3 3 2 2 2 2 1 1 1
Frames
F3 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0
F1 3 3 3 2 2 2 2 2 4 4
CPU
F2 2 2 2 1 1 1 1 1 3 3 3
Frames
F3 1 1 1 4 4 4 5 5 5 5 5 5
F1 4 4 4 4 4 4 3 3 3
CPU
F2 3 3 3 3 3 3 2 2 2 2
Frames
F3 2 2 2 2 2 2 1 1 1 1 5
F4 1 1 1 1 1 1 5 5 5 5 4 4
Page Hit Ratio =
2/12 F F F F H H F+R F+R F+R F+R F+R F+R
16
Virtual Memory - Page Replacement Algorithms
2. Optimal Page Replacement - Replace page which is not
used in longest dimension of time in future
Reference
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2
String
F1 2 2 2 2 2 2 2 2 2 2 2 2
CPU F2 1 1 1 1 1 4 4 4 4 4 4 1 1
Frames
F3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
F4 7 7 7 7 7 3 3 3 3 3 3 3 3 3 3
F1 2 2 2 2 2 2 2 2 2 2 2 2
CPU F2 1 1 1 1 1 4 4 4 4 4 4 1 1
Frames
F3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
F4 7 7 7 7 7 3 3 3 3 3 3 3 3 3 3
File System
20
Disk Structure
21
File Allocation Methods
Mapping
24
File Allocation Methods
25
File Allocation Methods
1. Linked list allocation:-
File A
File Start Length
A 0 3
File B
0 1 2 3 4 B 3 5
C 2 4
5 6 7 8 9
0 7 7 16 16 -1
10 11 12 13 14 File C
15 16 17 18 19 2 5 5 10 10 15 15 -1
3 13 13 6 6 18 18 11 11 -1
Disk with Sectors 26
File Allocation Methods
27
File Allocation Methods
2. Indexed allocation:-
File A
File Start
Sector 0
A 0 A0 6
File B
A1 9
0 1 2 3 4 B 3 A2 10
5 6 7 8 9 C 12
29
Free Space Management
1 0 1 1 0 1 1 1 1 0 0 1 Occupied
Free
30
Free Space Management
2. Linked List:-
● In this approach, the free disk blocks are linked together
i.e. a free block contains a pointer to the next free
block.
● The block number of the very first disk block is stored at
a separate location on disk and is also cached in memory.
5 6 7 8 9
10 11 12 13 14
31
Free Space Management
2. Grouping:-
● This approach stores the address of the free blocks in the first
free block.
● The first free block stores the address of some, say n free blocks.
Out of these n blocks, the first n-1 blocks are actually free and
the last block contains the address of next free n blocks.
● An advantage of this approach is that the addresses of a group of
free disk blocks can be found easily.
32
File Directory Structure
● Directory can be defined as the listing of the related
files on the disk. The directory may store some or the
entire file attributes.
● To get the benefit of different file systems on the
different operating systems, A hard disk can be divided
into the number of partitions of different sizes.
● The partitions are also called volumes or mini disks.
33
File Directory Structure
34
Directory Implementation
35
Directory Implementation
● Linear List:-
○ all the files in a directory are maintained as singly linked
list.
○ Each file contains the pointers to the data blocks which are
assigned to it and the next file in the directory.
○ When a new file is created, then the entire list is checked
whether the new file name is matching to a existing file name
or not.
○ In case, it doesn't exist, the file can be created at the
beginning or at the end.
○ Therefore, searching for a unique name is a big concern
because traversing the whole list takes time.
36
Directory Implementation
● Linear List:-
○ Therefore, searching for a unique name is a big concern
because traversing the whole list takes time.
○ The list needs to be traversed in case of every operation
(creation, deletion, updating, etc) on the files therefore
the systems become inefficient.
37
Directory Implementation
● Hash Table:-
○ To overcome the drawbacks of singly linked list
implementation of directories, there is an alternative
approach that is hash table.
○ This approach suggests to use hash table along with the
linked lists.
39
Completed Fifth Module!
40