Docsity

Paging in operating system, Summaries of Operating Systems

Toggle sidebar
bg1
Operating System - Paging
Paging is a memory management technique used by modern operating
systems to manage the allocation of memory to processes. In this
technique, the physical memory (i.e., the RAM) is divided into blocks of
fixed size called frames, and the logical memory (i.e., the memory where
the process is stored) is divided into blocks of the same size called pages.
When a process is executed, its pages are loaded into available frames in
the physical memory.
The image below shows working of paging in operating system –
Key Concepts Related to Paging
Here are some key concepts related to paging āˆ’
1. Logical Address Space āˆ’ The logical address space is the set of all
logical addresses generated by a process in hard disk. It is divided
into pages.
2. Physical Address Space āˆ’ The physical address space refers to the
set of all physical addresses in the main memory (RAM). It is divided
into frames.
3. Page Table āˆ’ A page table is a data structure used by the operating
system to direct logical addresses to physical addresses. It stores
information such as the location of each page in the physical memory.
4. Page Number āˆ’ The page number is the index of a page in the logical
address space.
5. Frame Number āˆ’ The frame number is the index of a frame in the
physical address space.
6. Page Size āˆ’ The page size is the size of each page and frame. It is
usually represented as a power of 2, For example, 4KB, 8KB, etc.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56

Partial preview of the text

Download Paging in operating system and more Summaries Operating Systems in PDF only on Docsity!

Operating System - Paging

Paging is a memory management technique used by modern operating systems to manage the allocation of memory to processes. In this technique, the physical memory (i.e., the RAM) is divided into blocks of fixed size called frames, and the logical memory (i.e., the memory where the process is stored) is divided into blocks of the same size called pages. When a process is executed, its pages are loaded into available frames in the physical memory. The image below shows working of paging in operating system – Key Concepts Related to Paging Here are some key concepts related to paging āˆ’

  1. Logical Address Space āˆ’ The logical address space is the set of all logical addresses generated by a process in hard disk. It is divided into pages.
  2. Physical Address Space āˆ’ The physical address space refers to the set of all physical addresses in the main memory (RAM). It is divided into frames.
  3. Page Table āˆ’ A page table is a data structure used by the operating system to direct logical addresses to physical addresses. It stores information such as the location of each page in the physical memory.
  4. Page Number āˆ’ The page number is the index of a page in the logical address space.
  5. Frame Number āˆ’ The frame number is the index of a frame in the physical address space.
  6. Page Size āˆ’ The page size is the size of each page and frame. It is usually represented as a power of 2, For example, 4KB, 8KB, etc.

Address Translation in Paging

When a process generates a logical address, the operating system uses the page table to translate it into a physical address. This process is known as address translation. The logical address is divided into two parts āˆ’

  • The page number
  • The offset within the page The page number is used to search the corresponding frame number in the page table, and the offset is the location within that frame. The physical address is then calculated by combining the frame number and the offset. Here is the formula to calculate the physical address:

Physical Address=(Frame Number Ɨ Page Size )+Offset

Page Table in Paging

The page table is a data structure inside the operating system that maps logical addresses and physical addresses. Each entry in the page table contains the following information:

  1. Frame Number āˆ’ The frame number refer to the location of the page in physical memory.
  2. Valid/Invalid Bit āˆ’ This bit is used to know if the page is in memory (valid) or not (invalid).
  3. Protection Bits āˆ’ Bits that is used to indicate the permission of accessing the page (e.g., read, write, execute).
  4. Dirty Bit āˆ’ A bit that is used to indicate if the page has been modified since it was loaded into memory.
  5. Reference Bit āˆ’ A bit that is used to indicate whether the page has been accessed recently.

Example: Structure of a Page Table

Consider a system with following configuration āˆ’

  • System A 32-bit system with a 4KB (2^12 bytes) page size.
  • Logical Address Space 232 bytes (4GB).
  • Physical Address Space 230 bytes (1GB).
  • Number of Pages 232 / 2^12 = 2^20 pages. In this case, the page table will have 2^20 entries. Each entry in the page table will contain the frame number, valid/invalid bit, protection bits,

Effective Access Time (EAT)

