In the last article, we talked about threads and different types of threads. In this article we are going to talk about paging and how does it work. You can read the last article below.
What is Paging?
Paging is a memory management scheme by which a computer stores and retrieves data from secondary memory for use in the main memory.
How does paging work?
So this paging memory management scheme is being implemented by the memory management unit(MMU). MMU is a hardware unit that performs this and applies this paging scheme. In paging, we will use some portion of secondary memory as virtual RAM, and this virtual RAM is divided into equal size blocks known as pages, and all these pages are of the same size. We use this virtual RAM to store programs when the physical memory exceeds the actual limit.
In this paging scheme, our RAM is divided into blocks which are known as Frame and the size of this frame is equal to the page size of the virtual RAM.
This virtual memory has its address which is generated by the CPU and this address is known as a virtual address whereas a physical address is an address available on the memory unit and this mapping of the virtual address to physical address is done by the memory management unit and this mapping is known as mapping technique.
Suppose we have a process A which is of size 10kb and this process is divided among 5 pages P1, P2, P3, P4, P5 of size 2kb each.
The above figure represents our whole process A and with the help of this figure, I’m going to explain to you the whole concept of paging step by step. As we know, a logical address is generated by the CPU while the program is running and, this logical address does not exist physically so it is also known as a virtual address. This address is used as a reference by the CPU to access the actual physical memory.
As you see in the figure our CPU is pointing towards an address consists of two parts P and D where P is a page number used as an index in a page table that contains the base address of each in physical memory and D represents page offset which is location difference between the byte address you want and the start of the page. So let us say that our CPU is looking for P5 so the memory management unit checks for P5 which is page 5 of the process A in the appropriate page table which is in the main memory and it will directly go to P5 as it is a direct index, now the memory management unit gets the P5 as a page number now it looking for the frame number which is F22 and then the memory management unit takes this F22 and get the Physical address.
Now move on to page offset which is represented by D which is combined with the base address to define the physical memory address that is sent to the memory unit, we know that process A is divided into 5 pages and each page is also going to have multiple instructions. So, the page offset is basically the address inside the page where exactly the instructions are situated, and by this, the entire memory management unit resolves the physical address and the CPU gets the physical address and it goes to P5 of process A and starts executing it and this whole process is known as Paging.
We also go through a term known as page fault so what happens is when the desired page which is about to be executed and this page is mapped into the virtual memory space instead of physical memory space(RAM) a fault arises and this fault notifies the operating system that it must locate the data in virtual memory space(HDD) and transfer it to system physical memory(RAM). To read more about page fault follow the below link.
To read more about page fault follow the below articles.
If you like the article please share and subscribe.