Tag: Demand Paging in OS

Translation Lookaside Buffer | TLB | Paging

Paging in OS-

 

Before you go through this article, make sure that you have gone through the previous article on Paging in OS.

 

We have discussed-

  • Paging in OS is a non-contiguous memory allocation technique.
  • Page Table is a data structure that maps page number to the frame number.

 

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.

 

Structure-

 

Translation Lookaside Buffer (TLB) consists of two columns-

  1. Page Number
  2. Frame Number

 

 

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-

  1. Page Number
  2. 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.

 

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 corresponding frame number for the referenced page number.
  • Then, TLB is updated with the page number and frame number for future references.

 

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.

 

NOTE-

 

In the above discussion, we have assumed that no Page Fault occurs.

 

Diagram-

 

The following diagram illustrates the above steps of translating logical address into physical address-

 

 

Flowchart-

 

The following flowchart illustrates the above steps of 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.

 

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-

  • After some time of running the process, when TLB hits increases and process starts to run smoothly, 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.

 

Effective Access Time-

 

In a single level paging using TLB, the effective access time is given as-

 

 

This formula is valid only when there is single level paging and there are no page faults.

 

PRACTICE PROBLEMS BASED ON TRANSLATION LOOKASIDE BUFFER-

 

Problem-01:

 

A paging scheme uses a Translation Lookaside buffer (TLB). A TLB access takes 10 ns and a main memory access takes 50 ns. What is the effective access time (in ns) if the TLB hit ratio is 90% and there is no page fault?

  1. 54
  2. 60
  3. 65
  4. 75

 

Solution-

 

Given-

  • TLB access time = 10 ns
  • Main memory access time = 50 ns
  • TLB Hit ratio = 90% = 0.9

 

Calculating TLB Miss Ratio-

 

TLB Miss ratio

= 1 – TLB Hit ratio

= 1 – 0.9

= 0.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

Thus, Option (C) is correct.

 

Problem-02:

 

A paging scheme uses a Translation Lookaside buffer (TLB). The effective memory access takes 160 ns and a main memory access takes 100 ns. What is the TLB access time (in ns) if the TLB hit ratio is 60% and there is no page fault?

  1. 54
  2. 60
  3. 20
  4. 75

 

Solution-

 

Given-

  • Effective access time = 160 ns
  • Main memory access time = 100 ns
  • TLB Hit ratio = 60% = 0.6

 

Calculating TLB Miss Ratio-

 

TLB Miss ratio

= 1 – TLB Hit ratio

= 1 – 0.6

= 0.4

 

Calculating TLB Access Time-

 

Let TLB access time = T ns.

Substituting values in the above formula, we get-

160 ns = 0.6 x { T + 100 ns } + 0.4 x { T + 2 x 100 ns }

160 = 0.6 x T + 60 + 0.4 x T + 80

160 = T + 140

T = 160 – 140

T = 20

 

Thus, Option (C) is correct.

 

To gain better understanding about Translation Lookaside Buffer (TLB),

Watch this Video Lecture

 

Next Article- Multilevel Paging

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Paging in OS | Formulas | Practice Problems

Paging in OS-

 

Before you go through this article, make sure that you have gone through the previous article on Paging in OS.

 

We have discussed-

  • Paging is a non-contiguous memory allocation technique.
  • Page Table is a data structure that performs the mapping of page number to the frame number.

 

Important Formulas-

 

The following list of formulas is very useful for solving the numerical problems based on paging.

 

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 pages 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

 

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.

 

PRACTICE PROBLEMS BASED ON PAGING AND PAGE TABLE-

 

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

 

Thus, Size of memory

= 222 x 2 bytes

= 223 bytes

= 8 MB

 

Problem-02:

 

Calculate the number of bits required in the address for memory having size of 16 GB. Assume the memory is 4-byte addressable.

 

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

 

Problem-03:

 

Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte page size and page table entries of 4 bytes each. The size of the page table in the system in megabytes is _____.

  1. 2
  2. 4
  3. 8
  4. 16

 

Solution-

 