The Effective Access Time (EAT) is the average time taken to access a memory location, by considering both TLB hits and TLB misses. It can be calculated using the following formula āˆ’ EAT=(HƗT) + (MƗ (T+M.A. T)) Where,

  • H = TLB Hit Ratio āˆ’ The percentage of memory accesses that leads to TLB hit.
  • M = TLB Miss Ratio āˆ’ The percentage of memory accesses that leads to TLB miss (1 - TLB Hit Ratio).
  • T = TLB Access Time āˆ’ The time taken to access the TLB.
  • M.A.T = Memory Access Time āˆ’ The time taken to access the main memory. For example, if the TLB hit ratio is 90%, TLB access time is 10 nanoseconds, and memory access time is 100 nanoseconds, the EAT can be calculated as follows āˆ’ EAT=(0.9Ɨ10 ns)+(0.1Ɨ(10 ns+100 ns)) =9 ns+11 ns=20 ns So, the effective access time in this case is 20 nanoseconds.

Pros and Cons of Paging

Here are some advantages of implementing paging in operating systems āˆ’

  • Remove External Fragmentation āˆ’ Paging helps to reduce external fragmentation issues by dividing memory into fixed-size pages and frames.
  • Efficient Memory Utilization āˆ’ By implementing paging, memory can be utilized more efficiently, because pages can be loaded into any available frame in physical memory.
  • Simplified Memory Management āˆ’ Paging simplifies management of memory inside the operating system. It eliminates the need for contiguous allocation of memory.
  • Extra Physical Memory āˆ’ Paging allows the use of secondary storage (like hard disk) as an extension of physical memory, which is known as virtual memory.

Paging comes with some disadvantages as well, such as āˆ’

  • Overhead of Page Table āˆ’ If the size of the page table is large, it will consume large amount of memory and can lead to performance issues.
  • Increased Memory Access Time āˆ’ During address translation, if there is a TLB miss, then it will take more time to access the page table, which can increase the overall memory access time.
  • Extra Complexity āˆ’ Implementing paging adds extra complexity while creating operating system.

In the image,

  • CPU request Page 2 āˆ’ First, the CPU requests Page 2. The page table is used to check if Page 2 is in main memory. In this case, it is not. So a page fault occurs.
  • Page Fault Handling āˆ’ The operating system is notified of the page fault. It locates Page 2 on the disk and finds a free frame in main memory.
  • Loading the Page āˆ’ The OS loads Page 2 from the disk into the free frame in main memory. Then the page table is updated with the new frame number for Page 2.
  • Resuming the Process āˆ’ Finally, the CPU is notified that Page 2 is now in memory, and the process can continue executing.

How Demand Paging Works?

Here is detailed step-by-step explanation of working process of demand paging āˆ’

1. Page Table and Valid/Invalid Bit Every process in the CPU has a page table that maps virtual addresses to physical addresses. For every entry in this page table, there is a column called the valid/invalid bit. If this bit is set to valid, it means that the corresponding page is currently in physical memory (RAM). And if it is invalid, it means that the page is not in physical memory but is stored on the disk. Accessing an invalid page will cause a page fault. 2. Page Fault Handling When a process tries to access a page in physical memory, the CPU checks the page table. Inside the page table, if the valid/invalid bit is set to invalid, a page fault occurs. The operating system then takes the following steps: - Locate the Page on Disk - The OS looks for where the required page is stored on the disk. - Find a Free Frame āˆ’ The OS checks for a free frame in physical memory (RAM). If no free frame is available, it uses a page replacement algorithm to remove a page from memory to free up space. - Load the Page āˆ’ The OS reads the required page from the disk and loads it into the selected frame in physical memory.

  • Update the Page Table āˆ’ The OS updates the page table with the new frame number and sets the valid/invalid bit to valid.
  • Restart the Instruction āˆ’ The instruction that caused the page fault is restarted, and the process can now access the required page in memory. The following image shows how an OS handles a page fault – 3. Page Replacement If there are no free frames available in physical memory, the OS uses page replacement algorithms to decide which page is to be removed from physical memory to free up space for the new page. Some common page replacement algorithms are āˆ’
  • Least Recently Used (LRU) āˆ’ The LRU algorithm removes the page that has not been used for the longest time.
  • First-In-First-Out (FIFO) āˆ’ The FIFO algorithm removes the oldest page in memory.
  • Optimal Page Replacement āˆ’ The optimal page replacement algorithm removes the page that will not be used for the longest period in the future (This is a theoretical concept and cannot be implemented in practice). 4. Thrashing Thrashing occurs when a system spends more time handling page faults than executing actual processes. This can happen if the size of process is

