Paging in OS
Paging is technique or method used in operating systems for taking data from secondary source/storage and use it in main memory operation and function.
Wikipedia says “In computer operating systems, memory paging is a memory management scheme by which a computer stores and retrieves data from secondary storage[a] for use in main memory. In this scheme, the operating system retrieves data from secondary storage in same-size blocks called pages. Paging is an vital part of virtual memory implementations in new age operating systems, using secondary storage to let programs cross the size of available physical memory.”
It is utilized for faster access to information/data and it is also logical concept. The RAM (the main memory want to say) is parted out into little/small specific-sized blocks of actual memory(physical registers) which are called frames. We must have to keep size of frame same as that of page so as to maximize utilization of RAM (main memory) and for avoiding external problems.
The mapping which is done from virtual memory to physical memory is functioned by memory management unit called MMU. which is a hardware device and this mapping is known as paging technique.
1. The Physical Address Space is conceptually divided into a number of fixed-size blocks, called frames.
2. The Logical address Space is also splitted into fixed-size blocks, called pages.
3. Page Size = Frame Size
We can consider example like:
· If physical Address = 12 bits, then Physical Address Space would be 4 K words
· If logical Address = 13 bits, then Logical Address Space would be 8 K words
· Page size = frame size = 1 K words (assumption)
Paging Hardware
Every address generated by CPU mainly consists of two parts:
1. Page Number(p)
2. Page Offset (d)
where,
Page Number is used as an index into the page table that generally contains the base address of each page in the physical memory.
Page offset is combined with base address in order to define the physical memory address which is then sent to the memory unit.
If the size of logical address space is 2 raised to the power m and page size is 2 raised to the power n addressing units then the high order m-n
bits of logical address designates the page number and the n
low-order bits designate the page offset.
The logical address is as follows:
where p indicates the index into the page table, and d indicates the displacement within the page. The Page size is usually defined by the hardware. The size of the page is typically the power of 2 that varies between 512 bytes and 16 MB per page.
Assuming main memory size 16 KB and frame size is 1 KB hence the main memory will be parted/divided into the collection of 16 frames of 1 KB each.
There are 4 processes in the system that is P1, P2, P3 and P4 having 4 KB each. Each process is parted/divided into pages of 1 KB each by which one page can be stored in one frame.
Initially, all the frames are empty therefore pages of the processes will get stored in the contiguous way.
We can see in following diagram, frames, pages and the mapping between the two.
Assuming that, P2 and P4 are moved to waiting state after some time. Now, 8 frames become empty and hence other pages can be loaded in that empty place. The process P5 of size 8 KB i.e.-of 8 pages is waiting inside the ready queue.
Having 8 non-contiguous frames available in the memory and paging provides the flexibility of storing the process at the different places, we can load the pages of process P5 in the place of P2 and P4.
Main types of Paging
Demand paging
As the name suggests demand paging occurs when the CPU calls for the pages that are residing in the secondary memory into the primary memory. E.g., a process is in execution by the operating system and this process needs some commands or instructions of the other process hence the CPU will demand those specific frames from the pages into the main memory (RAM) to be loaded for the execution of the first process.
Loader paging
The loader paging assumes that the program will be executed completely. Hence, all the processes are loaded and all the process has pages associated with them that are residing in the secondary memory and have to be loaded into the primary memory hence as all the process are loaded therefore all the pages are loaded into the primary memory too.
Anticipatory paging
This paging loads up all the pages that are likely to be referenced in the near future by any processes that are executing in the CPU. It helps to deduct/reduce the number of page faults that occur to call the pages from the secondary memory into the primary memory and hence facilities in faster execution of the programs.
Advantages of Paging
Paging mainly allows to storage of parts of a single process in a non-contiguous fashion.
With the help of Paging, the problem of external fragmentation is solved.
Paging is one of the simplest algorithms for memory management.
Disadvantages of Paging
In Paging, sometimes the page table consumes more memory.
Internal fragmentation is caused by this technique.
There is an increase in time taken to fetch the instruction since now two memory accesses are required.
So, this was a brief overview of Paging in operating systems hope you would have taken some insights from it which will be helpful to you!