Paging in Operating System (OS): Real-life analogy

Paging in Operating System (OS): Real-life analogy

7 mins read8.9K Views Comment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on May 14, 2024 15:43 IST

Do you want to learn paging in operating system with real life analogy? Then you have landed on the right article.This article also explains the working of paging technique.

Paging in Operating System

Operating systems have come a long way since the days of MS-DOS. With the advent of powerful CPUs and more memory available, systems have become smarter and more capable. However, all that power can come at a cost – slower system responses. To avoid this, modern operating systems employ paging memory management techniques. This technique allows the operating system to “page” data from memory into and out of physical blocks on the hard drive. In this blog, we will be talking about Paging in the Operating System and will check how it works. Before understanding, I recommend you understand Memory management. This topic is very important from the interview and exam point of view. I myself was asked questions about this concept in an interview.

Must Check: History of Operating Systems

Let’s assume you have a cake which you want to store in the refrigerator. There will be two situations in this case.

  1. When you have enough space: You have enough space in the refrigerator and will store the cake by just placing it. This means you were having contiguous space available in the refrigerator.
  2. When you don’t have enough space: In this situation, you have to cut the pieces of cake and put them in the refrigerator wherever space is available. This available space is in a non-contiguous way. Paging is based on this concept.
2022_06_image-121.jpg

 

What is Paging?

Non-contiguous memory management technique for retrieving processes from the secondary storage into the main memory.

Paging is a memory management technique for retrieving processes from secondary memory storage units as pages and stored in the main memory’s frame.

When a program needs to access data, it sends a request to access a process to the operating system, which stores the process in the main memory from the secondary memory. Each process is divided into small fixed-sized chunks called pages; similarly, the main memory will be divided into equal-fixed-sized pieces called frames. The process pages are stored at a different location in the main memory. The thing to note here is that the size of the page and frame will be the same.

Like in the above fig, every page in secondary memory is 2 Kb, and in the same way, every frame in main memory is also 2 Kb.

The problem is that physical memory is finite. When all of the spaces in physical memory are filled with requests, the operating system has to start swapping the processes which are not in use to make room for new ones. This process is called swapping.

Working of Demand Paging in Operating System
Working of Demand Paging in Operating System
This article explain a very important topic of operating system-Demand paging with real-life analogy
CPU Scheduling Algorithm: Operating System
CPU Scheduling Algorithm: Operating System
Scheduling of processes is very important thing in an operating system. CPU Scheduling Algorithm is an essential part of any operating system. Various algorithms can be used, each with advantages...read more
Process Scheduling: Operating System
Process Scheduling: Operating System
Have you ever wondered how your computer juggles multiple tasks simultaneously? To know that, you must read this blog on process scheduling in the operating system! This article includes...read more

How does Paging Work?

This process is quite simple. Your computer has a finite amount of memory, which is used to store programs and data currently being used. When that memory is complete and you try to open another program or save a document, your computer has to get creative.

It can’t just delete old files or programs to free up space because that would mean deleting data or programs you may need later. So instead, it looks for unused memory in other parts of the computer. When it finds some, it copies the necessary data and deletes the program or file from memory.

This process is known as paging in operating systems.

Also read: What is Operating Systems (OS) – Types, Functions, and Examples

Also read: Operating system interview questions

Address translation in paging

We have two types of addresses.

1. Logical address: An address generated by the CPU is commonly referred to as a logical address. A set of logical addresses generated by a program is a logical address space. 

2. Physical address: An address seen by the memory unit—that is, the one loaded into the memory address register—is commonly referred to as a physical address. A set of physical addresses corresponding to these logical addresses is physical address space.

Let me understand this paging concept.

Firstly we should know that paging follows non-contiguous memory allocation. The pages are stored(from secondary memory ) in the main memory at different locations but will be the same size as the page.

Whenever a request to find a page comes, the CPU tries to find that page on main memory because the access time of main memory is less compared to secondary memory as main memory is smaller than secondary memory. So, if the CPU wants to find the page, the CPU generates a logical address. The main memory has a physical address, so the CPU wants to communicate with the main memory, so the CPU needs to convert the logical to physical. So, we need something that can give us a physical address corresponding to the logical address generated by the CPU. That is done by page table, so what is a page table? The data structure stored in the main memory stores the mapping between logical address and physical address. A logical address will then be mapped to an actual physical address which indicates the frame it’s in, and offset represents the location where the instruction is within the frame.

Here we have a logical address in which the first part represents page number and the second part represents instruction offset.

Suppose here p=2 and page offset=8. This means the CPU wants to access page 2 of the main memory and the eighth instruction of that page. But remember, this address generated was for secondary memory, but the CPU must pick this page from the main memory. So we need a physical address to access the main memory. So CPU will check the page table and get the physical address corresponding to that logical address.

NOTE: Different page tables will be there for different processes.

Advantages of Paging

  • Conserve memory by only keeping the active pages in memory. This is especially helpful in large-scale systems where memory is scarce.
  • Enables the operating system to manage more processes by allowing each process to have its dedicated memory space. This maximizes efficiency and performance by allowing the operating system to schedule and run each process without conflicts.
  • Allows for greater flexibility and scalability regarding the size and complexity of the systems that can be created. 
  • Parts of the program are allowed to store at different locations in the main memory.
  • It solves the problem of external fragmentation.
  • Swapping becomes very easy due to equal-sized pages and frames.

Disadvantages of Paging

  • It can be very inefficient. When a process needs more memory, the operating system must find a block of unused memory and copy it to the process.
  • This process can take a long time and, in some cases, can even crash the system. 
  • Paging can cause internal fragmentation, which makes the system run more slowly.
  • The page table is there, which takes some memory space.
  • Have to maintain a page table for each process.
  • Memory access time increases as the page table needs to be accessed.

When to Use Paging

  • You have too many processes running and not enough physical memory to store them all.
  • When you have a large process that can be split into multiple pages.
  • When you want to load a process into physical memory without loading the entire process. 

Conclusion

Paging is a technique used in most modern operating systems to improve performance. When an application needs more memory than is currently available, the operating system will page it out to disk. This allows the application to continue running without interruption, and the memory can be reclaimed when needed. This article covered Paging in the operating system with a real-life analogy. If this article helped you in any way. Then please share with your friends.

 

FAQs

What is operating system paging?

Paging is a memory mechanism used by operating systems to fetch processes as pages from secondary storage to main memory. The main concept behind paging is to split each process into individual pages. Therefore, primary storage is also divided into frames.

What is demand paging?

Demand paging systems are similar to swapping paging systems in that processes are stored primarily in main memory (usually disk). As such, on-demand paging is a technique that addresses the above problem by simply navigating pages as needed. Lazy swapper is another name (it doesn't swap pages into memory unless necessary).

What is a page fault?

An error occurs if the referenced page does not exist in main memory. This is called a page fault or page fault. The CPU must search for missing pages in secondary storage. A high number of page faults dramatically increases the effective access time of the system.

About the Author
author-image
Anshuman Singh
Senior Executive - Content

Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr... Read Full Bio