This is done based on the assumption that if one page is accessed, the pages near to it will be accessed soon. We have tabulated the differences between demand paging and pre paging for better understanding – Aspect Demand Paging Pre Paging Definition Pages are loaded into memory only when they are needed (on demand). Multiple pages are loaded into memory before they are actually needed. Page Faults More page faults can occur as pages are loaded only when accessed. Less page faults expected because multiple pages are preloaded, so it will reduce chances of accessing a non-loaded page. Memory Usage More efficient memory usage. Low memory efficiency. This leads to wastage of memory space. Performance Can lead to slower performance due to frequent page faults and disk I/O operations. Improved performance because of no page faults. Complexity Easy to implement More complex to implement as it requires predicting future page accesses.

Page Table

A page table is a data structure used during paging in virtual memory systems. We have already learned about paging and demand paging in previous sections. Read this chapter to learn the basics of page table and the data it stores. Following topics will be covered in this chapter āˆ’

  • What is Page Table?
  • Data Stored in Page Table
  • Types of Page Tables What is Page Table? A page table is a data structure inside the operating system that maps logical addresses and physical addresses. It stores other information about each page, such as whether it is in memory or on disk, whether it is read-only or read-write, and so on. These information will help the operating system to easily find the location of a page in main memory (RAM) when it is needed. The following image shows the structure and entries of a simple page table – The main purpose of a page table is to keep track of the mapping between the logical addresses used by a program and the physical addresses in the computer's memory. When a program try to access a memory location in RAM, the CPU first checks the page table to see the stored mapping for the corresponding logical address. If the page is already in memory, the CPU can directly access it. A page fault will occur if the page is not in memory. In such cases, the operating system need to load the page from disk into memory and then update the page table.

4. Reference Bit The reference bit is used by page replacement algorithms to keep track of whether a page has been accessed recently. When a page is accessed, the reference bit is set to 1. This information helps the operating system and page replacement algorithms to make decisions about which pages to replace when a page fault occurs. Pages that have been accessed recently (with reference bit set to 1) are less likely to be replaced compared to pages that have not been accessed recently (with reference bit set to 0). 5. Caching Bit The caching bit is used to indicate whether a page can be cached in the CPU cache or not. - Caching bit is set to 1 → The page can be cached. - Caching bit is set to 0 → The page cannot be cached. This bit is important for performance optimization. Pages that are frequently accessed should be cached to reduce memory access time. However, some pages, such as those used for memory-mapped I/O, should not be cached to ensure data consistency. 6. Dirty Bit The dirty bit is used to indicate whether a page has been modified since it was loaded into memory. - Dirty bit is set to 1 → The page has been modified. - Dirty bit is set to 0 → The page has not been modified. This information is important for page replacement algorithms. If a page with the dirty bit set to 1 is selected for replacement, it must be written back to disk before being replaced. This ensures that any changes made to the page are not lost. Types of Page Tables Depending on the system architecture and requirements, there are several types of page tables used in operating systems. Some of the common types include āˆ’ 1. Single Level Page Table In a single-level page table, there is a single table that maps all the pages of a process to their corresponding frames in physical memory. This type of page table is easy to implement. But it may not be suitable for systems with large address spaces, as the page table can become very large and consume a lot of memory.

2. Multi-Level Page Table In a multi-level page table, the page table is divided into multiple levels. Each level contains a smaller page table that maps a portion of the address space. This hierarchical structure helps to reduce the size of the page table and makes it more manageable. The most common type of multi-level page table is the two-level page table. 3. Inverted Page Table In an inverted page table, there is a single page table for the entire system. Each entry in the table corresponds to a frame in physical memory. The entry contains information about which page is currently stored in that frame. Also the process ID to which the page belongs. This type of page table is more memory efficient. Because it reduces the amount of memory needed to store page tables for multiple processes. However, it can be slower to access, as the system needs to search through the table to find the corresponding page for a given logical address.