Given-

  • Number of bits in logical address = 32 bits
  • Page size = 4KB
  • Page table entry size = 4 bytes

 

Process Size-

 

Number of bits in logical address = 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

 

Page Table Size-

 

Page table size

= Number of entries in page table x Page table entry size

= 220 x 4 bytes

= 4 MB

 

Thus, Option (B) is correct.

 

Problem-04:

 

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?

  1. 16 MB
  2. 8 MB
  3. 2 MB
  4. 24 MB

 

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

 

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-

 

 

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

 

Page Table Size-

 

Page table size

= Number of entries in page table x Page table entry size

= Number of entries in page table x Number of bits in frame number

= 220 x 14 bits

= 220 x 16 bits      (Approximating 14 bits ≈ 16 bits)

= 220 x 2 bytes

= 2 MB

 

Thus, Option (C) is correct.

 

Problem-05:

 

In a virtual memory system, size of virtual address is 32-bit, size of physical address is 30-bit, page size is 4 Kbyte and size of each page table entry is 32-bit. The main memory is byte addressable. Which one of the following is the maximum number of bits that can be used for storing protection and other information in each page table entry?

  1. 2
  2. 10
  3. 12
  4. 14

 

Solution-

 

Given-

  • Number of bits in virtual address = 32 bits
  • Number of bits in physical address = 30 bits
  • Page size = 4 KB
  • Page table entry size = 32 bits

 

Size of Main Memory-

 

Number of bits in physical address = 30 bits

Thus,

Size of main memory

= 230 B

= 1 GB

 

Number of Frames in Main Memory-

 

Number of frames in main memory

= Size of main memory / Frame size

= 1 GB / 4 KB

= 230 B / 212 B

= 218

Thus, Number of bits in frame number = 18 bits

 

Number of Bits used for Storing other Information-

 

Maximum number of bits that can be used for storing protection and other information

= Page table entry size – Number of bits in frame number

= 32 bits – 18 bits

= 14 bits

 

Thus, Option (D) is correct.

 

To gain better understanding about solving numerical problems on paging,

Watch this Video Lecture

 

Next Article- Optimal Page Size | Practice Problems

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Page Table | Paging in Operating System

Paging in OS-

 

Before you go through this article, make sure that you have gone through the previous article on Paging in OS.

 

We have discussed-

  • Paging is a non-contiguous memory allocation technique.
  • The logical address generated by the CPU is translated into the physical address using the page table.

 

In this article, we will discuss about Page Table.

 

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.

 

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.

 

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-

 

 

1. 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.

 

2. 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.

 

3. 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.

 

4. 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.

 

5. 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.

 

6. 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.

 

To gain better understanding about Page Table Entry,

Watch this Video Lecture

 

Next Article- Paging Important Formulas

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Paging | Memory Management | Operating System

Non-Contiguous Memory Allocation-

 

  • Non-contiguous memory allocation is a memory allocation technique.
  • It allows to store parts of a single process in a non-contiguous fashion.
  • Thus, different parts of the same process can be stored at different places in the main memory.

 

Techniques-

 

There are two popular techniques used for non-contiguous memory allocation-

 

 

  1. Paging
  2. Segmentation

 

In this article, we will discuss about Paging.

Learn about Segmentation.

 

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.

 

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-

 

 

Also Read- Contiguous Memory Allocation

 

Translating Logical Address into Physical Address-

 

  • CPU always generates a logical address.
  • A physical address is needed to access the main memory.

 

Following steps are followed to translate logical address into physical address-

 

Step-01:

 

CPU generates a logical address consisting of two parts-

  1. Page Number
  2. Page Offset

 

 

  • 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.

 

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.

 

Diagram-

 

The following diagram illustrates the above steps of translating logical address into physical address-

 

 

Advantages-

 

The advantages of paging are-

  • It allows to store parts of a single process in a non-contiguous fashion.
  • It solves the problem of external fragmentation.

 

Disadvantages-

 

The disadvantages of paging are-

  • 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 now two memory accesses are required.

 

To gain better understanding about Paging,

Watch this Video Lecture

 

Next Article- Page Table | Page Table Entry

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.