In this article, we will deep dive and discuss what is paging in os, page replacement algorithms in os, demand paging in os, and page fault in os. Paging in the operating system plays an important role.
What is Paging in OS?
The OS uses paging, a storage method, to recover processes as pages from secondary storage and place them in main memory. The fundamental idea behind paging is to divide each process into separate pages. As a result, the main memory would also be divided into frames.
One process page needs to be saved in each of the available memory frames. These pages may be kept in different parts of memory, but the basic objective is always to locate continuous frames or holes. Process pages are typically only saved in secondary storage until they are required to be in the main memory.
The OS may influence the frame sizes. Every frame needs to have the exact same size. The page size should be similar to the frame size since the pages used in paging are mapped into the frames.
Example of Paging in OS
The main memory will be divided into a group of 16 1 KB frames if the main memory is 16 KB and the frame size is 1 KB. The four processes in the system are P1, P2, P3, and P4. They are each 4 KB in size. One page can be saved in a single frame because each procedure is divided into 1 KB pages.
The pages of the processes will be continuously stored because all of the frames are originally empty. Frames, pages, and their mapping are shown in the diagram below.
Think about the scenario where P2 and P4 are eventually sent to the waiting state. Now that eight frames are empty, other pages can be loaded in their place. The process P5, which is 8 KB (8 pages) in size, is located in the ready queue.
We can save the process in multiple places thanks to paging because there are 8 noncontiguous frames that are available in memory. So, rather than loading pages P2 and P4, we can load page P5.
Now, we have a clear idea about the paging in the operating system. Let’s discuss page replacement algorithms in os.
Page Replacement Algorithms in Operating System
Which memory page needs to be replaced is decided by the page replacement algorithm. Writing to disk or swapping out are other names for the replacement operation. When the requested page cannot be found in the main memory, page replacement is carried out (page fault).
Frame allocation and Page Replacement are the two primary components of virtual memory. Having the best frame allocation and page replacement strategy is crucial. The question of how many frames should be allotted to the process is addressed by frame allocation, but the question of which page needs to be replaced to create room for the requested page is addressed by page replacement.
What If the algorithm is not optimal?
Thrashing issues might occur if a process is not given an adequate or accurate amount of frames to work with. The majority of the pages will be stored in main memory because there won’t be any frames, which will lead to more page faults.
Internal fragmentation, however, may occur if the OS allots the process extra frames.
The issue of thrashing will also exist if the page replacement algorithm is poor. There will be more swap-ins and swap-outs if the number of pages to be replaced by the desired pages is referred to in the near future. As a result, the OS will need to perform more replacements than usual, which will affect performance.
An ideal page replacement algorithm’s duty is to select the page that can reduce thrashing.
Types of Page Replacement Algorithms
There are various page replacement algorithms. Each algorithm has a unique approach for replacing the pages.
- Optimal Page Replacement algorithm – This algorithm replaces the page which has not been referred for a long time. . Although it can not be practically implementable, it can be used as a benchmark. Other algorithms are compared to this in terms of optimality.
- Least recent used (LRU) page replacement algorithm – This algorithm replaces the page which has not been referred for a long time. This algorithm is just opposite to the optimal page replacement algorithm. In this, we look at the past instead of staring at the future.
- FIFO – In this algorithm, a queue is maintained. The page which is assigned the frame first will be replaced first. In other words, the page which resides at the rare end of the queue will be replaced on every page fault.
What is Demand Paging in OS (Operating System)?
According to the idea of virtual memory, only a small portion of a process needs to be present in the main memory at any given time in order for it to be executed, hence only a small number of pages are ever stored there.
However, choosing which pages should be retained in the primary memory and which should be preserved in the secondary memory will be challenging since we cannot predict in advance which page will be needed at which time by a process.
Therefore, to overcome this problem, a concept called Demand Paging is introduced. It advises storing every frame’s page in the backup memory until it is needed. In other words, it advises against loading any pages into the main memory until they are absolutely necessary.
Every time a page is referenced in the main memory for the first time, the secondary memory contains that page as well.
Depending on the page replacement process, which will be addressed later in this lesson, it may or may not thereafter be present in the main memory.
What is a Page Fault?
The term "page miss" or "page fault" refers to the concept of a miss that occurs if the referred page is not existent in the main memory.
The missing page must be accessed by the CPU from secondary memory. The system’s effective access time will increase if the number of page faults is quite high.
What is Thrashing?
The effective access time will be as long as it takes the CPU to read one word from secondary memory if there are as many page faults as referred pages or if there are so many page faults that the CPU is constantly reading pages from secondary memory. Thrashing is the name given to the idea.
When the memory access time is ma, the page fault rate is PF%, and it takes S (service time) to retrieve a page from secondary memory before resuming, the effective access time can be calculated as follows:
EAT = PF X S + (1 - PF) X (ma)
In this article, we had discussed paging in os, page replacement algorithms in os, demand paging in os and page fault in os. Operating systems are one of the most interesting topics of the computer. Also, in the interviews operating system is one of the most asked topics in the interviews. Practice more and more to increase the efficiency in the operating system.