In this example, the initial pages 5, 0, 1, 7 are loaded directly into the empty frames. Then, the next page 2 is referenced. Now, the frames are full, so according to FIFO, the oldest page 5 will be removed and 2 will be loaded into its place. This process continues for the rest of the page references. The table below shows each step of the process, including the current page reference – Step Reference Frames (Oldest to Newest) Page fault? Removed page 1 5 5 Yes (1) - 2 0 5, 0 Yes (2) - 3 1 5, 0, 1 Yes (3) - 4 7 5, 0, 1, 7 Yes (4) - 5 2 0, 1, 7, 2 Yes (5) 5 (oldest) 6 0 0, 1, 7, 2 No - 7 2 0, 1, 7, 2 No - 8 8 1, 7, 2, 8 Yes (6) 0 (oldest)

  • Total Page Faults = 6
  • Page Fault Rate = 6/8 = 75%
  • Pages Removed = 5, 0 The main drawback of FIFO page replacement algorithm is Belady's Anomaly. It states that increasing the number of page frames can sometimes increase the number of page faults. We have a complete chapter on Belady's Anomaly where we explained this concept with examples.

Least Recently Used (LRU) Page Replacement

The LRU is another popular page replacement algorithm. In this algorithm, the page that has not been used for the longest period of time will be replaced first. Meaning, the page that was least recently used will be removed to make space for the new page. This work like memory cache, where the item that has not been touched for the longest time is removed first.

This algorithm uses timestamps to keep track of page usage. When a page is accessed, its timestamp is updated to the current time. When a page needs to be replaced, the page with the oldest timestamp (i.e., the least recently used page) is removed from memory. That's how the LRU algorithm works. Example Consider the a page reference string of 1, 2, 3, 1, 4, 5 and with 3 frames of physical memory. That is,

  • Page Reference String: 1, 2, 3, 1, 4, 5
  • Number of Frames: 3 The initial pages 1, 2, 3 are loaded directly into the 3 empty frames. When the next page 1 is referenced, it is already in memory, so no page fault occurs. Next, page 4 is referenced, this can cause a page fault. So the LRU algorithm will remove page 2. (If it was FIFO, it would have removed page 1, because 2 is least recently used and 1 is oldest page). This process continues for the rest of the page references. The table below shows all the steps of the process – Step Reference Frames (Oldest to Newest) Page fault? Removed page 1 1 1 Yes (1) - 2 2 1, 2 Yes (2) - 3 3 1, 2, 3 Yes (3) - 4 1 1, 2, 3 No - 5 4 1, 3, 4 Yes (4) 2 6 5 3, 4, 5 Yes (5) 1
  • Total Page Faults = 5
  • Page Fault Rate = 5/6 = 83.33%
  • Pages Removed = 2, 1 The main drawback of LRU algorithm is that it can be expensive and difficult to implement. We need to keep track of the order of page usage, which requires data structures like stacks or linked lists. Also, updating the timestamps for each page access can be costly in terms of time and space complexity. However, LRU is the most popular page replacement algorithm used in many operating systems.
  • Total Page Faults = 5
  • Page Fault Rate = 5/9 = 55.56%
  • Pages Removed = 3, 4 Compared to FIFO and LRU, the LFU algorithm have improved page fault rate (55.56% vs 75% and 83.33%). But there is a drawback of LFU algorithm. It can lead to starvation of pages that are not regularly used. For example, if a page is accessed once at the beginning and then not used again, it will hard to get back into memory because its access count will remain low.

Optimal Page Replacement

The optimal page replacement algorithm is a theoretical concept which is very difficult to be implemented in real operating systems. This algorithm works by replacing the page that will not be used for the longest period of time in the future. Meaning, it involves predicting what pages will be used in the future and replacing the one that will not be used for the longest time. This is considered as the best page replacement algorithm. Let's solve an example that involves the optimal page replacement algorithm to understand how it works. Example Consider the following page reference string and frames āˆ’

  • Page Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3
  • Frames: 4 Here, the first four pages 7, 0, 1, 2 are loaded directly into the first four empty frames. When the next page 0 is referenced, it is already in memory, so no page fault occurs. Next, page 3 is referenced, which causes a page fault. So the optimal algorithm will remove page 1 since it will not be used for the longest period of time in the future (it won't be used again). The table below shows all the steps of the process – 7 0 1 2 0 3 0 4 2 3 0 3 2 3 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 4 4 4 4 4 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 7 7 7 7 7 3 3 3 3 3 3 3 3 3 Mis s Mis s Mis s Mis s Hi t Mis s Hi t Mis s Hi t Hi t Hi t Hi t Hi t Hi t

We are able to implement this algorithm as we already know the entire page reference string. But that will not be the case in real-world scenarios. You never know what pages will be referenced in the future. That's why we told earlier that this algorithm is just a theoretical model.