Professional Documents
Culture Documents
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Outline
Background
Swapping
Contiguous Memory Allocation
Paging
Structure of the Page Table
Segmentation
Examples on different topics
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
Memory
1. Primary Memory
2. Secondary Memory
Operating System Concepts – 8th Edition 8.4 Silberschatz, Galvin and Gagne ©2009
Random Access Memory(RAM) ,is a volatile memory that
loses its contents when the computer or hardware device
losses power.
Read Only Memory(ROM), is a non-volatile memory,
sometimes abbreviated as NVRAM, is a memory that keeps
its contents even if the power is lost.
Computer uses special ROM called BIOS(Basic Input Output
System) which stores the software needed to access
computer hardware such as hard disk and then load an
operating system into RAM and start to execute it.
Operating System Concepts – 8th Edition 8.5 Silberschatz, Galvin and Gagne ©2009
PROM: In which you store a program. But once the PROM has
been used , you cannot wipe it clean and use it to store
something else. E.g. CD-R
Operating System Concepts – 8th Edition 8.6 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
Memory unit only sees a stream of addresses + read requests, or address
+ data and write requests
Register access in one CPU clock (or less)
Main memory can take many cycles
Cache sits between main memory and CPU registers
Protection of memory required to ensure correct operation
Operating System Concepts – 8th Edition 8.7 Silberschatz, Galvin and Gagne ©2009
Need for Multi programming
However, The CPU can directly access the main memory, Registers
and cache of the system.
The program always executes in main memory.
The size of main memory affects degree of Multi programming to
most of the extant.
If the size of the main memory is larger than CPU can load more
processes in the main memory at the same time and therefore will
increase degree of Multi programming as well as CPU utilization.
Operating System Concepts – 8th Edition 8.8 Silberschatz, Galvin and Gagne ©2009
Memory Allocation Preliminaries:
Data structure used by Compiler and Operating System can be classified into the following
categories.
1. One-dimensional Array
2. Linked List
3. Stack
4. Queue
1. Tree
2. Graph
Operating System Concepts – 8th Edition 8.9 Silberschatz, Galvin and Gagne ©2009
Swapping
Swapping is the method of preventing an active process from
executing for a temporary time period in which another
process takes the address space that was allocated to the
former process.
Operating System Concepts – 8th Edition 8.10 Silberschatz, Galvin and Gagne ©2009
Contiguous Memory Allocation-
Operating System Concepts – 8th Edition 8.11 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 8.12 Silberschatz, Galvin and Gagne ©2009
Static Partitioning
Operating System Concepts – 8th Edition 8.13 Silberschatz, Galvin and Gagne ©2009
Under fixed size partitioning scheme, a memory of size 10
KB may be divided into fixed size partitions as-
Operating System Concepts – 8th Edition 8.14 Silberschatz, Galvin and Gagne ©2009
Algorithms for Partition Allocation
Operating System Concepts – 8th Edition 8.15 Silberschatz, Galvin and Gagne ©2009
Algorithms for Partition Allocation
Operating System Concepts – 8th Edition 8.16 Silberschatz, Galvin and Gagne ©2009
Algorithms for Partition Allocation
Operating System Concepts – 8th Edition 8.17 Silberschatz, Galvin and Gagne ©2009
Important Points
Point-01:
For static partitioning,
Best Fit Algorithm works best.
This is because space left after the allocation inside the partition is of
very small size.
Thus, internal fragmentation is least.
Point-02:
For static partitioning,
Worst Fit Algorithm works worst.
This is because space left after the allocation inside the partition is of
very large size.
Thus, internal fragmentation is maximum.
Operating System Concepts – 8th Edition 8.18 Silberschatz, Galvin and Gagne ©2009
Internal Fragmentation
It occurs when the space is left inside the partition after allocating the
partition to a process.
This space is called as internally fragmented space.
This space can not be allocated to any other process.
This is because only static partitioning allows to store only one process in
each partition.
Internal Fragmentation occurs only in static partitioning.
Operating System Concepts – 8th Edition 8.19 Silberschatz, Galvin and Gagne ©2009
External Fragmentation
Operating System Concepts – 8th Edition 8.20 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical
Address
Operating System Concepts – 8th Edition 8.21 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical
Address
Step-01:
The translation scheme uses two registers that are under the
control of operating system.
During context switching, the values corresponding to the process
being loaded are set in the registers.
These two registers are-
Relocation Register
Limit Register
Operating System Concepts – 8th Edition 8.22 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical
Address
Operating System Concepts – 8th Edition 8.23 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical
Address
Step-03:
The logical address generated by the CPU is compared with the limit of the
process.
Now, two cases are possible-
Case-01: Generated Address >= Limit
If address is found to be greater than or equal to the limit, a trap is generated.
This helps to prevent unauthorized access.
Case-02: Generated Address < Limit
The address must always lie in the range [0, limit-1].
If address is found to be smaller than the limit, then the request is treated as a
valid request.
Then, generated address is added with the base address of the process.
The result obtained after addition is the address of the memory location storing
the required word.
Operating System Concepts – 8th Edition 8.24 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical
Address
Operating System Concepts – 8th Edition 8.25 Silberschatz, Galvin and Gagne ©2009
Advantages
Operating System Concepts – 8th Edition 8.26 Silberschatz, Galvin and Gagne ©2009
Disadvantages
Operating System Concepts – 8th Edition 8.27 Silberschatz, Galvin and Gagne ©2009
Dynamic Partitioning
Operating System Concepts – 8th Edition 8.28 Silberschatz, Galvin and Gagne ©2009
The processes arrive and leave the main memory.
As a result, holes of different size are created in the main memory.
These holes are allocated to the processes that arrive in future.
Partition allocation algorithms are used to decide which hole should be
allocated to the arrived process.
Operating System Concepts – 8th Edition 8.29 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 8.30 Silberschatz, Galvin and Gagne ©2009
Important Points-
Point-01:
For dynamic partitioning,
Worst Fit Algorithm works best.
This is because space left after allocation inside the partition is of large
size.
There is a high probability that this space might suit the requirement of
arriving processes.
Operating System Concepts – 8th Edition 8.31 Silberschatz, Galvin and Gagne ©2009
Important Points-
Point-02:
For dynamic partitioning,Best Fit Algorithm works worst.
This is because space left after allocation inside the partition is of very
small size.
There is a low probability that this space might suit the requirement of
arriving processes.
Operating System Concepts – 8th Edition 8.32 Silberschatz, Galvin and Gagne ©2009
Address
Operating System Concepts – 8th Edition 8.33 Silberschatz, Galvin and Gagne ©2009
Advantages
Operating System Concepts – 8th Edition 8.34 Silberschatz, Galvin and Gagne ©2009
Disadvantages
NOTE: Memory management unit (MMU)is a hardware device which is responsible for
the translation of virtual addresses used by software to physical addresses
used in the memory system.It handles all memory and caching operations
associated with the CPU.
Operating System Concepts – 8th Edition 8.35 Silberschatz, Galvin and Gagne ©2009
Problems
Problems on Contiguous Memory Allocation
Operating System Concepts – 8th Edition 8.36 Silberschatz, Galvin and Gagne ©2009
Non-Contiguous Memory Allocation
Operating System Concepts – 8th Edition 8.37 Silberschatz, Galvin and Gagne ©2009
Paging
Paging is a fixed size partitioning scheme.
In paging, secondary memory and main memory are divided into equal fixed size partitions.
The partitions of secondary memory are called as pages.
The partitions of main memory are called as frames.
Each process is divided into parts where size of each part is same as page size.
The size of the last part may be less than the page size.
The pages of process are stored in the frames of main memory depending upon their availability.
Operating System Concepts – 8th Edition 8.38 Silberschatz, Galvin and Gagne ©2009
Example
Consider a process is divided into 4 pages P0, P1, P2 and P3.
Depending upon the availability, these pages may be stored in the main
memory frames in a non-contiguous fashion as shown-
Operating System Concepts – 8th Edition 8.39 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Page Number specifies the specific page of the process from which CPU wants to read the
data.
Page Offset specifies the specific word on the page that CPU wants to read.
Operating System Concepts – 8th Edition 8.40 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Step-02:
For the page number generated by the CPU,
Page Table provides the corresponding frame number (base address of the frame) where that
page is stored in the main memory.
Step-03:
The frame number combined with the page offset forms the required physical address.
Frame number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
Operating System Concepts – 8th Edition 8.41 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Page table is stored in main memory at the time of process creation and its base address is stored
in process control block.
Operating System Concepts – 8th Edition 8.42 Silberschatz, Galvin and Gagne ©2009
Paging
Advantages of Paging-
It allows to store parts of a single process in a non-contiguous fashion.
It solves the problem of external fragmentation.
Disadvantages of Paging-
It suffers from internal fragmentation.
There is an overhead of maintaining a page table for each process.
The time taken to fetch the instruction increases since two memory accesses
are required (Page table and Frame).
Operating System Concepts – 8th Edition 8.43 Silberschatz, Galvin and Gagne ©2009
Page Table
Page table is a data structure.
It maps the page number referenced by the CPU to the frame number where that
page is stored.
Characteristics-
Page table is stored in the main memory.
Number of entries in a page table = Number of pages in which the process is
divided.
Page Table Base Register (PTBR) contains the base address of page table.
Each process has its own independent page table.
Operating System Concepts – 8th Edition 8.44 Silberschatz, Galvin and Gagne ©2009
Page Table Working
Page Table Base Register (PTBR) provides the base address of the page table.
The base address of the page table is added with the page number referenced by the CPU.
It gives the entry of the page table containing the frame number where the referenced page
is stored.
Operating System Concepts – 8th Edition 8.45 Silberschatz, Galvin and Gagne ©2009
Page table Entry
A page table entry contains several information about the page.
The information contained in the page table entry varies from operating system to operating
system.
The most important information in a page table entry is frame number
In general, each entry of a page table contains the following information-
Frame Number-
Frame number specifies the frame where the page is stored in the main memory.
The number of bits in frame number depends on the number of frames in the main memory.
Operating System Concepts – 8th Edition 8.46 Silberschatz, Galvin and Gagne ©2009
Page table Entry
Present / Absent Bit
This bit is also sometimes called as valid / invalid bit.
This bit specifies whether that page is present in the main memory or not.
If the page is not present in the main memory, then this bit is set to 0 otherwise set to
1.
NOTE
If the required page is not present in the main memory, then it is called as Page Fault.
A page fault requires page initialization.
The required page has to be initialized (fetched) from the secondary memory and brought
into the main memory.
Protection Bit-
This bit is also sometimes called as “Read / Write bit“.
This bit is concerned with the page protection.
It specifies the permission to perform read and write operation on the page.
If only read operation is allowed to be performed and no writing is allowed, then this bit
is set to 0.
If both read and write operation are allowed to be performed, then this bit is set to 1.
Operating System Concepts – 8th Edition 8.47 Silberschatz, Galvin and Gagne ©2009
Page table Entry
Reference Bit-
Reference bit specifies whether that page has been referenced in the last
clock cycle or not.
If the page has been referenced recently, then this bit is set to 1
otherwise set to 0.
NOTE
Reference bit is useful for page replacement policy.
A page that has not been referenced recently is considered a good candidate
for page replacement in LRU page replacement policy.
Caching Enabled / Disabled-
This bit enables or disables the caching of page.
Whenever freshness in the data is required, then caching is disabled using
this bit.
If caching of the page is disabled, then this bit is set to 1 otherwise set
to 0.
Operating System Concepts – 8th Edition 8.48 Silberschatz, Galvin and Gagne ©2009
Page table Entry
Dirty Bit
This bit is also sometimes called as “Modified bit“.
This bit specifies whether that page has been modified or not.
If the page has been modified, then this bit is set to 1 otherwise set to 0.
NOTE
In case the page is modified,before replacing the modified page with some
other page, it has to be written back in the secondary memory to avoid
losing the data.
Dirty bit helps to avoid unnecessary writes.
This is because if the page is not modified, then it can be directly
replaced by another page without any need of writing it back to the disk.
Operating System Concepts – 8th Edition 8.49 Silberschatz, Galvin and Gagne ©2009
Important Formulas
For Main Memory-
Physical Address Space = Size of main memory
Size of main memory = Total number of frames x Page size
Frame size = Page size
If number of frames in main memory = 2X, then number of bits in frame number = X bits
If Page size = 2X Bytes, then number of bits in page offset = X bits
If size of main memory = 2X Bytes, then number of bits in physical address = X bits
For Process-
Virtual Address Space = Size of process
Number of pages the process is divided = Process size / Page size
If process size = 2X bytes, then number of bits in virtual address space = X bits
For Page Table-
Size of page table = Number of entries in page table x Page table entry size
Number of entries in page table = Number of pages the process is divided
Page table entry size = Number of bits in frame number + Number of bits used for optional
fields if any
Operating System Concepts – 8th Edition 8.50 Silberschatz, Galvin and Gagne ©2009
Important Formulas
NOTE-
In general, if the given address consists of „n‟ bits, then using „n‟
bits, 2n locations are possible.
Then, size of memory = 2n x Size of one location.
If the memory is byte-addressable, then size of one location = 1 byte.
Thus, size of memory = 2n bytes.
If the memory is word-addressable where 1 word = m bytes, then size of one
location = m bytes.
Thus, size of memory = 2n x m bytes.
Operating System Concepts – 8th Edition 8.51 Silberschatz, Galvin and Gagne ©2009
Problem-01
Calculate the size of memory if its address consists of 22 bits and the
memory is 2-byte addressable.
Solution-
We have-
Number of locations possible with 22 bits = 222 locations
It is given that the size of one location = 2 bytes
Operating System Concepts – 8th Edition 8.52 Silberschatz, Galvin and Gagne ©2009
Problem 2
Calculate the number of bits required in the address for memory having size
of 16 GB. Assume the memory is 4-byte adder
Solution-
Let „n‟ number of bits are required. Then, Size of memory = 2n x 4 bytes.
Since, the given memory has size of 16 GB, so we have-
2n x 4 bytes = 16 GB
2n x 4 = 16 G
2n x 22 = 234
2n = 232
∴ n = 32 bits
Operating System Concepts – 8th Edition 8.53 Silberschatz, Galvin and Gagne ©2009
Problem 3
Consider a machine with 64 MB physical memory and a 32 bit virtual address space. If the page size is
4 KB, what is the approximate size of the page table?
Solution-
Given-
Size of main memory = 64 MB
Number of bits in virtual address space = 32 bits
Page size = 4 KB
We will consider that the memory is byte addressable.
Number of Bits in Physical Address-
Size of main memory
= 64 MB
= 226 B
Thus, Number of bits in physical address = 26 bits
Operating System Concepts – 8th Edition 8.54 Silberschatz, Galvin and Gagne ©2009
Problem 3
Number of Frames in Main Memory-
Number of frames in main memory
= Size of main memory / Frame size
= 64 MB / 4 KB
= 226 B / 212 B
= 214
Thus, Number of bits in frame number = 14 bits
Number of Bits in Page Offset-
We have,
Page size
= 4 KB
= 212 B
Thus, Number of bits in page offset = 12 bits
So, Physical address is-
Operating System Concepts – 8th Edition 8.55 Silberschatz, Galvin and Gagne ©2009
…Problem 3
Process Size-
Number of bits in virtual address space = 32 bits
Thus,
Process size
= 232 B
= 4 GB
Number of Entries in Page Table-
Number of pages the process is divided
= Process size / Page size
= 4 GB / 4 KB
= 220 pages
Thus, Number of entries in page table = 220 entries
Operating System Concepts – 8th Edition 8.56 Silberschatz, Galvin and Gagne ©2009
…Problem 3
Page Table Size-
Operating System Concepts – 8th Edition 8.57 Silberschatz, Galvin and Gagne ©2009
Translation Lookaside Buffer
Disadvantage Of Paging-
One major disadvantage of paging is-
It increases the effective access time due to increased number of memory accesses.
One memory access is required to get the frame number from the page table.
Another memory access is required to get the word from the page.
Translation Lookaside Buffer-
Translation Lookaside Buffer (TLB) is a solution that tries to reduce the effective access
time.
Being a hardware, the access time of TLB is very less as compared to the main memory.
Operating System Concepts – 8th Edition 8.58 Silberschatz, Galvin and Gagne ©2009
Structure-
Translation Lookaside Buffer (TLB) consists of two columns-
Page Number
Frame Number
Operating System Concepts – 8th Edition 8.59 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
In a paging scheme using TLB, the logical address generated by the CPU is translated into
the physical address using following steps-
Step-01:
CPU generates a logical address consisting of two parts-
Page Number
Page Offset
Step-02:
TLB is checked to see if it contains an entry for the referenced page number.
The referenced page number is compared with the TLB entries all at once.
Now, two cases are possible-
Case-01: If there is a TLB hit-
If TLB contains an entry for the referenced page number, a TLB hit occurs.
In this case, TLB entry is used to get the corresponding frame number for the referenced
page number.
Operating System Concepts – 8th Edition 8.60 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Operating System Concepts – 8th Edition 8.61 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Diagram-
The following diagram illustrates the above steps of translating logical address into physical
address-
Operating System Concepts – 8th Edition 8.62 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Flowchart-
The following flowchart illustrates the above steps of translating logical address into physical
address-
Operating System Concepts – 8th Edition 8.63 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Important Points-
Point-01:
Unlike page table, there exists only one TLB in the system.
So, whenever context switching occurs, the entire content of TLB is flushed and deleted.
TLB is then again updated with the currently running process.
Point-02:
When a new process gets scheduled-
Initially, TLB is empty. So, TLB misses are frequent.
With every access from the page table, TLB is updated.
After some time, TLB hits increases and TLB misses reduces.
Point-03:
The time taken to update TLB after getting the frame number from the page table is
negligible.
Also, TLB is updated in parallel while fetching the word from the main memory.
Operating System Concepts – 8th Edition 8.64 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Advantages-
The advantages of using TLB are-
TLB reduces the effective access time.
Only one memory access is required when TLB hit occurs.
Disadvantages-
A major disadvantage of using TLB is-
When a context switching occurs, the entire content of the TLB is flushed.
Then, TLB is again updated with the currently running process. This happens again and
again.
Other disadvantages are-
TLB can hold the data of only one process at a time.
When context switches occur frequently, the performance of TLB degrades due to low hit
ratio.
As it is a special hardware, it involves additional cost.
Operating System Concepts – 8th Edition 8.65 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address into Physical Address
Operating System Concepts – 8th Edition 8.66 Silberschatz, Galvin and Gagne ©2009
Problem 1
Operating System Concepts – 8th Edition 8.67 Silberschatz, Galvin and Gagne ©2009
…Problem 1
Calculating Effective Access Time-
Substituting values in the above formula, we get-
Effective Access Time
= 0.9 x { 10 ns + 50 ns } + 0.1 x { 10 ns + 2 x 50 ns }
= 0.9 x 60 ns + 0.1 x 110 ns
= 54 ns + 11 ns
= 65 ns
Operating System Concepts – 8th Edition 8.68 Silberschatz, Galvin and Gagne ©2009
Multilevel Paging
Multilevel paging is a paging scheme where there exists a hierarchy of page tables.
Need –
The need for multilevel paging arises when-
The size of page table is greater than the frame size.
As a result, the page table can not be stored in a single frame in main memory.
Working-
In multilevel paging,
The page table having size greater than the frame size is divided into several parts.
The size of each part is same as frame size except possibly the last part.
The pages of page table are then stored in different frames of the main memory.
To keep track of the frames storing the pages of the divided page table, another page table
is maintained.
As a result, the hierarchy of page tables get generated.
Multilevel paging is done till the level is reached where the entire page table can be
stored in a single frame
Operating System Concepts – 8th Edition 8.69 Silberschatz, Galvin and Gagne ©2009
Illustration of Multilevel Paging
Consider a system using paging scheme where-
Logical Address Space = 4 GB
Physical Address Space = 16 TB
Page size = 4 KB
Now, let us find how many levels of page table will be required.
Operating System Concepts – 8th Edition 8.70 Silberschatz, Galvin and Gagne ©2009
…Illustration of Multilevel Paging
Number of Frames in Main Memory-
Number of frames in main memory
= Size of main memory / Frame size
= 16 TB / 4 KB
= 232 frames
Thus, Number of bits in frame number = 32 bits
Number of Bits in Page Offset-
We have,
Page size
= 4 KB
= 212 B
Thus, Number of bits in page offset = 12 bits
Operating System Concepts – 8th Edition 8.71 Silberschatz, Galvin and Gagne ©2009
…Illustration of Multilevel Paging
Alternatively,
Number of bits in page offset
Number of bits in physical address – Number of bits in frame number
= 44 bits – 32 bits
= 12 bits
So, Physical address is-
Operating System Concepts – 8th Edition 8.72 Silberschatz, Galvin and Gagne ©2009
…Illustration of Multilevel Paging
Number of Pages of Process-
Number of pages the process is divided
= Process size / Page size
= 4 GB / 4 KB
= 220 pages
Inner Page Table Size-
Inner page table keeps track of the frames storing the pages of process.
Inner Page table size
= Number of entries in inner page table x Page table entry size
= Number of pages the process is divided x Number of bits in frame number
= 220 x 32 bits
= 220 x 4 bytes
= 4 MB
Operating System Concepts – 8th Edition 8.73 Silberschatz, Galvin and Gagne ©2009
…Illustration of Multilevel Paging
Now, we can observe-
The size of inner page table is greater than the frame size (4 KB).
Thus, inner page table can not be stored in a single frame.
So, inner page table has to be divided into pages.
Number of Pages of Inner Page Table-
Number of pages the inner page table is divided
= Inner page table size / Page size
= 4 MB / 4 KB
= 210 pages
Now, these 210 pages of inner page table are stored in different frames of the main memory.
Operating System Concepts – 8th Edition 8.76 Silberschatz, Galvin and Gagne ©2009
…Illustration of Multilevel Paging
Operating System Concepts – 8th Edition 8.77 Silberschatz, Galvin and Gagne ©2009
Page Fault
When a page referenced by the CPU is not found in the main memory, it is called as a page
fault.
When a page fault occurs, the required page has to be fetched from the secondary memory
into the main memory.
Translating Logical Address into Physical Address-
In a paging scheme using TLB with possibility of page fault,
The logical address generated by the CPU is translated into the physical address using the
following steps-
Step-01:
CPU generates a logical address consisting of two parts-
Page Number
Page Offset
Step-02:
TLB is checked to see if it contains an entry for the referenced page number.
The referenced page number is compared with the TLB entries all at once.
Now, two cases are possible-
Operating System Concepts – 8th Edition 8.78 Silberschatz, Galvin and Gagne ©2009
Page Fault
Case-01: If there is a TLB hit-
• If TLB contains an entry for the referenced page number, a TLB hit occurs.
• In this case, TLB entry is used to get the frame number for the referenced page number.
Case-02: If there is a TLB miss-
• If TLB does not contain an entry for the referenced page number, a TLB miss occurs.
• In this case, page table is used to get the frame number for the referenced page number.
• The valid / invalid bit of the page table entry indicates whether the referenced page is
present in the main memory or not.
• now, two cases are possible-
Operating System Concepts – 8th Edition 8.79 Silberschatz, Galvin and Gagne ©2009
…Page Fault
Case-02: If Valid / Invalid Bit is Set to 0-
If valid / invalid bit is set to 0, it indicates that the page is not present in the main memory.
A page fault occurs.
The occurrence of page fault calls the page fault interrupt which executes the page fault handling
routine.
Page Fault Handling Routine-
The following sequence of events take place-
The currently running process is stopped and context switching occurs.
The referenced page is copied from the secondary memory to the main memory.
If the main memory is already full, a page is replaced to create a room for the referenced page.
After copying the referenced page successfully in the main memory, the page table is updated.
When the execution of process is resumed, step-02 repeats.
Step-03:
After the frame number is obtained, it is combined with the page offset to generate the physical
address.
Then, physical address is used to read the required word from the main memory.
Operating System Concepts – 8th Edition 8.80 Silberschatz, Galvin and Gagne ©2009
… Page Fault
Flowchart-
The following flowchart illustrates the above steps of translating logical address into
physical address-
Yes
No
Operating System Concepts – 8th Edition 8.81 Silberschatz, Galvin and Gagne ©2009
… Page Fault
Page Fault Service Time-
The time taken by the page fault handling routine to service the page fault
is called as page fault service time.
Page fault service time is much greater than main memory access time.
It increases the effective access time.
Operating System Concepts – 8th Edition 8.82 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms
Page Replacement
Page replacement is a process of swapping out an existing page from the frame of a main memory and
replacing it with the required page.
Page replacement is required when-
All the frames of main memory are already occupied.
Thus, a page has to be replaced to create a room for the required page.
A good page replacement algorithm is one that minimizes the number of page faults.
Operating System Concepts – 8th Edition 8.83 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms
FIFO Page Replacement Algorithm-
As the name suggests, this algorithm works on the principle of “First in First out“.
It replaces the oldest page that has been present in the main memory for the longest time.
It is implemented by keeping track of all the pages in a queue.
Operating System Concepts – 8th Edition 8.84 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms
LRU Page Replacement Algorithm-
As the name suggests, this algorithm works on the principle of “Least Recently Used“.
It replaces the page that has not been referred by the CPU for the longest time.
Operating System Concepts – 8th Edition 8.85 Silberschatz, Galvin and Gagne ©2009
Problem1
A system uses 3 page frames for storing process pages in main memory. It uses the First in
First out (FIFO) page replacement policy. Assume that all the page frames are initially
empty. What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Solution---Total number of references = 10
From here,
Total number of page faults occurred = 6
Operating System Concepts – 8th Edition 8.86 Silberschatz, Galvin and Gagne ©2009
…Problem1
Calculating Hit ratio-
Total number of page hits
= Total number of references – Total number of page misses or page faults
= 10 – 6
= 4
Operating System Concepts – 8th Edition 8.87 Silberschatz, Galvin and Gagne ©2009
Problem1
Calculating Miss ratio-
Total number of page misses or page faults = 6
Thus, Miss ratio
= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%
Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%
Operating System Concepts – 8th Edition 8.88 Silberschatz, Galvin and Gagne ©2009
Problem 2
A system uses 3 page frames for storing process pages in main memory. It uses the Least
Recently Used (LRU) page replacement policy. Assume that all the page frames are initially
empty. What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Solution-
Total number of references = 10
From here,
Total number of page faults occurred = 6
In the similar manner as above-
Hit ratio = 0.4 or 40%
Miss ratio = 0.6 or 60%
Operating System Concepts – 8th Edition 8.89 Silberschatz, Galvin and Gagne ©2009
Problem 3
A system uses 3 page frames for storing process pages in main memory. It
uses the Optimal page replacement policy.
From here,
Total number of page faults occurred = 5Assume that all the page frames
are initially empty. What is the total number of page faults that will
occur while processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Solution-
Total number of references = 10
6 came before 1
From here,
Total number of page faults occurred = 5
In the similar manner as above-
Hit ratio = 0.5 or 50%
Miss
Operating System ratio
Concepts = 0.5
– 8th Edition or 50% 8.90 Silberschatz, Galvin and Gagne ©2009
PRACTICE PROBLEMS BASED ON PAGE FAULTS IN OS
Operating System Concepts – 8th Edition 8.91 Silberschatz, Galvin and Gagne ©2009
Effective Access Time
Operating System Concepts – 8th Edition 8.92 Silberschatz, Galvin and Gagne ©2009
Problem 1
Let the page fault service time be 10 ms in a computer with average memory access time
being 20 ns. If one page fault is generated for every 106 memory accesses, what is the
effective access time for the memory?
Solution-
Given-
Page fault service time = 10 ms
Average memory access time = 20 ns
One page fault occurs for every 106 memory accesses
Page Fault Rate-
It is given that one page fault occurs for every 106 memory accesses.
Thus,
Page fault rate
= 1 / 106
= 10-6
Operating System Concepts – 8th Edition 8.93 Silberschatz, Galvin and Gagne ©2009
Problem 1
Effective Access Time With Page Fault-
It is given that effective memory access time without page fault = 20 ns.
Operating System Concepts – 8th Edition 8.94 Silberschatz, Galvin and Gagne ©2009
Belady’s Anomaly
Effect of Increasing Number of Frames
The number of page faults should either decrease or remain constant on
increasing the number of frames in main memory.
But sometimes the unusual behavior is observed.
Sometimes, on increasing the number of frames in main memory, the number of
page faults also increase.
Operating System Concepts – 8th Edition 8.95 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomaly
Operating System Concepts – 8th Edition 8.96 Silberschatz, Galvin and Gagne ©2009
Reason behind Belady’s Anomally
An algorithm suffers from Belady‟s Anomaly if and only if it does not
follow stack property.
Algorithms that follow stack property are called as stack based algorithms.
Stack based algorithms do not suffer from Belady‟s Anomaly.
This is because these algorithms assign priority to a page for replacement
that is independent of the number of frames in the main memory.
Operating System Concepts – 8th Edition 8.97 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomaly
Examples-
Following page replacement algorithms are stack based algorithms-
LRU Page Replacement Algorithm
Optimal Page Replacement Algorithm
Hence, they do not suffer from Belady‟s Anomaly.
Stack Property-
Consider-
Initially, we had „m‟ number of frames in the main memory.
Now, the number of frames in the main memory is increased to „m+1‟.
According to stack property-
At each stage, the set of pages that were present in the main memory when
number of frames is „m‟ will be compulsorily present in the corresponding
stages in main memory when the number of frames is increased to „m+1‟.
Operating System Concepts – 8th Edition 8.98 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomaly
Illustration-01: For Optimal Page Replacement Algorithm-
Consider the reference string is-
0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Now, observe the following two cases-
Case-01: When frame size = 3
Operating System Concepts – 8th Edition 8.99 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomaly
Case-02: When frame size = 4
0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Operating System Concepts – 8th Edition 8.100 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomally
Illustration-02: For LRU Page Replacement Algorithm-
Consider the reference string is-
0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Now, observe the following two cases-
Case-01: When frame size = 3
Operating System Concepts – 8th Edition 8.101 Silberschatz, Galvin and Gagne ©2009
Belady’s Anomally
Case-02: When frame size = 4
0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Operating System Concepts – 8th Edition 8.102 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomally
Illustration-03: For FIFO Page Replacement Algorithm-
Consider the reference string is- 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Now, observe the following two cases-
Case-01: When frame size = 3
Operating System Concepts – 8th Edition 8.103 Silberschatz, Galvin and Gagne ©2009
…Belady’s Anomaly
Case-02: When frame size = 4
0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
Operating System Concepts – 8th Edition 8.104 Silberschatz, Galvin and Gagne ©2009
Belady’s Anomally
NOTE-
FIFO Page Replacement Algorithm suffers from Belady‟s Anomaly.
It would be wrong to say that “FIFO Page Replacement Algorithm always suffer from Belady‟s Anomaly”.
Operating System Concepts – 8th Edition 8.105 Silberschatz, Galvin and Gagne ©2009
Inverted Page Table
An inverted page table is one of the techniques to structure a page table, where
the page table is indexed by the actual frame number in the physical memory.
The entry in the table corresponds to the logical page number p and the process-
id (pid) of the process that owns that page. The tuple (pid,p) is extracted
whenever a memory reference happens and is forwarded to the memory subsystem that
contains the inverted page table.
Source: https://www.educative.io/answers/what-is-an-inverted-
page-table-in-operating-systems
Operating System Concepts – 8th Edition 8.106 Silberschatz, Galvin and Gagne ©2009
…Inverted Page Table
The memory subsystem searches through the inverted page table for a match. If a
match is found at an entry, such as i, then the corresponding physical address
consists of i and the offset (d). If no match is found, this means an address
was accessed illegally.
Why inverted page table?
An inverted page table has exactly one entry for each physical frame in the real
memory, and therefore only one inverted page table exists in an operating system.
This is in contrast to the standard hierarchical page tables, which are indexed
by logical address pages, and each process has its own separate page table.
Through the inverted page table, the overhead of storing an individual page table
for every process gets eliminated and only a fixed portion of memory is required
to store the paging information of all the processes together, which eliminates
memory wastage.
This technique is called inverted paging as the indexing is done with respect to
the frame number instead of the logical page number.
Operating System Concepts – 8th Edition 8.107 Silberschatz, Galvin and Gagne ©2009
Segmentation
Like Paging, Segmentation is another non-contiguous memory allocation technique.
In segmentation, process is not divided blindly into fixed size pages.
Rather, the process is divided into modules for better visualization.
Characteristics-
Segmentation is a variable size partitioning scheme.
In segmentation, secondary memory and main memory are divided into partitions of unequal
size.
The size of partitions depend on the length of modules.
The partitions of secondary memory are called as segments.
Operating System Concepts – 8th Edition 8.108 Silberschatz, Galvin and Gagne ©2009
Example-
Consider a program is divided into 5 segments as-
Operating System Concepts – 8th Edition 8.109 Silberschatz, Galvin and Gagne ©2009
Segment Table
Segment table is a table that stores the information about each segment of the process.
It has two columns.
First column stores the size or length of the segment.
Second column stores the base address or starting address of the segment in the main memory.
Segment table is stored as a separate segment in the main memory.
Segment table base register (STBR) stores the base address of the segment table.
For the above illustration, consider the segment table is-
Operating System Concepts – 8th Edition 8.110 Silberschatz, Galvin and Gagne ©2009
…Segment Table
Here,
Limit indicates the length or size of the segment.
Base indicates the base address or starting address of the segment in the main memory.
In accordance to the above segment table, the segments are stored in the main memory as-
Operating System Concepts – 8th Edition 8.111 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
Segment Number specifies the specific segment of the process from which CPU wants to read the data.
Segment Offset specifies the specific word in the segment that CPU wants to read.
Operating System Concepts – 8th Edition 8.112 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
Step-02:
For the generated segment number, corresponding entry is located in the segment table.
Then, segment offset is compared with the limit (size) of the segment.
Now, two cases are possible-
Operating System Concepts – 8th Edition 8.113 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
The following diagram illustrates the above steps of translating logical address into physical
address-
Operating System Concepts – 8th Edition 8.114 Silberschatz, Galvin and Gagne ©2009
Segmentation
Advantages-
The advantages of segmentation are-
It allows to divide the program into modules which provides better visualization.
Segment table consumes less space as compared to Page Table in paging.
It solves the problem of internal fragmentation.
Disadvantages-
The disadvantages of segmentation are-
There is an overhead of maintaining a segment table for each process.
The time taken to fetch the instruction increases since now two memory accesses are required.
Segments of unequal size are not suited for swapping.
It suffers from external fragmentation as the free space gets broken down into smaller pieces with
the processes being loaded and removed from the main memory.
Operating System Concepts – 8th Edition 8.115 Silberschatz, Galvin and Gagne ©2009
Problem
Consider the following segment table-
0 1219 700
1 2300 14
2 90 100
3 1327 580
4 1952 96
Which of the following logical address will produce trap addressing error?
a) 0, 430
b) 1, 11
c) 2, 100
d) 3, 425
e) 4, 95
Calculate the physical address if no trap is produced.
Operating System Concepts – 8th Edition 8.116 Silberschatz, Galvin and Gagne ©2009
Problem
Solution-
In a segmentation scheme, the generated logical address consists of two parts-
Segment Number
Segment Offset
We know-
Segment Offset must always lie in the range [0, limit-1].
If segment offset becomes greater than or equal to the limit of segment, then trap addressing error is
produced.
Option-A: 0, 430-
Here,
Segment Number = 0
Segment Offset = 430
We have,
In the segment table, limit of segment-0 is 700.
Thus, segment offset must always lie in the range = [0, 700-1] = [0, 699]
Now,
Since generated segment offset lies in the above range, so request generated is valid.
Therefore, no trap will be produced.
Physical Address = 1219 + 430 = 1649
Operating System Concepts – 8th Edition 8.117 Silberschatz, Galvin and Gagne ©2009
…Problem
Option-B: 1, 11-
Here,
Segment Number = 1
Segment Offset = 11
We have,
In the segment table, limit of segment-1 is 14.
Thus, segment offset must always lie in the range = [0, 14-1] = [0, 13]
Now,
Since generated segment offset lies in the above range, so request generated is valid.
Therefore, no trap will be produced.
Physical Address = 2300 + 11 = 2311
Operating System Concepts – 8th Edition 8.118 Silberschatz, Galvin and Gagne ©2009
…Problem
Option-C: 2, 100-
Here,
Segment Number = 2
Segment Offset = 100
We have,
In the segment table, limit of segment-2 is 100.
Thus, segment offset must always lie in the range = [0, 100-1] = [0, 99]
Now,
Since generated segment offset does not lie in the above range, so request generated is invalid.
Therefore, trap will be produced.
Operating System Concepts – 8th Edition 8.119 Silberschatz, Galvin and Gagne ©2009
…Problem
Option-D: 3, 425-
Here,
Segment Number = 3
Segment Offset = 425
We have,
In the segment table, limit of segment-3 is 580.
Thus, segment offset must always lie in the range = [0, 580-1] = [0, 579]
Now,
Since generated segment offset lies in the above range, so request generated is valid.
Therefore, no trap will be produced.
Physical Address = 1327 + 425 = 1752
Operating System Concepts – 8th Edition 8.120 Silberschatz, Galvin and Gagne ©2009
…Problem
Option-E: 4, 95
Here,
Segment Number = 4
Segment Offset = 95
We have,
In the segment table, limit of segment-4 is 96.
Thus, segment offset must always lie in the range = [0, 96-1] = [0, 95]
Now,
Since generated segment offset lies in the above range, so request generated is valid.
Therefore, no trap will be produced.
Physical Address = 1952 + 95 = 2047
Thus, Option-(C) is correct.
Operating System Concepts – 8th Edition 8.121 Silberschatz, Galvin and Gagne ©2009
Segmented Paging
Segmented paging is a scheme that implements the combination of segmentation and paging.
Working-
In segmented paging,
Process is first divided into segments and then each segment is divided into pages.
These pages are then stored in the frames of main memory.
A page table exists for each segment that keeps track of the frames storing the pages of that
segment.
Each page table occupies one frame in the main memory.
Number of entries in the page table of a segment = Number of pages that segment is divided.
A segment table exists that keeps track of the frames storing the page tables of segments.
Number of entries in the segment table of a process = Number of segments that process is divided.
The base address of the segment table is stored in the segment table base register.
Operating System Concepts – 8th Edition 8.122 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
Segment Number specifies the specific segment from which CPU wants to reads the data.
Page Number specifies the specific page of that segment from which CPU wants to read the data.
Page Offset specifies the specific word on that page that CPU wants to read.
Operating System Concepts – 8th Edition 8.123 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
Step-02:
For the generated segment number, corresponding entry is located in the segment table.
Segment table provides the frame number of the frame storing the page table of the referred segment.
The frame containing the page table is located.
Step-03:
For the generated page number, corresponding entry is located in the page table.
Page table provides the frame number of the frame storing the required page of the referred segment.
The frame containing the required page is located.
Step-04:
The frame number combined with the page offset forms the required physical address.
For the generated page offset, corresponding word is located in the page and read.
Operating System Concepts – 8th Edition 8.124 Silberschatz, Galvin and Gagne ©2009
Translating Logical Address to Physical Address
The following diagram illustrates the above steps of translating logical address into physical
address-
Operating System Concepts – 8th Edition 8.125 Silberschatz, Galvin and Gagne ©2009
Segmented Paging
Advantages-
Segment table contains only one entry corresponding to each segment.
It reduces memory wastage.
The size of Page Table is limited by the segment size.
It solves the problem of external fragmentation.
Disadvantages-
Segmented paging suffers from internal fragmentation.
The complexity level is much higher as compared to paging.
Operating System Concepts – 8th Edition 8.126 Silberschatz, Galvin and Gagne ©2009